
Column '' not found .但是表里明明有。初学者,整一晚上了,在线等
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;impor...
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 + " 的意思是: " + mean);
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
程序运行老是显示String spell = res.getString("word");这一句出错:Column 'word' not found
把后面改成res.getString(1)可以,但是显示的是后面的mean部分,改成res.getString(2)就说超过了
但是表中明明有,用的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 | 笔 |
| 5 | apple | 苹果 |
| 6 | card | 卡片 |
| 7 | pig | 猪 |
| 8 | dog | 狗 |
+----+-------+------+
5 rows in set (0.00 sec)
mysql> 展开
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 + " 的意思是: " + mean);
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
程序运行老是显示String spell = res.getString("word");这一句出错:Column 'word' not found
把后面改成res.getString(1)可以,但是显示的是后面的mean部分,改成res.getString(2)就说超过了
但是表中明明有,用的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 | 笔 |
| 5 | apple | 苹果 |
| 6 | card | 卡片 |
| 7 | pig | 猪 |
| 8 | dog | 狗 |
+----+-------+------+
5 rows in set (0.00 sec)
mysql> 展开
3个回答
展开全部
String sql = "select mean from tb_dic where word = 'pig' ";
你看,你sql语句中,查询到了什么?你查的是“mean”这行。你要知道res这个数据集中,返回的是你查询的结果。也就是说。res里边是:
+----+-------+------+
| mean |
+----+-------+------+
| 笔 |
| 苹果 |
| 卡片 |
| 猪 |
| 狗 |
+----+-------+------+
那你说,能有2列数据么?
你看,你sql语句中,查询到了什么?你查的是“mean”这行。你要知道res这个数据集中,返回的是你查询的结果。也就是说。res里边是:
+----+-------+------+
| mean |
+----+-------+------+
| 笔 |
| 苹果 |
| 卡片 |
| 猪 |
| 狗 |
+----+-------+------+
那你说,能有2列数据么?
展开全部
String sql = "select mean from tb_dic where word = 'pig' ";
应该改为String sql = "select word,mean from tb_dic where word = 'pig' ";
因为你word没查,怎么可能取String spell = res.getString("word");
应该改为String sql = "select word,mean from tb_dic where word = 'pig' ";
因为你word没查,怎么可能取String spell = res.getString("word");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
where word = 'pig' ,可以看一下这里
where word=' ”+pig+“ '
where word=' ”+pig+“ '
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询