Learning the JDBC Driver

What is the JDBC driver?

The Microsoft SQL Server 2005 JDBC Driver is a Java Database Connectivity (JDBC) 3.0 compliant driver that provides robust data access to SQL Server 2000 and SQL Server 2005 databases. The JDBC driver can access many of the new features of SQL Server 2005, including XML, user-defined, and large value data types; and it supports the new snapshot isolation cursor type. For more information about the data types supported by the JDBC driver, see Understanding the JDBC Driver Data Types.

What is required in order to use the JDBC driver?

To access data from a SQL Server database by using the Microsoft SQL Server 2005 JDBC Driver, you must have the following components installed on your computer:

  • Microsoft SQL Server 2005 JDBC Driver
  • Java Runtime Environment

JDBC 3.0 features require a Java Runtime Environment (JRE) of version 1.4 or later; therefore, the JDBC driver also requires a JRE of version 1.4 or later. The JDBC driver is designed to work within all major virtual machines, but is officially supported on the following:

  • Sun JRE 1.4 or later
  • IBM JRE 1.4 or later

In addition, the JDBC driver is designed to work on any operating system that supports the use of a Java Virtual Machine (JVM). However, only Sun Solaris, Red Hat Linux, and Windows 2000 or later operating systems are officially supported.

How do I use the JDBC driver?

The JDBC driver is not part of the Java SDK, so you must set the classpath to include the sqljdbc.jar file if you want to use it. If the classpath is missing an entry for sqljdbc.jar, your application will throw the common "Class not found" exception.

The sqljdbc.jar file is installed in the following location:

<installation directory>\sqljdbc_<version>\<language>\sqljdbc.jar

The following is an example of the CLASSPATH statement used for a Windows application:

CLASSPATH =.;C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.1\enu\sqljdbc.jar

The following is an example of the CLASSPATH statement used for a Unix/Linux application:

CLASSPATH =.;/home/usr1/mssqlserver2005jdbc/Driver/sqljdbc_1.1/enu/sqljdbc.jar

To connect to a database by using the DriverManager class, you must first register the driver as in the following:

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

When the driver is loaded, you can establish a connection by using a connection URL:

Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;user=MyUserName;password=*****");

For more information about connecting with data sources and using a connection URL, see Building the Connection URL and Setting the Connection Properties.

How do I deploy the JDBC driver?

When deploying an application which is dependent on the JDBC driver, you will need to redistribute the JDBC driver with your application. Unlike Microsoft Data Access Components (MDAC), which is now a component of the operating system, the JDBC driver is a component of SQL Server 2005. Therefore, it is important to install the JDBC driver in your development environment and redistribute the JDBC driver with your application. The JDBC driver redistributable installation program is available online at the JDBC reference page. To register to redistribute the JDBC driver, see Register for Microsoft SQL Server 2005 JDBC Driver Redistribution Rights.

Topic Areas

  • What is the JDBC driver?
  • What is required in order to use the JDBC driver?
  • How do I use the JDBC driver?
  • How do I deploy the JDBC driver?