Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Lỗi Com.microsoft.sqlserver.jdbc.sqlserverexception: The Result Set Is Closed.

Discussion in 'MS SQL Server' started by nguyenndm, 9/12/19.

  1. nguyenndm

    nguyenndm New Member

    Chào mọi người,
    Em là thành viên mới và mới học Java. Hiện em đang làm bài tập làm việc với MS SQL và có xuất hiện lỗi.
    Code của em:

    package QuanLy;
    import java.sql.*;
    public class QuanLy {
    public static void main(String[] args) throws ClassNotFoundException {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    try {
    connection = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=QLLTCK;integratedSecurity=true;");
    PreparedStatement preparedStatement=connection.prepareStatement("SELECT * FROM PHANCONG");
    resultSet = preparedStatement.executeQuery();

    statement = connection.createStatement();
    resultSet = statement.executeQuery("SELECT * FROM PHANCONG");
    while(resultSet.next()) {
    System.out.println(resultSet.getString("id") + "\t"+ resultSet.getString("maLop")
    + "\t"+ resultSet.getString("tenMH") + "\t"+ resultSet.getString("ngayThi")
    + "\t"+ resultSet.getString("caThi") + "\t"+ resultSet.getString("phongThi")
    + "\t"+ resultSet.getString("SoSV") + "\t"+ resultSet.getString("SoCBCT"));
    }

    statement = connection.createStatement();
    statement.executeUpdate("ALTER TABLE PHANCONG ADD xepLoai nvarchar(15)");
    ResultSet soSV = statement.executeQuery("SELECT soSV / 25 FROM PHANCONG");
    ResultSet soCBCT = statement.executeQuery("SELECT soCBCT FROM PHANCONG");
    //neu so sinh vien / 25 nho hon so can bo coi thi thi DAT, con lai KHONG DAT
    while(SoSV.next()) {
    if(soSV.getInt("soSV") <= soCBCT.getInt("soCBCT")) {
    statement = connection.createStatement();
    statement.executeUpdate("INSERT INTO PHANCONG [(xepLoai)] VALUES (N'Đạt')");
    }
    else {
    statement = connection.createStatement();
    statement.executeUpdate("INSERT INTO PHANCONG [(xepLoai)] VALUES (N'Không Đạt')");
    }
    }

    } catch (SQLException e) {
    e.printStackTrace();
    }
    finally {
    try {
    if(null != connection) {
    // cleanup resources, once after processing
    resultSet.close();
    statement.close();
    // and then finally close connection
    connection.close();
    }
    }
    catch (SQLException sqlex) {
    sqlex.printStackTrace();
    }
    }
    }
    }

    Sau khi chạy thì em bị lỗi:
    com.microsoft.sqlserver.jdbc.SQLServerException: The result set is closed.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.checkClosed(SQLServerResultSet.java:443)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(SQLServerResultSet.java:2309)
    at QuanLyThiCuoiKy/QuanLy.QuanLy.main(QuanLy.java:33)

    Mong mọi người có thể giải đáp giúp em ạ.
    Em cảm ơn.
     
    Last edited: 9/12/19
  2. quydtkt

    quydtkt Administrator

    Ban hãy upload full file code này để mình debug thử. Mình nghĩ cần thiết lập lại transaction là ổn
     
  3. Joe

    Joe Thành viên VIP

    The basic rule for DB is that no other transaction should be executed in the same connection before the result set from the first transaction is traversed. And that is done in this loop:
    PHP:
          while(SoSV.next()) {
            if(
    soSV.getInt("soSV") <= soCBCT.getInt("soCBCT")) {
              
    statement connection.createStatement();
              
    statement.executeUpdate("INSERT INTO PHANCONG....");
            }
            else {
              
    statement connection.createStatement();
              
    statement.executeUpdate("INSERT INTO PHANCONG....");
            }
          }
    And the result is what you've seen: result set is closed.
    Solution: try to detach them.
     
    Last edited: 13/12/19
    Thanhpv likes this.

Chia sẻ trang này

Loading...