Use JDBC driver

Source: Internet
Author: User
Document directory
  • Select the correct JAR File
  • Set classpath
  • Establish a simple connection with the database
  • See

This section provides quick start guide for using the Microsoft SQL Server JDBC driver to establish a simple connection with the SQL Server database. Before connecting to the SQL Server database, you must first Install SQL server on the local computer or server and install JDBC driver on the local computer.

Select the correct JAR File

Microsoft SQL Server JDBC driver 2.0 provides two class library files:Sqljdbc. JarAndSqljdbc4.jarThe specific file used depends on the preferred Java Runtime Environment (JRE) settings. For more information about which JAR file to select, see JDBC driver system requirements.

Set classpath

The JDBC driver is not included in the Java SDK. To use this driver, you must set classpath to include the sqljdbc. jar file or the sqljdbc4.jar file. If the classpath lacks the sqljdbc. Jar item or the sqljdbc4.jar item, the application will cause a common exception of "class not found.

The installation path of the sqljdbc. jar file and the sqljdbc4.jar file is as follows:

<Installation Directory> \ Sqljdbc _ <Version>\<Language> \ Sqljdbc. Jar

<Installation Directory> \ Sqljdbc _ <Version>\<Language> \ Sqljdbc4.jar

The following is a classpath statement example for a Windows application:

CLASSPATH =.;C:\Program Files\Microsoft SQL Server JDBC Driver\sqljdbc_2.0\enu\sqljdbc.jar

The following is a classpath statement example for Unix/Linux applications:

CLASSPATH =.:/home/usr1/mssqlserverjdbc/Driver/sqljdbc_2.0/enu/sqljdbc.jar

Make sure that the classpath statement contains only one Microsoft SQL Server JDBC driver, such as sqljdbc. jar or sqljdbc4.jar.

Note:

In Windows, if the directory name is longer than 8.3 or the folder name contains spaces, the classpath may be faulty. If you suspect that this type of problem exists, temporarily move the sqljdbc. jar file or the sqljdbc4.jar file to a directory with a simple name, such C:\Temp, Change the classpath, and then test whether the problem is resolved.

Applications that run directly at the command prompt

Classpath is configured in the operating system. Append sqljdbc. jar or sqljdbc4.jar to the classpath of the system. Or, use java -classpath You can specify the classpath on the Java command line that runs the application.

Applications Running in IDE

Each ide vendor provides different methods to set classpath in IDE. Setting classpath only in the operating system will not work properly. You must add sqljdbc. jar or sqljdbc4.jar to ide classpath.

Servlet and JSP

Servlet and JSP run in Servlet/JSP Engine (such as Tomcat. You must set classpath according to the servlet/JSP Engine documentation. Setting classpath only in the operating system will not work properly. Some Servlet/jsp engines provide the setting screen for setting the classpath of the engine. In this case, you must append the correct JDBC driver JAR file to the existing engine classpath and restart the engine. In other cases, you can deploy this driver by copying sqljdbc. jar or sqljdbc4.jar to a specific directory such as lib during engine installation. You can also specify the classpath of the engine driver in the engine-specific configuration file.

Enterprise Java Beans

Enterprise Java Bean (EJB) runs in the EJB container. The EJB container comes from multiple vendors. The Java Applet runs in the browser but is downloaded from the web server. Copy sqljdbc. jar or sqljdbc4.jar to the Web server root directory, and specify the name of the JAR file in the HTML Archive Tab Of the applet, for example<applet ... archive=sqljdbc.jar>.

Establish a simple connection with 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, you can use the connection URL andDrivermanagerClassGetconnectionMethod To establish a connection:

Copy code

String connectionUrl = "jdbc:sqlserver://localhost:1433;" +   "databaseName=AdventureWorks;user=MyUserName;password=*****;";Connection con = DriverManager.getConnection(connectionUrl);

In the jdbc api 4.0,Drivermanager. getconnectionThe method is enhanced to automatically load the JDBC driver. Therefore, when using the sqljdbc4.jar class library, the application does not need to callClass. fornameMethod to register or load the driver.

CallDrivermanagerClassGetconnectionMethod, the corresponding driver is found from the registered JDBC driver set. The sqljdbc4.jar file contains the META-INF/services/Java. SQL. Driver file, which containsCom. Microsoft. sqlserver. JDBC. sqlserverdriverAs a registered driver. Existing applicationsClass. fornameMethod to load the driver) will continue to work without modification.

Note:

The sqljdbc4.jar Class Library requires a Java Runtime Environment (JRE) of version 6.0 or later ).

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

See other resources

JDBC driver Overview

 

Source: http://msdn.microsoft.com/zh-cn/library/ms378526.aspx

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.