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
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023