2016-07-16 33 views
0

後,我試圖運行下面的代碼,但得到的錯誤:如何解決拋出的錯誤SQL異常:java.sql.SQLException中:不允許操作的ResultSet關閉

SQL Exception thrown: java.sql.SQLException: Operation not allowed after ResultSet closed.

如何解決這個問題?我需要兩個結果集用於我的應用程序。

public static void main(String[] args) { 
     String connectionUrl = "jdbc:mysql://127.0.0.1:3306/test"; 
     String dbUser = "root"; 
     String dbPwd = "Syntel#92"; 
     Connection conn; 
     ResultSet rs, res1 = null; 
     Statement stmt = null; 
     int rowcount = 0; 
    // String queryString = "create table job_status_table as select j1.job_id,s1.Source_ID,s1.Source_name from JOb_list j1,SOURCE_DETAILS s1 where s1.Source_ID = j1.Source_ID"; 
     String queryString = "create table job_status_table as select source_id,source_name from source_details"; 

     String addcolumn = "alter table job_status_table add column Source_rowcount int"; 
     String fetchdata = "Select Source_name from job_status_table"; 
     try { 
      conn = DriverManager.getConnection(connectionUrl, dbUser, dbPwd); 
      stmt = conn.createStatement(); 
      // get record count from table job_status_table1 
      // stmt.executeQuery("select count() from job_status_table1"); 
      // create table 
      stmt.executeUpdate(queryString); 
      System.out.println("Table created in the database"); 
      stmt.executeUpdate(addcolumn); 
      System.out.println("alter table"); 
      rs = stmt.executeQuery(fetchdata); 
      System.out.println("fetch data"); 
      while (rs.next()) { 
       String table_count = null; 
       String table_name = null; 




        table_name = rs.getString("Source_name"); 
        System.out.println(table_name); 


      // table_name = rs.getString("Source_name"); 
       //System.out.println(table_name); 
       //rs.close(); 

        table_count = "select count(*) from " + table_name; 
       //table_count = "select count(*) from " + table_name; 
       //rs.close(); 
      // res1 = stmt.executeQuery(table_count); 

       res1 = stmt.executeQuery(table_count); 
       //System.out.print(res1); 
       if (res1.next()) { 

        rowcount = res1.getInt(1);//res1.getInt(1); 
        System.out.println("Result set values" + rowcount); 

       } else { 
        System.out.println("value is not present in resultset"); 
       } 


       System.out.println("Get Row Count"); 
        System.out.println(table_count); 
      //  int cnt = rcnt(table_count); 
       String updaterow = "update job_status_table set Source_rowcount =" 
         + rowcount 
         + " where Source_name = '" 
         + table_name 
         + "'"; 

       System.out.println("updateoutput" +stmt.executeUpdate(updaterow)); 

       System.out.println("Update Complete"); 

      } 

    /*  if (conn != null) { 

       rs.close(); 
       res1.close(); 
       stmt.close(); 
       conn.close(); 
       conn = null; 
      } 
      */ 
     } 
     catch (SQLException sqle) { 
      System.out.println("SQL Exception thrown: " + sqle); 
     } 
    } 
}** 
+0

請參閱本http://stackoverflow.com/questions/5840866/ gets-java-sql-sqlexception-operation-not-allowed-after-resultset-closed –

+1

您應該使用不同的語句對象來執行循環中的查詢(並且應該禁用自動提交)。 –

回答

0

你可以試試這個:

首頁複印在ArrayListResultSet rs並關閉它。

在更新之前迭代ArrayList並關閉res1

而且我不認爲其他的「結果集中不存在值」是可到達的,但是如果您應該將rowcount設置爲0

編輯

讀取引用問題,第二次後: 的問題是stmtres1重用和更新

相關問題