title: Casting JDBC Connection
body: <p>I'm working on a new module for a project which should connect to a SQL database using a JDBC driver. I am required to implement a Connection Interface with method <code>getConnection()</code> which is of the return type <code>org.forgerock.opendj.ldap.Connection</code>. However, the JDBC driver returns a connection of the type <code>com.mysql.jdbc.JDBC4Connection</code>. Casting gives me the following error: </p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: 
      com.mysql.jdbc.JDBC4Connection cannot be cast to org.forgerock.opendj.ldap.Connection
      at org.forgerock.opendj.virtual.JDBCConnectionFactory.getConnection(JDBCConnectionFactory.java:105)
  </code></pre>
  
  <p>What would be the best way to obtain a connection of the type <code>org.forgerock.opendj.ldap.Connection</code> instead of the <code>com.mysql.jdbc.JDBC4Connection</code>?</p>
  
  <p>The JDBCConnectionFactory class:</p>
  
  <pre><code>public class JDBCConnectionFactory implements ConnectionFactory{
      private final String driverName = "com.mysql.jdbc.Driver";
      private Connection con = null;
      private String ConnectionUrl = "";
      private final String Host;
      private final int Port;
      private final String DbName;
      private final String UserName;
      private final String UserPass;
  
  public JDBCConnectionFactory(final String host, final int port, final String dbName, final String userName, final String userPass) {
          this.Host = host;
          this.Port = port;
          this.DbName = dbName;
          this.UserName = userName;
          this.UserPass = userPass;
          this.ConnectionUrl="jdbc:mysql://"
                  .concat(this.Host+":")
                  .concat(this.Port+"/")
                  .concat(this.DbName);
  
          try {
                  Class.forName(driverName);
              } catch (ClassNotFoundException e) {
                  System.out.println(e.toString());
              }
      }
  </code></pre>
  
  <p>The getConnection method: </p>
  
  <pre><code>@Override
      public org.forgerock.opendj.ldap.Connection getConnection()throws ErrorResultException {
          try {
  
              con = DriverManager
                              .getConnection(this.ConnectionUrl,this.UserName,this.UserPass);
  
              org.forgerock.opendj.ldap.Connection newcon = (org.forgerock.opendj.ldap.Connection) con;
              System.out.println("Connection created.");
              return newcon;
              } catch (SQLException e) {
              System.out.println(e.toString());
              return null;
              }
  
  
      }
  </code></pre>