0

当我执行搜索查询时,我收到错误“java.sql.SQLException:空结果集上的非法操作”。我想从我的数据库中获取日期到 jDateChoser 上。这是我试过的,请帮帮我!提前致谢!

try { 
        Statement s = DB2.kavish().createStatement();

        ResultSet rs = s.executeQuery("SELECT * FROM Invoice WHERE Invoice_No='"+jTextField28.getText()+"'");

        while (rs.next());

        jTextField29.setText(rs.getString(2));
        jTextField30.setText(rs.getString(3));
        jTextField31.setText(rs.getString(4));
        jTextField32.setText(rs.getString(5));
        jTextField33.setText(rs.getString(6));
        jTextField34.setText(rs.getString(7));
        jTextField35.setText(rs.getString(8));
        jDateChooser1.setDate(rs.getDate(9));


    } catch (Exception e) {
    e.printStackTrace();
    }
4

1 回答 1

0

使用以下方式

try{ 
Statement s = DB2.kavish().createStatement();
        ResultSet rs = s.executeQuery("SELECT * FROM Invoice WHERE Invoice_No='"+jTextField28.getText()+"'");

        while (rs.next()){

        jTextField29.setText(rs.getString(2));
        jTextField30.setText(rs.getString(3));
        jTextField31.setText(rs.getString(4));
        jTextField32.setText(rs.getString(5));
        jTextField33.setText(rs.getString(6));
        jTextField34.setText(rs.getString(7));
        jTextField35.setText(rs.getString(8));
        jDateChooser1.setDate(rs.getDate(9));

}
    } catch (Exception e) {
    e.printStackTrace();
    }

;在这一行有分号while (rs.next()); 删除它

最初,光标位于第一行之前。rs.next 方法将光标移动到下一行。如果光标位于最后一行之后,则此方法返回 false。此方法使用 while 循环重复调用 ResultSet.next 方法,以遍历 ResultSet 中的所有数据。您正在尝试在 while 循环之后检索,这意味着光标已经在底部,因此没有数据存在。因此它抛出异常

笔记

防止SQL InjectionPreparedStatement代替Statement如下

PreparedStatement pt=DB2.kavish().prepareStatement("SELECT * FROM Invoice WHERE Invoice_No=?");
pt.setString(1,jTextField28.getText());
ResultSet rs=pt.executeQuery();
while (rs.next()){

    jTextField29.setText(rs.getString(2));
    jTextField30.setText(rs.getString(3));
    jTextField31.setText(rs.getString(4));
    jTextField32.setText(rs.getString(5));
    jTextField33.setText(rs.getString(6));
    jTextField34.setText(rs.getString(7));
    jTextField35.setText(rs.getString(8));
    jDateChooser1.setDate(rs.getDate(9));

}
} catch (Exception e) {
e.printStackTrace();
}
于 2013-09-21T11:19:25.273 回答