JDBC存在奇怪的问题,select返回null

Dmi*_*ris 0 java jsp jdbc

我正在尝试使用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;
    }
Run Code Online (Sandbox Code Playgroud)

但是,如果我改变

String query = "Select msg_body, msg_author from lawers_topic_msg";
Run Code Online (Sandbox Code Playgroud)

到了

String query = "Select msg_body, msg_author from lawers_topic_msg where msg_id = " + topicId; 
Run Code Online (Sandbox Code Playgroud)

然后结果集没有任何回复....我在这里打破了我的脑袋仍然无法弄清楚是什么问题

duf*_*ymo 6

您仍然没有正确关闭资源.这应该在finally块中完成:

http://www.java-blog.com/correct-closing-jdbc-resources