JDBC的奇怪问题,select返回null

编程入门 行业动态 更新时间:2024-10-23 20:30:21
本文介绍了JDBC的奇怪问题,select返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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

本文发布于:2023-10-24 13:47:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   JDBC   null   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!