Published on Sep 14 2011 in Databases Java

By default we offer MySQL and PostgreSQL and we recommend the following driver settings. For MongoDB driver settings see our Articles section.

If you use MS/SQL you may try the below drivers.

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) {
            e.printStackTrace();
            System.out.println("Error Trace in getConnection() : " + e.getMessage());
        }
        return con;
    }
    
    public void displayDbProperties(){
    java.sql.DatabaseMetaData dm = null;
       try {
            con= this.getConnection();
            if(con!=null){
                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());
                closeConnection();
            } else System.out.println("Error: No active Connection");
        } catch(Exception e) { e.printStackTrace(); }
        dm=null;
    }

    private void closeConnection(){
        try {
               if(con!=null) con.close();
               con=null;
        } catch(Exception e) { e.printStackTrace(); }
    }
 
    public static void main(String[] args) throws Exception {
        Connect myDbTest = new Connect();
        myDbTest.displayDbProperties();
    }
}

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