代码之家  ›  专栏  ›  技术社区  ›  Samuel Ludwig

执行查询[重复]后立即关闭结果集

  •  0
  • Samuel Ludwig  · 技术社区  · 6 年前

    我使用以下方法在JavaFX应用程序中显示来自数据库的信息

    public void showFirstSix() {
        PreparedStatement takeFields = null;
        ResultSet rs = null;
        String sqlTakeFields = "SELECT * FROM VirtualProductTable WHERE VirtualProductTable MATCH name=? ORDER BY rank DESC";
        try {
            takeFields = this.newSearchModel.connection.prepareStatement(sqlTakeFields);
            takeFields.setString(1, nameSearch.getText());
            rs = takeFields.executeQuery();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    
        // gets a sorted list of matching products, next we just need to print them out entry by entry //
        // we'll traverse every row in the result set, and print all needed values before moving to the next row //
    
        try {
    
            // first entry
            String id = rs.getString(2);
            String name = rs.getString(3);
            String description = rs.getString(4);
            String price = rs.getString(5);
            String length = rs.getString(6);
            String width = rs.getString(7);
            String height = rs.getString(8);
            String dateAdded = rs.getString(9);
            Blob image = rs.getBlob(14);
    
            id1.setText(id);
            name1.setText(name);
            description1.setText(description);
            dateAdded1.setText(dateAdded);
            price1.setText(price);
            length1.setText(length);
            width1.setText(width);
            height1.setText(height);
            image1.setImage((Image) image);
    
            // second entry
            rs.next();
    
            id = rs.getString(2);
            name = rs.getString(3);
            description = rs.getString(4);
            price = rs.getString(5);
            length = rs.getString(6);
            width = rs.getString(7);
            height = rs.getString(8);
            dateAdded = rs.getString(9);
            image = rs.getBlob(14);
    
            id2.setText(id);
            name2.setText(name);
            description2.setText(description);
            dateAdded2.setText(dateAdded);
            price2.setText(price);
            length2.setText(length);
            width2.setText(width);
            height2.setText(height);
            image2.setImage((Image) image);
    
            ...
    
        } catch (SQLException e1) {
            e1.printStackTrace();
        } 
    
    }
    

    执行此方法后,我在第二个try块中得到一个指向第一个rs.getString()行的错误,声称ResultSet已关闭

    我真正的问题似乎发生在第一个try块中,紧接着 rs = takeFields.executeQuery() - 我用一些 System.out.println(rs.isClosed()) System.out.println(takeFields.isClosed()) 在上述查询之后,并确认准备好的语句takeFields实际上仍然处于打开状态,但结果集rs在查询执行后立即关闭。

    我在研究类似的问题,我已经确认,这个特定的准备好的语句从未在其他任何地方使用或引用过,也从未关闭过,所以我试图弄清楚为什么这个结果集总是立即关闭,以及如何使其配合。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Adrian    6 年前

    典型的处理结果集迭代器使用下一个方法调用,如下所示:

      ResultSet rs = stmt.executeQuery(query);
      while (rs.next()) {
    
        //read record
      }
    

    在您的情况下,请确保在阅读第一条记录之前拨打next:

      ResultSet rs = stmt.executeQuery(query);
      if(! rs.next()) {
        return;
      }
      //read first reacord
    
       if(! rs.next()) {
        return;
      }
      //read second reacord