我正在编写与数据库一起工作的应用程序。我有连接到mysql数据库的类:
public class DataBaseConnector {
private static final String URL = "jdbc:mysql://localhost:3306/dataBase";
private static final String USER = "user";
private static final String PASSWORD = "password";
private Connection connection;
private static DataBaseConnector instance;
public synchronized static DataBaseConnector getInstance() {
if (instance == null) {
instance = new DataBaseConnector();
}
return instance;
}
private DataBaseConnector() {
}
public Connection openConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
System.out.println("Database connected!");
this.connection = connection;
return connection;
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
}
}
执行SQL查询的测试方法:
public static void main(String[] args) {
try {
Connection connection = DataBaseConnector.getInstance().openConnection();
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM hotel_administrators WHERE login=?");
stmt.setString(1, "lll");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
我不明白为什么出现这样的例外:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
Database connected!
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1187)
at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1182)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4035)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4004)
at model.DataBaseManager.main(DataBaseManager.java:81)
怎么了?
答案 0 :(得分:2)
创建连接时,使用try-with-resources语句自动关闭新连接。
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
return connection;
}
/* this is automatically executed:
finally {
connection.close();
}
*/
现在,当您执行查询时,MySQL只会抱怨连接已经关闭。为了避免错误,你可以写:
try {
this.connection = DriverManager.getConnection(URL, USER, PASSWORD));
System.out.println("Database connected!");
return this.connection;
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}