我正在尝试使用JDBC,但我的查询在某些情况下有效,但在其他情况下则无效. 我将非常感谢您的帮助.
我的一些代码:
public Result getSpecificTopic() { String query = "Select msg_body, msg_author from lawers_topic_msg";// where msg_id=2 order by msg_id desc"; try { con = mysql.getConnection(); //Statement stmt = con.createStatement(); PreparedStatement stmt = con.prepareStatement(query); //stmt.setInt(1, topicId); ResultSet rs = stmt.executeQuery(query); int rowCount = rs.getRow(); specificTopic = ResultSupport.toResult(rs); con.close(); stmt.close(); } catch(Exception e) { } return this.specificTopic; } public void setTopicId(String num) { this.topicId = Integer.parseInt(num); } public int getTopicId() { return this.topicId; }但是如果我改变
String query = "Select msg_body, msg_author from lawers_topic_msg";到
String query = "Select msg_body, msg_author from lawers_topic_msg where msg_id = " + topicId;然后结果集将不会重新调整任何内容.... 我在这里伤脑筋,仍然无法找出问题所在
解决方案第一步,确保没有引发异常是值得的-至少要在catch()块中记录一些内容.
还值得记录所生成的SQL,并确保在直接运行它时实际上返回了您期望从数据库中返回的内容.
如果您有多个数据库,则值得确认您正在与自己认为的数据库相对比-我很尴尬地承认我以前曾经被这种方式迷住过.
I am trying to use JDBC and my query is working in some cases but not working in others. I would really appreciate any help.
Some of my code:
public Result getSpecificTopic() { String query = "Select msg_body, msg_author from lawers_topic_msg";// where msg_id=2 order by msg_id desc"; try { con = mysql.getConnection(); //Statement stmt = con.createStatement(); PreparedStatement stmt = con.prepareStatement(query); //stmt.setInt(1, topicId); ResultSet rs = stmt.executeQuery(query); int rowCount = rs.getRow(); specificTopic = ResultSupport.toResult(rs); con.close(); stmt.close(); } catch(Exception e) { } return this.specificTopic; } public void setTopicId(String num) { this.topicId = Integer.parseInt(num); } public int getTopicId() { return this.topicId; }However if i change
String query = "Select msg_body, msg_author from lawers_topic_msg";to the
String query = "Select msg_body, msg_author from lawers_topic_msg where msg_id = " + topicId;Then the resultset retunrs nothing.... I am breaking my head here and still cannot figure out what is the problem
解决方案As a first step, it'd be worth making sure an exception's not being thrown - at the very least log something in your catch() block.
Also be worth logging the SQL generated, and making sure that actually returns what you expect from the database when running it directly.
If you have multiple databases, it'd be worth confirming you're running against the one you think you are - I'm embarrassed to admit I've been caught out that way before.
更多推荐
JDBC的奇怪问题,select返回null
发布评论