This example show 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 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 = "root";
private static final String PASSWORD = "";
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
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.28/mysql-connector-java-8.0.28.jar -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
Latest posts by Wayan (see all)
- How do I convert Map to JSON and vice versa using Jackson? - June 12, 2022
- How do I find Java version? - March 21, 2022
- How do I convert CSV to JSON string using Jackson? - February 13, 2022