无法弄清楚为什么sqlite查询没有返回正确的结果

时间:2013-05-14 23:04:36

标签: java sqlite

我有这个sqlite数据库,我可以运行查询“select * from barcode where barcode ='CFMS-ZUFH-WRVY-EXAA'并获得单行返回。但是在我的Java应用程序中运行时几乎一样sql,它返回一个空的结果集。变量last,email等是JTextFields,虽然问题出在他们面前。如果我在它之前运行rs.next()并设置if(true),if语句甚至都不执行我收到错误,说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;
}

3 个答案:

答案 0 :(得分:1)

您的预期条形码可能仍有空格。将其分配给replaceAll

的结果
barcode = barcode.replaceAll("\\s","");

答案 1 :(得分:1)

query方法中,在返回PreparedStatement之前关闭ResultSet。根据{{​​3}}:

  

注意:当Statement对象关闭时,它的当前ResultSet对象,   如果存在,也会关闭。

请勿关闭PreparedStatement中的query,请在使用ResultSet后将其关闭。我会删除query方法并将其代码与其余代码内联,这样您在完成PreparedStatement后仍然可以引用ResultSet

正如@Reimeus已经提到的那样,将replaceAll的结果分配回barcode

答案 2 :(得分: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子句条件匹配的正确值的情况下,这必须起作用。在测试运行时,请仔细检查您的查询是否成功执行。