What JDBC string and driver should I use to connect to your database servers?

By default we offer MySQL and PostgreSQL and we recommend the following drivers:

  1. MySQL Connector/J driver connection string looks like “jdbc:mysql://[hostname]:[port]/[db_name]” You can find the driver on MySQL website at http://www.mysql.com/products/connector/j/ Example driver archive name: mysql-connector-java-5.1.5-bin.jar
  2. PostgreSQL driver connection string looks like “jdbc:postgresql://[hostname]:[port]/[db_name]” You can find the driver on PostgreSQL website at http://jdbc.postgresql.org/ Example driver archive name: postgresql-8.3-603.jdbc3.jarIf you use MS/SQL you may try:
  3. Open source MS/SQL driver connection string looks like “jdbc:jtds:://[hostname][:port][/db_name][;property=value[;…]]” This is open source JDBC 3.0 Type 4 driver for Microsoft SQL Server (6.5, 7.0, 2000 and 2005) and Sybase. You can find the driver at http://jtds.sourceforge.net/ Example driver archive name: jtds-1.2.2.jar
  4. Proprietary MS/SQL driver connection string looks like “jdbc:sqlserver://[hostname]:[port][/db_name][;property=value[;…]]” You can find the driver at Microsoft website at http://msdn.microsoft.com/en-us/data/aa937724.aspx Example driver archive name: sqljdbc.jar

Below you’ll find example java code for these 4 drivers. Respective JDBC strings and class names for the drivers mentioned above are marked with numeric comment.

import java.*;
public class Connect {
    private java.sql.Connection  con = null;
    private final String databaseName= "testdb";
    private final String userName = "testdbuser";
    private final String password = "secret";
    private final String url = "jdbc:mysql://localhost:3306/"+databaseName; // 1
//    private final String url = "jdbc:postgresql://localhost:5432/"+databaseName; // 2
//    private final String url = "jdbc:jtds:sqlserver://hostname:1433;databaseName="+databaseName; // 3
//    private final String url = "jdbc:sqlserver://hostname:1433;databaseName="+databaseName; // 4

    public Connect() { }

    private java.sql.Connection getConnection(){
	try {
    	    Class.forName("com.mysql.jdbc.Driver");              // 1
//            Class.forName("org.postgresql.Driver");                    // 2
//            Class.forName("net.sourceforge.jtds.jdbc.Driver");        // 3
//            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  // 4
    	    con = java.sql.DriverManager.getConnection(url,userName,password);
    	    if(con!=null) System.out.println("Connection Successful!");
	} catch(Exception e) {
    	    System.out.println("Error Trace in getConnection() : " + e.getMessage());
	return con;

    public void displayDbProperties(){
        java.sql.DatabaseMetaData dm = null;
        try {
            con= this.getConnection();
                dm = con.getMetaData();
                System.out.println("\tDriver Name: "+ dm.getDriverName());
                System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
            } else System.out.println("Error: No active Connection");
        } catch(Exception e) { e.printStackTrace(); }

    private void closeConnection(){
        try {
               if(con!=null) con.close();
        } catch(Exception e) { e.printStackTrace(); }

    public static void main(String[] args) throws Exception {
        Connect myDbTest = new Connect();

You can put the above code in Connect.java file and then complie it:

$ javac Connect.java

and run it with respective driver in classpath e.g. for MySQL:

$ java -cp .:mysql-connector-java-5.1.5-bin.jar Connect

This entry was posted in Databases, Java. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

four × = twenty eight

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>