2010-10-29 5 views
9

Tôi đang sử dụng apache derby cho cơ sở dữ liệu của tôi. Tôi có thể thực hiện chèn vào cơ sở dữ liệu. Sau đây là đoạn trích từ mã tìm cách hiển thị nội dung của bảng duy nhất của tôi 'MAINTAB'. Ví dụ của java.sql.Connection là 'dbconn'.Resultset không mở. Xác minh Autocommit là OFF. Apache Debry

ResultSet word; 

    Statement query; 

    String getData="SELECT THEWORD FROM MAINTAB"; 
    try{ 
     System.out.println(dbconn.getAutoCommit()); 
     query = dbconn.createStatement(); 
     word = query.executeQuery(getData); 
     query.close(); 

     dbconn.setAutoCommit(false); 
     System.out.println(dbconn.getAutoCommit()); 

     for(;word.next();) 
      System.out.println(word.getString(1)); 

    }catch(Throwable e){ 
     System.out.println("Table fetch failed or result data failed");} 

Và sau đây là đầu ra.

org.apache.derby.jdbc.EmbeddedDriver loaded. 
Database testDB connected 
true 
false 
Table fetch failed or result data failed 

---SQLException Caught--- 

SQLState: XCL16 
Severity: 20000 
Message: ResultSet not open. Operation 'getString' not permitted. Verify that autocommit is OFF. 
java.sql.SQLException: ResultSet not open. Operation 'getString' not permitted. Verify that autocommit is OFF. 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.ConnectionChild.newSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedResultSet.checkIfClosed(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedResultSet.getString(Unknown Source) 
Closed connection 
    at test.ShowData.main(ShowData.java:30) 
Caused by: java.sql.SQLException: ResultSet not open. Operation 'getString' not permitted. Verify that autocommit is OFF. 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(



Unknown Source) 
    ... 9 more 
Database shut down normally 

Khi nào, nó lần đầu tiên yêu cầu xác minh nếu Autocommit là OFF, tôi đã tìm thấy từ các tài liệu Derby rằng Autocommit được bật theo mặc định cho bất kỳ kết nối. Vì vậy, tôi đã tắt nó bằng cách sử dụng dbconn.setAutoCommit (false). Tuy nhiên, lỗi được ném.

Đầu ra trước khi lỗi giải thích rằng tập hợp kết quả được tìm nạp mà không có bất kỳ lỗi nào. Ngoài ra, xin lưu ý rằng cùng một lỗi được ném ngay cả khi tôi không đặt AutoCommit thành false. Giữa, tôi đang chạy trận derby trên nhật thực.

Trả lời

15

Sự cố là bạn đã đóng truy vấn trước khi đọc kết quả của mình. Đóng truy vấn, đóng resultset, do đó tại sao bạn nhận được lỗi "ResultSet not open". Bạn nên đóng truy vấn ngay ở cuối, trong một khối finally:

ResultSet word; 

Statement query=null; 

String getData="SELECT THEWORD FROM MAINTAB"; 
try{ 
    System.out.println(dbconn.getAutoCommit()); 
    query = dbconn.createStatement(); 
    word = query.executeQuery(getData); 


    dbconn.setAutoCommit(false); 
    System.out.println(dbconn.getAutoCommit()); 

    for(;word.next();) 
     System.out.println(word.getString(1)); 

}catch(Throwable e){ 
    System.out.println("Table fetch failed or result data failed"); 
} finally{ 
    if(query!=null) { 
     try { 
      query.close(); 
     } 
     catch(SQLException ex) { 
       System.out.println("Could not close query"); 
     } 
    } 
} 
+0

Có, Nó hoạt động ngay bây giờ! Cảm ơn nhiều. Tôi đã đấu tranh nửa ngày! – Sundeep

+0

Ồ, vì vậy, tôi hiểu nó theo cách này - Đóng truy vấn, đóng tập kết quả. Đó là lý do tại sao lỗi nói rằng tập kết quả không mở. – Sundeep