Spring MVC - 结果集结束后(SQL)

时间:2016-01-07 21:35:57

标签: java mysql spring

我正在尝试从CUSTOMER发送ORDER表的最后一个ID以及PRODUCT表中的所有ID。在“idproduct”(ORDER表)中应该出现所有ID,它们之间有空格。我在这里使用rs.next()两次,我知道这就是错误出现的原因,但我该如何处理呢?这是一个代码:

List<String> IDproducts = new ArrayList<>();

        Connection conn = db.getConnect();
        PreparedStatement pst = conn.prepareStatement("SELECT * FROM `onlineshop`.`customer`");
        ResultSet rs = pst.executeQuery();

        while (rs.next()){
            rs.getString(1);
        }

        Connection conn1 = db.getConnect();
        PreparedStatement pst1 = conn1.prepareStatement("SELECT * FROM `onlineshop`.`product`");
        ResultSet rs1 = pst1.executeQuery();

        while (rs1.next()){
            IDproducts.add(rs1.getString(1));
        }

        db.Query("INSERT INTO `onlineshop`.`order` (`idcustomer`, `idproduct`) 
        VALUES ('"+rs.getString(1)+"','" + IDproducts + ")");

错误:

java.sql.SQLException: After end of result set
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862)
com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790)
com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5244)
com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5167)

0 个答案:

没有答案