我在Java中有一个JDBC MySQL连接.我的程序适用于简单的查询执行.
如果我运行相同的程序超过10个小时并执行查询,那么我收到以下MySQL异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Connection.close() has already been called. Invalid operation in
this state.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(
Native Method)
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
No operations allowed after statement closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(
Native Method)
Run Code Online (Sandbox Code Playgroud)
我没有close()在任何地方使用过方法.我创建了数据库连接并永远打开它并始终执行查询.我没有明确提到连接超时的地方.我无法确定问题所在.
这是我用于数据库连接的代码:
String driver = PropertyReader.getDriver();
String url = dbURLPath;
Class.forName(driver);
connectToServerDB = DriverManager.getConnection(url);
connectToServerDB.setAutoCommit(false);
Run Code Online (Sandbox Code Playgroud)
是什么导致了这种例外
我正在尝试连接到我在Java程序中使用MySQL创建的数据库,但它总是失败.
为了举例,这是我的代码:
import java.sql.*;
public class Squirrel {
public static void main(String[] args) {
String user;
String password;
Connection connection;
Statement statement;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306", user, password);
statement = connection.createStatement();
// Other code
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我能够从IntelliJ中连接到数据库并添加mysql-connector-java-5.1.40.jar添加到项目中,但每次运行程序时都会DriverManager.getConnection()抛出:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: …Run Code Online (Sandbox Code Playgroud)