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>
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023