import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class dic {
public static void main (String [] args) {
try {
Connection conn = DriverManager.getConnection ("jdbc: mysql :/ / localhost: 3306/test", "root", "bjzhao04200526");
String sql = "select mean from tb_dic where word = 'pig'";
Statement statement = conn.createStatement ();
ResultSet res = statement.executeQuery (sql);
while (res.next ()) {
String spell = res.getString ("word");
String mean = res.getString ("mean");
System.out.println (spell + " means :" + mean);
}
} catch (SQLException e) {
e.printStackTrace ();
}
}
}
program runs always show String spell = res.getString ("word"); This is an error : Column 'word' not found
put back into res.getString (1) can be, but shows the mean part of the back , into res.getString (2) said more than
But obviously there are tables , using MySQL:
mysql> use test;
Database changed
mysql> create table tb_dic (
-> id int auto_increment primary key,
-> word varchar (30) not null,
-> mean varchar (30) not null);
Query OK, 0 rows affected (0.01 sec)
mysql> select * from tb_dic;
+ ---- + ------- + ------ +
| id | word | mean |
+ ---- + ------- + ------ +
| 4 | pen | pen |
| 5 | apple | Apple |
| 6 | card | cards |
| 7 | pig | pig |
| 8 | dog | dogs |
+ ---- + ------- + ------ +
5 rows in set (0.00 sec)
mysql>
------ Solution ---------------------------------- ----------
String sql = "select mean from tb_dic where word = 'pig'";
select mean from tb_dic only come up with one , of course, did not get the second column of the
------ For reference only ------------ ---------------------------
Blessings to you in the web sector in the whole of it. I have no points. Thanked
------ For reference only ----------------------------------- again ----
Blessings to you in the web sector in the whole of it. I have no points. Thanks again wooden thing haha look at the issue based
没有评论:
发表评论