Configuring and Managing Connection Properties

Connection properties are settings used to control how a Java application connects to and communicates with a database. In JDBC, these properties can be provided through the JDBC URL, a Properties object, or a DataSource. They can specify information such as the database username, password, connection timeout, SSL usage, character encoding, and auto-commit behavior. Properly configuring these properties helps improve security, performance, reliability, and connection management. Connection properties can vary depending on the JDBC driver and database being used.

Important Connection Properties

Connection Properties

user

Specifies the username used to connect to the database.

password

Specifies the password used to authenticate the database connection.

autoCommit

Specifies whether database operations are automatically committed.

readOnly

Specifies whether the connection should operate in read-only mode.

connectTimeout

Specifies the maximum time allowed for establishing a database connection.

socketTimeout

Specifies the maximum time to wait for network socket operations.

useSSL

Specifies whether SSL/TLS should be used for communication with the database.

serverTimezone

Specifies the time zone used for communication between the application and database server.

characterEncoding

Specifies the character encoding used for communication with the database.

useUnicode

Specifies whether Unicode character support should be enabled.

allowPublicKeyRetrieval

Allows the JDBC driver to retrieve the server’s public key when required by the authentication method.

transactionIsolation

Specifies the transaction isolation level for the database connection.

loginTimeout

Specifies the maximum time allowed for establishing a database connection.

sslMode

Specifies the SSL/TLS connection mode used by the MySQL JDBC driver.

databaseName

Specifies the name of the database or schema to connect to when supported by the driver.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class ConnectionPropertiesExample {

    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306/jdbc_demo";

        // Creating connection properties
        Properties properties = new Properties();

        properties.setProperty("user", "root");
        properties.setProperty("password", "password");
        properties.setProperty("useSSL", "false");
        properties.setProperty("serverTimezone", "UTC");
        properties.setProperty("characterEncoding", "UTF-8");

        try {
            // Establishing connection using properties
            Connection connection =
                    DriverManager.getConnection(url, properties);

            System.out.println("Database connected successfully!");

            // Creating Statement
            Statement statement = connection.createStatement();

            // Executing query
            ResultSet resultSet =
                    statement.executeQuery("SELECT * FROM employee");

            System.out.println("\nEmployee Details:");

            while (resultSet.next()) {
                System.out.println(
                        resultSet.getInt("employee_id") + " | "
                        + resultSet.getString("employee_name") + " | "
                        + resultSet.getString("department") + " | "
                        + resultSet.getDouble("salary")
                );
            }

            // Closing resources
            resultSet.close();
            statement.close();
            connection.close();

            System.out.println("\nConnection closed successfully!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" ConnectionPropertiesExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" ConnectionPropertiesExample
Database connected successfully!

Employee Details:
101 | Ravi | IT | 45000.0
102 | Priya | HR | 42000.0
103 | Arjun | Finance | 50000.0

Connection closed successfully!Code language: JavaScript (javascript)
Scroll to Top