测试 mysql 的最大连接数。
blog.csdn.net/shiyangxt/article/details/83264544 --- 原文链接
如果直接用 navicat 查询 mysql 的连接数,查询语句是 show status like 'Threads%';
其中的 Threads_connected 就是 mysql 的连接数
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
// 测试 mysql 的最大连接数
public class JdbcMaxTest{
// main
public static void main(String[] a){
int count = 1;
int max = 5000;
Connection[] conn = new Connection[max];
Statement[] stmt = new Statement[max];
ResultSet[] rs = new ResultSet[max];
String dbUrl = "jdbc:mysql://localhost:3306/testDB1";
String name = "admin";
String pwd = "123";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
for (count = 1; count < max; count++){
conn[count] = DriverManager.getConnection(dbUrl, name, pwd);
stmt[count] = conn[count].createStatement();
rs[count] = stmt[count].executeQuery("SELECT * FROM user");
while (rs[count].next()){
//System.out.println(rs.getString(1) + " t" + rs.getString(2));
}
System.out.print(count + "t ");
if(count % 20 == 0 && count > 0){
System.out.print("\n");
}
}
}catch(Exception ex){
System.out.println("\n" + ex.toString());
//System.out.println(count);
} finally {
try {
System.out.println("\n" + "System has opened " + --count
+ " Mysql connections.");
System.out.println("Press Enter key to close them.");
System.in.read();
System.out.println("Close these Connections:");
for(; count >= 0; count--){
if(rs[count] != null){
rs[count].close();
}
if(rs[count] != null){
rs[count].close();
}
if(conn[count] != null){
conn[count].close();
System.out.print(count + "t ") ;
if(count % 20 == 0 && count > 0){
System.out.print("\n");
}
}
}
}catch(Exception ex){
System.out.println("\n" + "Close connection exception: " + ex.toString());
}
}
}
}