A Java DB2 JDBC Driver class and URL example

Here's a quick post to help anyone that needs a quick Driver and URL reference when using DB2 with Java and JDBC.

If all you need is a reference to the DB2 JDBC Driver and URL connection string syntax, this basic information should work:

DB2 URL (JDBC Connection) String:
jdbc:as400://HOST/DATABASE;

DB2 JDBC Driver String: 
com.ibm.as400.access.AS400JDBCDriver

I have used many more parameters when connecting to a DB2 database using Java, JDBC, and Spring, but for a simple Java to DB2 database connection, I think that DB2 Driver and URL information is valid.

A DB2 JDBC Driver and URL database connection example

It may also help to see this used in a simple Java JDBC application. To that end, here's a simple Java JDBC DB2 example that shows how to use the DB2 Driver and URL to establish a database connection.

public class JdbcDb2DriverUrlExample
{
  public static void main(String[] args)
  {
    Connection connection = null;
    try
    {
      // the db2 driver string
      Class.forName("com.ibm.as400.access.AS400JDBCDriver");
    
      // the db2 url string
      String url = "jdbc:as400://HOST/DATABASE;";
      
      // get a db2 database connection
      connection = DriverManager.getConnection(url,"THE_USER", "THE_PASSWORD");
      
      // now do whatever you want to do with the connection
      // ...
      
    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      System.exit(2);
    }
  }
}

I hope this simple Java/JDBC/DB2 reference is helpful. There are also many other Java, JDBC, and DB2 tutorials on this site. Just use our search form to find many other examples.