在该方法中readAll()我得到了NullPointerException我Connection,但为什么,我怎么可能改变这种状况?如果我用主类测试它一切正常,但如果我在服务器(网络服务器 - 本地)上做同样的事情,它会抛出一个NullPointerException.这是我的ConnectionManager课:public class ConnectionManager { private volatile static ConnectionManager connectionManager = null; private Connection connection = null; private ConnectionManager() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { DBProperties properties = DBProperties.getInstance(); String url = properties.getStringProperty("databaseURL") + properties.getStringProperty("pathToDB") + properties.getStringProperty("urlSettings"); String user = properties.getStringProperty("user"); String passwort = properties.getStringProperty("password"); try { // load driver Class.forName(properties.getStringProperty("dbDriver")).newInstance(); connection = DriverManager.getConnection(url, user, passwort); } catch (SQLException ex) { Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex); } } public synchronized static ConnectionManager getInstance() throws SQLException { if (connectionManager == null) { try { connectionManager = new ConnectionManager(); } catch (Exception ex) { Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex); } } return connectionManager; } public Connection getConnection() { return connection; } public static void closeConnection() { //close the connection only if there is a instance //--> closeConnection() can always be called. //(e.g. if non JDBC-Dao's are used --> ConnectionManager is not instantiated) if (connectionManager != null) { try { connectionManager.connection.close(); } catch (SQLExce
1 回答
千巷猫影
TA贡献1829条经验 获得超7个赞
很可能你有一个SQLException你只登录的:
try { // load driver
Class.forName(properties.getStringProperty("dbDriver")).newInstance();
connection = DriverManager.getConnection(url, user, passwort);
} catch (SQLException ex) {
Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
}
没有理由在SQLException这里处理。您没有在您的 中的任何地方实现重新连接逻辑ConnectionManager,如果您没有有效的连接,您的应用程序将无法工作。您应该重新抛出它并让您的应用程序退出:
} catch (SQLException ex) {
Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
throw ex;
}
添加回答
举报
0/150
提交
取消