Connecting JDBC applications with SSL

You can configure database connections for the to use the Secure Sockets Layer (SSL) protocol.

The client must use the same public key certificate file as the server.
  1. Use the keytool utility that comes with your Java™ runtime environment to import a client-side keystore database and add the public key certificate to the keystore.
    C:\work>keytool -importcert -file filename.extension -keystore .keystore

    Follow the prompts to enter a new keystore password and to trust the certificate.

  2. Configure an SSL connection to the database from your Java application by using the following operations:
    • Set the javax.net.ssl.truststore system property to point to the keystore that you created.
    • Set the javax.net.ssl.trustStorePassword system property to the password that you used for the certificate.
    • Get a data source object.
    • Set the port number to the SSL port, 9888.
    • Set the data source property setIfxSSLCONNECTION to true.

JDBC sample for SSL connection

This sample Java program highlights the operations that are required to connect to the stores_demo database by using SSL.

import java.sql.Connection;
import java.sql.SQLException;

import com.informix.jdbc.IfxDriver;
import com.informix.jdbcx.IfxConnectionPoolDataSource;


public class InformixSSLConnection {

    public static void main(String[] args) {
        Connection conn = null;
        try {

            /* System properties for keystore */
            System.setProperty("javax.net.ssl.trustStore", "/opt/ids/.keystore");
            System.setProperty("javax.net.ssl.trustStorePassword", "password");

            /* Instantiate Informix connection pooled data source */
            IfxConnectionPoolDataSource  cds =  new IfxConnectionPoolDataSource();

            /* Set SSLConnection property to true and port pointing to SSL port on the server */
            cds.setUser("dbuser");
            cds.setPassword("password");
            cds.setDatabaseName("stores_demo");
            cds.setPortNumber(9888);
            cds.setIfxSSLCONNECTION("true");

            conn = cds.getPooledConnection().getConnection();

            if(conn != null) {
                System.out.println(" Successfully connected to Informix database using SSL Connection");
                System.out.println(" Database version  ...: " +conn.getMetaData().getDatabaseProductVersion());
                System.out.println(" JDBC Driver Version .: " +IfxDriver.getJDBCVersion());
            }
        }
        catch (Exception e) {
            System.err.println("Error Message : " +e.getMessage());
            if(e instanceof SQLException)
                System.err.println("Error Code : " +((SQLException)e).getErrorCode());
            e.printStackTrace();
        }
        finally {
            if(conn != null)
                try {
                    conn.close();
                } catch (SQLException e) {}          
        }
    }
}

Copyright© 2020 HCL Technologies Limited