2013年12月17日星期二

Column'' not found. , But there are clearly advised the table . Beginner , the whole night , and online

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 ------------ ---------------------------

oh so it is. . Alright . The usage of the sentence did not say I use the book , just to give an example .
Thanks Thanks
------ For reference only ------------------------------ ---------
the top floor , and no way to pro, you write the sql statement to query jdbc found only mean one

没有评论:

发表评论