【问题标题】:JDBC Can't connect to my DB from a separate class. Class DB creates the connectionJDBC 无法从单独的类连接到我的数据库。 DB 类创建连接
【发布时间】:2014-11-18 05:18:09
【问题描述】:

我一直试图让我的 JDBC 数据库连接从它自己的单独类 (DB.java) 执行,这样每当我想从我的其他多个类中的任何一个(例如 CusTab.java)创建新连接时,我可以只创建一个数据库实例。在运行我的第一个测试(包括将表格内容打印到控制台)后,我发现自己遇到了一个错误:

Something wrong with prepared statement test
com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:388)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.prepareStatement(SQLServerConnection.java:2166)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.prepareStatement(SQLServerConnection.java:1853)
at marsPackage.CusTab.<init>(CusTab.java:129)

这是我的 DB.java 类,它开始连接到我的数据库 KIS_DB,它使用 Windows 身份验证,现在我的 GUI 在本地与数据库连接。

package marsPackage;

import java.sql.*;

public class DB {

private Connection dbConn = null;

public Connection getConnection(){
    //This condition will check if the connection is not already open
    if (null == dbConn){
        //Setup a connection to the database
        String url = "jdbc:sqlserver://CoT-CIS3365-1\\MSSQLSERVER;databaseName=KIS_DB;integratedSecurity=true"; 
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            System.err.println("Class.forName thing is not working here");
            e1.printStackTrace();
        }
        try{
            dbConn = DriverManager.getConnection(url);

            PreparedStatement useStmt;
            try{
                useStmt = dbConn.prepareStatement("USE KIS_DB");
                useStmt.executeUpdate();
            }
            catch(SQLException e){
                e.printStackTrace();
            }
        }
        catch(SQLException e){
            System.err.println("There was a problem connecting to the Database");
            e.printStackTrace();
        }
        finally{
            if (dbConn != null)
                try{dbConn.close();} catch(SQLException e){e.printStackTrace();}
        }
    }
    return dbConn;
}
}

这是我将一个简单的结果集打印到控制台的测试,它来自 CusTab.java 类,因为这个类中的代码很大,我将只输入我尝试使用我的数据库的 sn-p。

            DB db = new DB();
        try {
            String sql = "SELECT * FROM PROJECT_STATUS;";
            PreparedStatement Stmt = db.getConnection().prepareStatement(sql);
            ResultSet rs = Stmt.executeQuery();
        while(rs.next()){
                System.out.println("PRO_STATUS_ID: "+rs.getString(1));
                System.out.println("PRO_STATUS_NAME: "+rs.getString(2));    
        }
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            System.err.println("Something wrong with prepared statement test");
            e1.printStackTrace();
        }

提前感谢您抽出宝贵时间。

更新:有了@ppuskar 的建议,我能够解决这个问题。我删除了 DB 类中的 finally 块,因为它过早地关闭了我的连接。然后我在DB.java中添加了一个close()方法,这个方法在使用的时候会关闭连接。

/*
        finally{
            if (dbConn != null)
                try{dbConn.close();} catch(SQLException e){e.printStackTrace();}
        }*/
    }
    return dbConn;
}

public void close(){
    try {
        dbConn.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        System.err.println("Could not close the connection");
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java sql-server database jdbc prepared-statement


    【解决方案1】:

    查看您的 getConnection() API,您正在关闭 finally 块中的连接。顺便说一句,我不明白你为什么在 getConnection() 中执行 useStmt.executeUpdate();

    【讨论】:

    • 我正在重用在这里找到的解决方案stackoverflow.com/questions/8479924/… 我认为他在 finally 块中关闭连接的原因是连接在使用完成后会关闭。然而,我已经删除了 finally 块和 useStmt.executeUpdate();你是对的,这行代码毫无意义。我没有使用 finally 块,而是创建了一个在调用时关闭连接的新方法。我将使用解决问题的更改来更新我的问题。
    猜你喜欢
    • 1970-01-01
    • 2014-07-21
    • 2021-06-06
    • 2014-12-09
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2020-11-24
    • 2021-06-27
    相关资源
    最近更新 更多