获取resultSet是关闭错误JDBC

时间:2017-06-18 16:21:30

标签: java jdbc

我使用以下代码连接到3个不同的数据库并对它们运行查询。但是当我从第二个查询中获取结果时,它给出了一个错误,说明resultSet已经关闭。

    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 个答案:

答案 0 :(得分:1)

首先,您似乎只连接到2个数据库。

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

在这里,您将再次连接到cbosConnectString,而不是tiConnectString。

其次,您的代码中有一些任意符号:

`               cbosRs.close();

最后,尝试关闭finally块中的每个resultSet。