2017-06-18 91 views
1

我正在使用以下代碼連接到3個不同的數據庫並在其上運行查詢。但是,當我從第二個查詢中獲得結果時,它給了我一個錯誤,說明resultSet已經關閉。獲取resultSet已關閉錯誤JDBC

import java.io.*; 
import java.sql.*; 

public class DB { 

    String sourceQueryResults ="no results", cbosQueryResults = "no results", tiQueryResults = "no results"; 
    public DB() {} 


    //below is the function called in Main to run connection 
    public void dbConnect(String sourceConnectString, 
          String db_userid, String db_password, String sourceTablename, String sourceSchemaName, 
          String sourceCondition, String cbosConnectString, String cbosTableName, 
          String cbosSchemaName, String cbosCondition, String tiConnectString, 
          String tiTableName, String tiSchemaName, String tiCondition, String fileName) 
    { 
     try 
     { 
      Class.forName("com.ibm.db2.jcc.DB2Driver"); 
      System.out.println("Loaded!"); 
      /* 
      * Below creates a connection to the databases 
      */ 
      //below attempts to connect to the source DB 
      Connection sourceConn = DriverManager.getConnection(
        sourceConnectString, db_userid, db_password); 
      System.out.println("Source DB Connected at: " + sourceConnectString); 
      System.out.println("Schema: " + sourceSchemaName); 
      System.out.println("Table: " + sourceTablename); 

      //below attempts to connect to the CBOS DB 
      Connection cbosConn = DriverManager.getConnection(cbosConnectString, 
        db_userid, db_password); 
      System.out.println("CBOS DB Connected at: " + cbosConnectString); 
      System.out.println("Schema: " + cbosSchemaName); 
      System.out.println("Table: " + cbosTableName); 

      //below attempts to connect to the CBOS DB 
      Connection tiConn = DriverManager.getConnection(cbosConnectString, 
        db_userid, db_password); 
      System.out.println("TI Db Connected!"); 
      System.out.println("TI DB Connected at: " + tiConnectString); 
      System.out.println("Schema: " + tiSchemaName); 
      System.out.println("Table: " + tiTableName); 



      /* 
      * Below set all of the statement and results for all of the data locations 
      */ 
      Statement sourceStmt = sourceConn.createStatement(); 
      ResultSet sourceRs; 
      Statement cbosStmt = cbosConn.createStatement(); 
      ResultSet cbosRs; 
      Statement tiStmt = tiConn.createStatement(); 
      ResultSet tiRs; 




      /* 
      * Below establishes for all data locations (source/cbos/ti) whether 
      * conditions will be used or not 
      */ 
      //below for source 
      if(sourceCondition == null) { 
       System.out.println("Is there a sourceCondition?"); 
       sourceQueryResults = ("SELECT COUNT(*) as testvalue FROM " + sourceSchemaName + "." + 
         sourceTablename + "WHERE " 
         + sourceCondition); 
       //sets the results to needed query 
       sourceRs = sourceStmt.executeQuery("SELECT COUNT(*) as testvalue FROM " + sourceSchemaName + "." + 
         sourceTablename + "WHERE " 
         + sourceCondition); 
      }else { 
       System.out.println("there is no source condition"); 
       //sets the variable used later in printing of results 
       sourceQueryResults = ("SELECT COUNT(*) as testvalue FROM " + sourceSchemaName + "." + 
         sourceTablename); 
       //this means there is no conditions to select on 
       //sourceRs = sourceStmt.executeQuery("SELECT COUNT(*) FROM " + 
       // sourceSchemaName + "." + sourceTablename + ";"); 
       System.out.println(sourceQueryResults); 
       sourceRs = sourceStmt.executeQuery("SELECT count(*) FROM " + sourceSchemaName + "." + 
         sourceTablename); 

       System.out.println("Got access to the table and selected!!!"); 
      } 

      //below for CBOS 
      if(cbosCondition == null) { 
       cbosQueryResults = ("SELECT COUNT(*) as testvalue FROM " + cbosSchemaName + "." + 
         cbosTableName + "WHERE " 
         + cbosCondition); 
       //sets the results to needed query 
       cbosRs = cbosStmt.executeQuery("SELECT COUNT(*) as testvalue FROM " + cbosSchemaName + "." + 
         cbosTableName + "WHERE " 
         + cbosCondition); 
      }else { 
       //sets the variable used later in printing of results 
       cbosQueryResults = ("SELECT COUNT(*) as testvalue FROM " + cbosSchemaName + "." + 
         cbosTableName); 
       //this means there is no conditions to select on 
       System.out.println(cbosQueryResults); 
       cbosRs = cbosStmt.executeQuery("SELECT COUNT(*) as testvalue FROM " + cbosSchemaName + 
         "." + cbosTableName); 
      } 

      //below for TI 
      if(tiCondition == null) { 
       tiQueryResults = ("SELECT COUNT(*) as testvalue FROM " + tiSchemaName + "." + 
         tiTableName + "WHERE " 
         + tiCondition); 
       //sets the results to needed query 
       tiRs = cbosStmt.executeQuery("SELECT COUNT(*) as testvalue FROM " + tiSchemaName + "." + 
         tiTableName + "WHERE " 
         + tiCondition); 
      }else { 
       //sets the variable used later in printing of results 
       tiQueryResults = ("SELECT COUNT(*) as testvalue FROM " + tiSchemaName + "." + 
         tiTableName); 
       System.out.println(tiQueryResults); 
       //this means there is no conditions to select on 
       tiRs = cbosStmt.executeQuery("SELECT COUNT(*) FROM " + tiSchemaName + "." + 
         tiTableName); 
      } 



      /* 
      * Below gets the value of the users desktop 
      */ 
      String userHomeFolder = System.getProperty("user.home") + "/Desktop"; 
      //below creates a new file with given fileName 
      File resultsFile = new File(userHomeFolder, fileName + ".txt"); 

      /* 
      * Below creates a string for saving results from each data location query 
      */ 
      System.out.println(sourceRs); 
      String sTestValue = "stest"; 
      String cTestValue = "ctest"; 
      String tTestValue = "ttest"; 


      /* 
      * Below attempts to write out the gathered results to the created file 
      */ 
      try (BufferedWriter out = new BufferedWriter(new FileWriter(resultsFile))){ 

       //below writes a header to the SOURCE part query result 
       out.write("********* Below is the query results from SOURCE table ********" + "\n"); 
       System.out.println("sourceQueryResults = :" + sourceQueryResults); 

       out.write(sourceQueryResults + "\n"); 

       //below iterates through the returned results 
       while (sourceRs.next()) { 
        //this writes all of the results to the test file 
        sTestValue = sourceRs.getString(1); 
        System.out.println("writing Source count is: " + sTestValue); 
        out.write(sTestValue + "\n"); 
       } 
       sourceRs.close(); 

       //below writes a header to the CBOS part query result 
       out.write("********* Below is the query results from CBOS table ********" + "/n"); 
       out.write(cbosQueryResults + "/n"); 
       //below iterates through the returned results 
       while(cbosRs.next()) { 
        //this writes all of the CBOS results to test file 
        cTestValue = cbosRs.getString(1); 
        System.out.println("writing cbos count is: " + cTestValue); 
        out.write(cTestValue + "\n"); 
       } 

`    cbosRs.close(); 
       //below writes a header to TI part of the query result 
       out.write("********* Below is the query results from TI table ********" + "\n"); 
       out.write(tiQueryResults + "\n"); 
       //below iterates through the returned results 
       while(tiRs.next()) { 
        //this writes all of the CBOS results to test file 
        tTestValue = tiRs.getString(1); 
        System.out.println("writing TI count is: " + tTestValue); 
        out.write(tTestValue + "\n"); 
       } 
       tiRs.close(); 
       out.close(); 
      } 
     } 
     //below catches any errors for any of the above work 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      System.err.println(e.getMessage()); 
     } 
    } 
} 

錯誤發生時上線: while(cbosRs.next()) {

+1

您正在使用'cbosStmt'而不是'tiStmt'來創建'tiRs'。在同一個'Statement'上執行一個新的查詢將關閉以前的ResultSet。 – Andreas

+0

謝謝!我知道這是愚蠢的,但找不到它! – dgelinas21

回答

1

首先,你似乎被連接到只有2個數據庫。

Connection tiConn = DriverManager.getConnection(cbosConnectString, 
      db_userid, db_password); 

在這裏,您將再次連接到cbosConnectString,而不是tiConnectString。

其次,有一個在你的代碼中的任意符號:

`    cbosRs.close(); 

最後,嘗試在finally塊關閉每個結果集。

+0

雖然這些也是問題,但它們不是造成錯誤的原因。在粘貼代碼時,'\''可能是複製/粘貼錯誤。在兩個連接上使用相同的連接字符串是允許的,但可能不是預期的,所以它仍然是一個非常好的觀察。 – Andreas

+0

@Andreas就是這樣。但我很欣賞這些意見。 – dgelinas21

+1

第一點本身強調了連接再次使用相同的數據庫創建的主要問題,並且在這種情況下建議將明確解決問題。另外,我也強調了其他問題。 :) – GaurZilla