Java 错误:数据源拒绝建立连接,来自服务器的消息:";“连接太多”;
我有一个文本文件,它的大小不是很重(600KB),我的代码应该读取该文件并将整个包含内容发送到我的数据库:Java 错误:数据源拒绝建立连接,来自服务器的消息:";“连接太多”;,java,mysql,jdbc,mysql-error-1064,Java,Mysql,Jdbc,Mysql Error 1064,我有一个文本文件,它的大小不是很重(600KB),我的代码应该读取该文件并将整个包含内容发送到我的数据库: private static void readBigFileAndSendToDB() throws Exception { Scanner s = new Scanner(new FileReader("tf1.txt")); while (s.hasNextLine()) { String eachLine = s.nextLine();
private static void readBigFileAndSendToDB() throws Exception {
Scanner s = new Scanner(new FileReader("tf1.txt"));
while (s.hasNextLine()) {
String eachLine = s.nextLine(); // each Line
sendBigFileToDB(eachLine);
} // (end of file).
System.out.println("Sent big file to DB");
s.close();
}
private static void sendBigFileToDB(String line) {
Connection con = null;
PreparedStatement ps = null;
String query = "Insert into BigFile(text) values ('" + line + "')";
try {
if (line != null) {
con = DriverManager.getConnection(...);
ps = con.prepareStatement(query);
ps.execute();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
但当我运行程序时,出现了以下异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
请在完成流程后关闭语句并关闭连接:
private static void readBigFileAndSendToDB() throws Exception {
Scanner s = new Scanner(new FileReader("tf1.txt"));
while (s.hasNextLine()) {
String eachLine = s.nextLine(); // each Line
sendBigFileToDB(eachLine);
} // (end of file).
System.out.println("Sent big file to DB");
s.close();
}
private static void sendBigFileToDB(String line) {
PreparedStatement ps = null;
String query = "Insert into BigFile(text) values ('" + line + "')";
try {
if (line != null) {
con = DriverManager.getConnection(...);
ps = con.prepareStatement(query);
ps.execute();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
finally {
if (ps != null) try { ps .close(); } catch (SQLException logOrIgnore) {}
if (con != null) try { con .close(); } catch (SQLException logOrIgnore) {}
}
}
当您获得连接时,还需要关闭它。不关闭连接会导致资源泄漏,这可能最终导致服务器拒绝连接,因为已达到最大连接数 我建议您更改代码,以便在资源的使用范围结束时自动关闭资源:
if (line == null) return;
String query = "Insert into BigFile(text) values (?)";
try (
Connection con = con = DriverManager.getConnection(...);
PreparedStatement ps = con.prepareStatement(query);
){
ps.setString(1, line);
ps.executeUpdate();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
请注意,我还将行
连接到查询中替换为正确使用prepared语句参数
通过将连接创建和语句准备移到readBigFileAndSendToDB
中,您的代码可以得到进一步的改进,这样只需执行一次,并且不会在每次迭代时产生开销:
String query = "Insert into BigFile(text) values (?)";
try (
Scanner s = new Scanner(new FileReader("tf1.txt"));
Connection con = con = DriverManager.getConnection(...);
PreparedStatement ps = con.prepareStatement(query);
){
while (s.hasNextLine()) {
String line = s.nextLine();
if (line == null) continue;
ps.setString(1, line);
ps.addBatch();
}
ps.executeBatch();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
尝试关闭连接,一旦完成,我建议尝试使用资源,使用
executeUpdate()
替换executeBatch()
关闭资源要简单得多。我是否应该将resultset对象初始化语句
移到Try()
?(这不是问题)@user3808021是的,你应该这样做,这将确保它在try块结束后关闭。