Using the JDBC driver

Source: Internet
Author: User

This section provides quick start instructions for establishing a simple connection to a SQL Server database using Microsoft JDBC Driver for SQL Server. Before you connect to a SQL Server database, you must first install SQL Server on the local computer or server, and you must install the JDBC driver on the local computer.

Select the correct JAR file

Microsoft JDBC Driver 4.1 for SQL Server provides three class library files:Sqljdbc.jar, Sqljdbc4.jar, and Sqljdbc41.jar, whichever file you use depends on your preferred Java Runtime Environment (JRE) settings.

Microsoft JDBC Driver for SQL Server 4.0 provides two class library files:Sqljdbc.jar and Sqljdbc4.jar, depending on the preferred Java Runtime Environment (JRE) settings.

For more information about which JAR file to select, see the system requirements for the JDBC driver.

Set Classpath

The JDBC driver is not included in the Java SDK. If you want to use this driver, you must set Classpath to include the Sqljdbc.jar file, the Sqljdbc4.jar file, or the Sqljdbc41.jar file. If Classpath is missing a Sqljdbc.jar item, Sqljdbc4.jar item, or Sqljdbc41.jar item, the application throws a common exception for the "Class not found".

The installation location of the Sqljdbc.jar file, Sqljdbc4.jar file, or Sqljdbc41.jar file is as follows:

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

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

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

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

CLASSPATH =.; C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc.jar

The following is an example of an CLASSPATH statement for an Unix/linux application:

CLASSPATH =.:/ Home/usr1/mssqlserverjdbc/driver/sqljdbc_4.1/enu/sqljdbc.jar

You must make sure that the CLASSPATH statement contains only a Microsoft JDBC Driver for SQL Server, such as Sqljdbc.jar, Sqljdbc4.jar, or Sqljdbc41.jar.

Note

In Windows systems, if the directory name is longer than 8.3, the file name convention or the folder name contains spaces, this can cause problems with classpath. If you suspect such a problem, you should temporarily move the Sqljdbc.jar file, the Sqljdbc4.jar file, or the Sqljdbc41.jar file to a directory with a simple name, such as C:\Temp, change classpath, and then test to see if this resolves the issue.

Applications that run directly at the command prompt

The CLASSPATH is configured in the operating system. Append Sqljdbc.jar, Sqljdbc4.jar, or Sqljdbc41.jar to the classpath of the system. Alternatively, you can specify classpath on the Java command line that runs the application by using the Java-classpath option.

Applications that run in the IDE

Each IDE vendor provides different ways to set up classpath in the IDE. Setting Classpath only in the operating system will not work correctly. You must add Sqljdbc.jar, Sqljdbc4.jar, or Sqljdbc41.jar to the IDE classpath.

Servlets and JSPs

Servlets and JSPs run in the servlet/jsp engine (such as Tomcat). The classpath must be set according to the SERVLET/JSP engine documentation. Setting Classpath only in the operating system will not work correctly. Some servlet/jsp engines provide a setup screen for setting the engine's classpath. In this case, you must append the correct JDBC driver JAR file to the existing engine classpath, and then restart the engine. In other cases, you can deploy this driver by copying Sqljdbc.jar, Sqljdbc4.jar, or Sqljdbc41.jar to a specific directory such as Lib during engine installation. You can also specify the engine driver's classpath in the engine-specific configuration file.

Enterprise Java Beans

Enterprise Java Beans (EJB) runs in the EJB container. EJB containers come from multiple vendors. The Java applet runs in the browser, but is downloaded from the WEB server. Copy Sqljdbc.jar, Sqljdbc4.jar, or Sqljdbc41.jar to the WEB server root and specify the name of the jar file in the applet's HTML Archive tab, such as <applet ... archive=sqlj Dbc.jar>.

establish a simple connection to the database

When using the Sqljdbc.jar class library, the application must first register the driver as follows:

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

After the driver is loaded, the connection can be established by using the connection URL and the getconnection method of the DriverManager class:

Copy
String Connectionurl = "jdbc:sqlserver://localhost:1433;" +   "databasename=adventureworks;user=myusername; password=*****; "; Connection con = drivermanager.getconnection (Connectionurl);

In JDBC API 4.0, thedrivermanager.getconnection method has been enhanced to automatically load the JDBC driver. Therefore, when using the Sqljdbc4.jar or Sqljdbc41.jar class library, the application does not need to call the Class.forName method to register or load the driver.

When you call the getconnection method of the DriverManager class, the appropriate driver is found from the registered JDBC driver set. The Sqljdbc4.jar or Sqljdbc41.jar file includes the "Meta-inf/services/java.sql.driver" file, which contains Com.microsoft.sqlserver.jdbc.SQLServerDriver as a registered driver. Existing applications (currently loaded by using the class.forname method) will continue to work without modification.

Attention

The Sqljdbc4.jar or Sqljdbc41.jar class library is not available for earlier versions of the Java Runtime Environment (JRE). Refer to the system requirements for the JDBC driver for a list of supported JRE versions of Microsoft jdbc Driver for SQL Server.

For more information about how to connect to a data source and use a connection URL, see Create a connection URL and set connection properties.

Using the JDBC driver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.