This is how you define the entry for the Scala class path

Using the JDBC Driver

  • 8 minutes to read

Download the JDBC driver

This section provides instructions on how to quickly connect to a SQL Server database using the Microsoft JDBC Driver for SQL Server. Before connecting to a SQL Server database, SQL Server must be installed on the local computer or a server and the JDBC driver must be installed on the local computer.

Choosing the correct JAR file

The Microsoft JDBC driver provides several JAR files that can be used in conjunction with your preferred settings for the Java Runtime Environment (JRE):

The Microsoft JDBC Driver 9.2 for SQL Server provides the class library files mssql-jdbc-9.2.1.jre8.jar, mssql-jdbc-9.2.1.jre11.jar and mssql-jdbc-9.2.1.jre15.jar ready.

The Microsoft JDBC Driver 8.4 for SQL Server provides the class library files mssql-jdbc-8.4.1.jre8.jar, mssql-jdbc-8.4.1.jre11.jar and mssql-jdbc-8.4.1.jre14.jar ready.

The Microsoft JDBC Driver 8.2 for SQL Server provides the class library files mssql-jdbc-8.2.2.jre8.jar, mssql-jdbc-8.2.2.jre11.jar and mssql-jdbc-8.2.2.jre13.jar.

The Microsoft JDBC Driver 7.4 for SQL Server provides the class library files mssql-jdbc-7.4.1.jre8.jar, mssql-jdbc-7.4.1.jre11.jar and mssql-jdbc-7.4.1.jre12.jar.

The Microsoft JDBC Driver 7.2 for SQL Server provides the class library files mssql-jdbc-7.2.2.jre8.jar and mssql-jdbc-7.2.2.jre11.jar.

The Microsoft JDBC Driver 7.0 for SQL Server provides the class library files mssql-jdbc-7.0.0.jre8.jar and mssql-jdbc-7.0.0.jre10.jar.

The Microsoft JDBC Driver 6.4 for SQL Server provides the class library files mssql-jdbc-6.4.0.jre7.jar, mssql-jdbc-6.4.0.jre8.jar and mssql-jdbc-6.4.0.jre9.jar.

The Microsoft JDBC Driver 6.2 for SQL Server provides the class library files mssql-jdbc-6.2.2.jre7.jar and mssql-jdbc-6.2.2.jre8.jar.

The Microsoft JDBC Drivers 6.0 and 4.2 for SQL Server provide the class library files sqljdbc41.jar and sqljdbc42.jar.

The Microsoft JDBC Driver 4.1 for SQL Server provides the class library file sqljdbc41.jar.

Your selection also determines the functions available. For more information about choosing the correct JAR file, see JDBC Driver System Requirements.

Set the class path

The Microsoft JDBC driver JAR files are not part of the Java SDK and must be included in the user application's classpath.

If you are using JDBC Driver 4.1 or 4.2, set the classpath to include the sqljdbc41.jar or sqljdbc42.jar from the corresponding driver download.

If you are using JDBC Driver 6.2, set the classpath to file the mssql-jdbc-6.2.2.jre7.jar or mssql-jdbc-6.2.2.jre8.jar contains.

If you are using the JDBC 6.4 driver, set the classpath to include the mssql-jdbc-6.4.0.jre7.jar, mssql-jdbc-6.4.0.jre8.jar or mssql-jdbc-6.4.0.jre9.jar contains.

If you are using JDBC Driver 7.0, set the classpath to the file mssql-jdbc-7.0.0.jre8.jar or mssql-jdbc-7.0.0.jre10.jar contains.

If you are using JDBC Driver 7.2, set the classpath to file the mssql-jdbc-7.2.2.jre8.jar or mssql-jdbc-7.2.2.jre11.jar contains.

If you are using the 7.4 JDBC driver, set the classpath to file the mssql-jdbc-7.4.1.jre8.jar, mssql-jdbc-7.4.1.jre11.jar or mssql-jdbc-7.4.1.jre12.jar contains.

If you are using the 8.2 JDBC driver, set the classpath to file the mssql-jdbc-8.2.2.jre8.jar, mssql-jdbc-8.2.2.jre11.jar or mssql-jdbc-8.2.2.jre13.jar contains.

If you are using the 8.4 JDBC driver, set the classpath to include the mssql-jdbc-8.4.1.jre8.jar, mssql-jdbc-8.4.1.jre11.jar or mssql-jdbc-8.4.1.jre14.jar contains.

If you are using the 9.2 JDBC driver, take the file mssql-jdbc-9.2.1.jre8.jar, mssql-jdbc-9.2.1.jre11.jar or mssql-jdbc-9.2.1.jre15.jar in the class path.

If there is no entry in the classpath for the correct JAR file, an application throws the general exception.

For the Microsoft JDBC driver 9.2

The files mssql-jdbc-9.2.1.jre8.jar, mssql-jdbc-9.2.1.jre11.jar or mssql-jdbc-9.2.1.jre15.jar are installed in the following locations:

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement only contains a Microsoft JDBC Driver for SQL Server file, either mssql-jdbc-9.2.1.jre8.jar, mssql-jdbc-9.2.1.jre11.jar or mssql-jdbc-9.2.1.jre15.jar.

For Microsoft JDBC driver 8.4

The files mssql-jdbc-8.4.1.jre8.jar, mssql-jdbc-8.4.1.jre11.jar or mssql-jdbc-8.4.1.jre14.jar are installed in the following directories:

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement only contains a Microsoft JDBC Driver for SQL Server file, either mssql-jdbc-8.4.1.jre8.jar, mssql-jdbc-8.4.1.jre11.jar or mssql-jdbc-8.4.1.jre14.jar.

For Microsoft JDBC driver 8.2

The files mssql-jdbc-8.2.2.jre8.jar, mssql-jdbc-8.2.2.jre11.jar and mssql-jdbc-8.2.2.jre13.jar are installed in the following directories:

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server file, e.g. B. either mssql-jdbc-8.2.2.jre8.jar, mssql-jdbc-8.2.2.jre11.jar or mssql-jdbc-8.2.2.jre13.jar.

For Microsoft JDBC driver 7.4

The file mssql-jdbc-7.4.1.jre8.jar, mssql-jdbc-7.4.1.jre11.jar or mssql-jdbc-7.4.1.jre12.jar is installed in the following directory:

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement only contains a Microsoft JDBC driver for SQL Server file, e.g. either mssql-jdbc-7.4.1.jre8.jar, mssql-jdbc-7.4.1.jre11.jar or mssql-jdbc-7.4.1.jre12.jar.

For Microsoft JDBC Driver 7.2

The files mssql-jdbc-7.2.2.jre8.jar and mssql-jdbc-7.2.2.jre11.jar are installed in the following directories:

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server file, e.g. B. either mssql-jdbc-7.2.2.jre8.jar or mssql-jdbc-7.2.2.jre11.jar.

For Microsoft JDBC Driver 7.0

The files mssql-jdbc-7.0.0.jre8.jar and mssql-jdbc-7.0.0.jre10.jar are installed in the following directories:

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server file, e.g. B. either mssql-jdbc-7.0.0.jre8.jar or mssql-jdbc-7.0.0.jre10.jar.

For Microsoft JDBC Driver 6.4

The file mssql-jdbc-6.4.0.jre7.jar, "** mssql-jdbc-6.4.0.jre8.jar" or mssql-jdbc-6.4.0.jre9.jar is installed in the following directory:

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server file, e.g. B. either mssql-jdbc-6.4.0.jre7.jar, "** mssql-jdbc-6.4.0.jre8.jar" or mssql-jdbc-6.4.0.jre9.jar.

For Microsoft JDBC Driver 6.2

The files mssql-jdbc-6.2.2.jre7.jar and mssql-jdbc-6.2.2.jre8.jar are installed in the following directories:

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server file, e.g. E.g. either "mssql-jdbc-6.2.2.jre7.jar" or "mssql-jdbc-6.2.2.jre8.jar".

For Microsoft JDBC drivers 4.1, 4.2 and 6.0

The JAR file sqljdbc.jar file, sqljdbc4.jar file, sqljdbc41.jar, or sqljdbc42.jar is installed in the following location.

The following code snippet is an example of the CLASSPATH statement used with a Windows application:

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

Make sure that the CLASSPATH statement only contains a Microsoft JDBC driver for SQL Server file, e.g. "sqljdbc4.jar", "sqljdbc41.jar" or "sqljdbc42.jar".

Note

On Windows systems, directory names longer than the 8.3 naming convention or folder names with spaces can cause class path problems. If you suspect such a problem, temporarily move the sqljdbc.jar, sqljdbc4.jar, or sqljdbc41.jar file to a directory with a simple name such as, change the classpath, and see if that solves the problem has been.

Applications that run directly from the command prompt

The class path is configured in the operating system. Append "sqljdbc.jar", "sqljdbc4.jar", or "sqljdbc41.jar" to the system classpath. Alternatively, you can use the option to specify the classpath on the Java command line used to run the application.

Applications that run in an IDE

Each IDE manufacturer uses a different method to set the classpath in the IDE. It is not enough to just set the class path in the operating system. You must append sqljdbc.jar, sqljdbc4.jar, or sqljdbc41.jar to the IDE classpath.

Servlets and JSPs

Servlets and JSPs run in a servlet / JSP engine like Tomcat. The class path must be set as specified in the documentation for the servlet / JSP engine. It is not enough to just set the class path in the operating system. Some servlet / JSP engines have setup windows that allow you to set the engine's classpath. In this situation, you need to append the correct JAR file for the JDBC driver to the engine's existing classpath and restart the engine. In other situations, you can deploy the driver by copying sqljdbc.jar, sqljdbc4.jar, or sqljdbc41.jar to a specific directory during the engine installation; B. "lib". The class path for the engine driver can also be specified in an engine-specific configuration file.

Enterprise Java Beans

Enterprise Java Beans (EJB) run in an EJB container. EJB containers are available from various manufacturers. Java applets run in a browser but are downloaded from a web server. Copy “sqljdbc.jar” “sqljdbc4.jar” or “sqljdbc41.jar” to the root directory of the web server and enter the name of the JAR file in the HTML archive tab of the applet, e.g..

Establish a simple connection to a database

If you are using the sqljdbc.jar class library, you must first register the driver as follows:

After the driver loads, you can connect using a connection url and the getConnection method of the DriverManager class:

As of JDBC API 4.0, the method has been extended so that JDBC drivers are loaded automatically. Therefore, when using JAR driver libraries, applications do not need to call the method to register or load the driver.

When the getConnection method of the DriverManager class is called, an appropriate driver is selected from the group of registered JDBC drivers. "Sqljdbc4.jar", "sqljdbc41.jar" and "sqljdbc42.jar" contain the file "META-INF / services / java.sql.Driver", which the com.microsoft.sqlserver.jdbc.SQLServerDriver as a registered driver. Existing applications that are currently loading the drivers using the Class.forName method will continue to work without changes.

Note

The sqljdbc4.jar, sqljdbc41.jar, or sqljdbc42.jar class library cannot be used with older versions of the Java Runtime Environment (JRE). For the list of JRE versions supported by the Microsoft JDBC Driver for SQL Server, see JDBC Driver System Requirements.

For more information about connecting to data sources and using a connection URL, see Create the Connection URL and Set Connection Properties.

additional Information

Overview of the JDBC driver