2017-06-18 76 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