Configure JDBC Connection to SQL Server

Microsoft JDBC driver for SQL Server allows you to access a SQL Server database from Java applications.

JDBC Driver for SQL Server

You can download a Microsoft SQL Server JDBC Driver at http://msdn.microsoft.com/data/jdbc

The Microsoft JDBC Driver for SQL Server provides access to SQL Server 2008, 2005 and 2000.

Microsoft provides sqljdbc.jar and sqljdbc4.jar class library files to be used depending on Java Runtime Environment (JRE) settings.

  • sqljdbc4.jar

sqljdbc4.jar class library provides support for JDBC 4.0. and requires JRE 6.0 or later (using sqljdbc4.jar on 5.0 will throw an exception)

  • sqljdbc.jar

sqljdbc.jar class library provides support for JDBC 3.0 and requires JRE 5.0 (using sqljdbc.jar on JRE 6.0 will throw an exception)

Setting the Classpath

The JDBC driver is not part of the Java SDK, so you need to specify sqljdbc4.jar or sqljdbc.jar file (not both) in the classpath.

  • System CLASSPATH

On Windows, you can set the CLASSPATH environment variable as:

CLASSPATH = . ;<directory>\sqljdbc4.jar
  • Running Application in IDE

If you run application in IDE, setting the CLASSPATH environment variable will not work. You have to add sqljdbc.jar or sqljdbc4.jar to the IDE classpath.

Eclipse IDE: Right-click on your project root node, select Properties, go to Java Build Path, select Libraries tab, and Add External JARs.

  • Running Application in Command Line

Besides the CLASSPATH environment variable, you can alternatively specify the classpath in the java -classpath option.

Establishing a Connection

If you using sqljdbc.jar class library (JDBC 3.0), you must first load the driver:

   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

When you use sqljdbc4.jar (JDBC 4.0), applications do not need to call the Class.forName method to load the driver, it is loaded automatically by DriverManager.

Connection String

The general form of the connection URL for JDBC driver for SQL Server is

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

Examples

   // Connect to the TestDB database on the local computer by using a user name and password:
   "jdbc:sqlserver://localhost; databaseName=TestDB; user=UserName;password=Password;"
 
   // Connect to the TestDB database by using integrated authentication:
   "jdbc:sqlserver://localhost; databaseName=TestDB; integratedSecurity=true;"

Connection Code Example

   import java.sql.*;
 
   public class Test {
 
	public static void main(String[] args) {
 
           Connection con = null;
           String conUrl = "jdbc:sqlserver://localhost; databaseName=test; user=sa; password=sa;";
 
	   try {
            // ...
	    con = DriverManager.getConnection(conUrl);
            // ... 
   	  } catch (Exception e) { e.printStackTrace(); }
             finally {
               if (con != null) try { con.close(); } catch(Exception e) {}
             }
	}
   }

Migration Resources

You could leave a comment if you were logged in.