Search code examples
javassldb2ibm-cloud

SSL DB2 connection failed


I am trying to connect to a DB2 database using SSL on IBM Bluemix.

When I first tried to connect without SSL, it doesn't work. After reading the documentation, I have realized that it connects to the database with SSL enabled.

I tried using the following code to get it connect to the database:

public boolean connect() {
  try {
    String url = "jdbc:db2://" + serverName + ":" + port + "/" + dbName+
                 ":securityMechanism=9";    

    connection = DriverManager.getConnection(url, userName, passWord);
    st = connection.createStatement();
    return true;
  } catch (Exception e) {
    System.err.println(e.getMessage());
  }

  return false;
}

Still I am not too sure on how to use the SSL certificate provided with the code above.

I tried searching for examples but most of the explanations are either unclear or used for another database system.


Solution

  • If you are using Liberty, a datasource is generated for you, and you can look it up using jndi.

    @Resource(lookup = "jdbc/mydb")
    private DataSource myDataSource;
    Connection c = myDataSource.getConnection();
    

    "mydb" is the name of the SQLDB service

    https://developer.ibm.com/bluemix/2014/02/07/java-db2-10-minutes/