This example shows you how to get the maximum number of concurrent connections to a database that are possible.
To get this information, we use the DatabaseMetaData.getMaxConnections()
method call. If the return value is zero, it means that there is no limit or the limit is unknown.
package org.kodejava.jdbc;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.DriverManager;
public class MaxConnections {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "kodejava";
private static final String PASSWORD = "s3cr*t";
public static void main(String[] args) {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
// Get database meta data.
DatabaseMetaData metaData = connection.getMetaData();
// Retrieves the maximum number of concurrent
// connections to this database that are possible.
// A result of zero means that there is no limit or
// the limit is not known.
int max = metaData.getMaxConnections();
System.out.println("Max concurrent connections: " + max);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Maven Dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>