Fixing SQL Server JDBC Connection SSL Certificate Errors

When connecting to a SQL Server database from a Java application using JDBC, you might encounter an SSL/TLS handshake failure. This typically manifests as an error indicating the driver cannot establish a secure connection.

The root cause is often that the SQL Server's SSL certificate is not trusted by the Java Virtual Machine (JVM). This can happen if the server uses a self-signed certificate or a certificate from a Certificate Authority (CA) not present in the JVM's default trust store.

The simplest solution is to instruct the JDBC driver to skip certificate vlaidation by adding the trustServerCertificate=true parameter to the connection string. This bypasses the security check and allows the connection to proceed, though it should be used with caution in production environments.

Here is an example of a Java program that successfully connects to a SQL Server database by including this parameter:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DatabaseConnector {
    private static Connection connection = null;

    public static void main(String[] args) {
        // Define the connection string with the trustServerCertificate parameter
        String connectionString = "jdbc:sqlserver://localhost:1433;database=mydatabase;trustServerCertificate=true";

        try {
            // Load the SQL Server JDBC driver
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("JDBC driver loaded successfully.");

            // Establish the database connection
            connection = DriverManager.getConnection(connectionString, "admin", "password123");
            System.out.println("Database connection established successfully.");

            // Prepare and execute a simple query
            String sqlQuery = "SELECT ProductName FROM Products";
            PreparedStatement statement = connection.prepareStatement(sqlQuery);
            ResultSet resultSet = statement.executeQuery();

            // Process the results
            while (resultSet.next()) {
                String productName = resultSet.getString("ProductName");
                System.out.println(productName);
            }

        } catch (ClassNotFoundException e) {
            System.err.println("JDBC driver not found.");
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("Database connection failed.");
            e.printStackTrace();
        } finally {
            // Close the connection in the finally block
            if (connection != null) {
                try {
                    connection.close();
                    System.out.println("Connection closed.");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

This aproach resolves the SSL certificate validation issue, allowing your Java application to connect to the SQL Server instance.

Tags: java JDBC SQL Server ssl connection-string

Posted on Thu, 18 Jun 2026 18:22:21 +0000 by nerya