Java中如何获得与数据库的最大并发连接?
package org.nhooo.example.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.DatabaseMetaData; import java.sql.SQLException; public class MaxConcurrentConnection { private static final String URL = "jdbc:mysql://localhost/nhooo"; private static final String USERNAME = "root"; private static final String PASSWORD = ""; public static void main(String[] args) { try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD)) { DatabaseMetaData metadata = connection.getMetaData(); //获取最大并发连接数的信息 //数据库。可能的最大活动连接数 //一次零的结果表示没有限制 //或限制未知。 int maxConnection = metadata.getMaxConnections(); System.out.println("Maximum Connection = " + maxConnection); } catch (SQLException e) { e.printStackTrace(); } } }
上面的代码片段的结果:
Maximum Connection = 0
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency>