0

我有这个sqlite数据库,我可以运行查询“从条形码中选择*,其中barcode ='CFMS-ZUFH-WRVY-EXAA'并获得单行返回。但是在我的Java应用程序中,当我运行几乎相同的sql时,它是返回一个空的结果集。变量last、email等是JTextFields,尽管问题在它们之前。if语句甚至不执行,如果我在它之前运行rs.next()并设置if(true)我得到一个错误提示 ResultSet 已关闭。

try {
    db.dbopen(config.getdbfolder(),config.getdbname()); 
    // barcode-barcode.replaceAll("\\s","");
    ResultSet rs=db.query("select * from barcode where barcode=?",barcode);

    if (rs.next()) {
        first.setText(rs.getString("first_name"));
        System.out.println(rs.getString("first_name"));
        last.setText(rs.getString("last_name"));
        email.setText(rs.getString("email"));
        phone.setText(rs.getString("phone"));
        subject.setSelectedItem(rs.getString("subject"));
        Boolean selected;
        if (rs.getString("baronly").equals("false")) selected=false;
        else selected=true;
        barcodebox.setSelected(selected);   
        update=true;

    } 
    db.conn.close();

} catch ( SQLException e) {
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

这是方法查询。

public ResultSet query(String sql, String barcode) throws SQLException {
    // TODO Auto-generated method stub
    PreparedStatement prep =conn.prepareStatement(sql);
    prep.setString(1, barcode);

    ResultSet rs= prep.executeQuery();  
    prep.close();
    return rs;
}
4

3 回答 3

1

您想要的条形码可能仍然有空格。将其分配给 的结果replaceAll

barcode = barcode.replaceAll("\\s","");
于 2013-05-14T23:09:11.730 回答
1

在您的query方法中,您PreparedStatement在返回之前关闭ResultSet. 根据Javadocsclose

注意:当 Statement 对象关闭时,其当前的 ResultSet 对象(如果存在)也将关闭。

不要关闭你的PreparedStatementin query,在你使用后关闭它ResultSet。我将删除该query方法并将其代码与您的其余代码内联,以便PreparedStatement您在完成ResultSet.

正如@Reimeus 已经提到的,将replaceAll返回的结果分配给barcode.

于 2013-05-14T23:09:57.133 回答
0

所以这是@Reimeus @rgettman 的最终答案。

        try {
            db.dbopen(config.getdbfolder(),config.getdbname()); 
            //you need to set this replaceAll return value to original variable
            barcode = barcode.replaceAll("\\s","");

            //make prep statement here in order to close it in the finally block
            PreparedStatement prep =conn.prepareStatement("select * from barcode where barcode=?");

            //modify query function to accept prepared statement instead of a string
            ResultSet rs=db.query(prep ,barcode);

            if (rs.next()) {
                first.setText(rs.getString("first_name"));
                System.out.println(rs.getString("first_name"));
                last.setText(rs.getString("last_name"));
                email.setText(rs.getString("email"));
                phone.setText(rs.getString("phone"));
                subject.setSelectedItem(rs.getString("subject"));
                Boolean selected;
                if (rs.getString("baronly").equals("false")) selected=false;
                else selected=true;
                barcodebox.setSelected(selected);   
                update=true;

            } 
            db.conn.close();

        } catch ( SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
         //finally you close prepared statement. note that closing prep also closes 
//resultset and closing connection won't clean up resultset nor prep according to most of 
//stackoverflow answers. However, it is recommended to close both resultset and prepared 
//statement explicitly
         prep.close();
         rs.close();
       }

鉴于您的数据库表具有与 where 子句条件匹配的正确值,这必须有效。请在测试运行时仔细检查您的查询是否成功执行。

于 2013-05-14T23:18:10.337 回答