【发布时间】:2013-08-20 08:12:45
【问题描述】:
我在java中的数据库有一个问题
我的代码是(它只是我项目的一小部分)
public void Read_from_DB(int exhibition_id){
Statement stmt = null;
Connection connect = null;
try {
connect=MYConnection.new_connection();
stmt = connect.createStatement();
QuestionCatalog.get_QuestionCatalog_instance().setShow_quest(new ArrayList<Question>());
String sql = "SELECT * FROM question WHERE Selection=0 AND exhibition_id="+exhibition_id;
//System.out.println(sql);
ResultSet rs = stmt.executeQuery(sql);
System.out.println("!");
System.out.println("->"+rs.getFetchSize());
while(rs.next()){
Question jd=new Question();
System.out.println("!!!");
jd.setQuestion_id(rs.getInt("Question_id"));
jd.setQuestion(rs.getString("Question"));
jd.setQuestion(rs.getString(exhibition_id));
jd.getOption_2().setContent(rs.getString("Content2"));
QuestionCatalog.get_QuestionCatalog_instance().getShow_quest().add(jd);
System.out.println("size"+QuestionCatalog.get_QuestionCatalog_instance().getShow_quest().size());
MYConnection.close_connection(stmt, connect);
}
}catch (Exception e) {
}
}
当我执行此代码时,它不起作用 我的数据库表名是“问题” 但是当我将此查询中的名称更改为“问题”时,不会出现任何错误 然后我认为它不执行我的查询,我的主要是
public static void main(String[] args) {
DB_question d=new DB_question();
d.Read_from_DB(1);
}
和“MYConnection.new_connection();”部分代码返回一个连接,(我在另一个类中对其进行测试)
控制台中的结果是:
SELECT * FROM Question WHERE Selection=0 AND exhibition_id=1
!
->0
它没有显示 "!!!" 那是 "System.out.println("!!!");" 的结果 然后我认为它不起作用:| 谢谢
p.s 我的数据库的图片 picture
【问题讨论】:
-
尝试将上述查询直接传递给数据库。你得到什么结果?
-
@Marty McVry : 没什么,我在我的 main 中说 sysout 的结果,在我的数据库中什么也没有发生,当我写错 qury 时它不会给我任何错误!!!
-
向我们展示一些示例数据。
-
您的 mysql 表中可能没有 Selection=0 和 Exhibition_id=1 的问题,因此您的 select 语句返回 0 行。
-
它怎么会给你一个错误:你正在捕捉异常,并完全忽略它。删除 catch 块,至少在 catch 块中添加
e.printStackTrace(); throw new RuntimeException(e);。
标签: java mysql database select