Download JDBC Driver

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

Choosing the Right JAR file

The Microsoft JDBC Driver 6.2 for SQL Server provides mssql-jdbc-6.2.1.jre7.jar, and mssql-jdbc-6.2.1.jre8.jar class library files to be used depending on your preferred Java Runtime Environment (JRE) settings.

The Microsoft JDBC Drivers 6.0 and 4.2 for SQL Server provide sqljdbc41.jar, and sqljdbc42.jar class library files to be used depending on your preferred Java Runtime Environment (JRE) settings.

The Microsoft JDBC Driver 4.1 for SQL Server provides the sqljdbc41.jar class library file to be used depending on your preferred Java Runtime Environment (JRE) settings.

The Microsoft JDBC Driver for SQL Server 4.0 provides sqljdbc.jar and sqljdbc4.jar class library files to be used depending on your preferred Java Runtime Environment (JRE) settings.

Your choice will also determine available features. For more information about which JAR file to choose, see System Requirements for the JDBC Driver.

Setting the Classpath

The JDBC driver is not part of the Java SDK. If you want to use it, you must set the classpath to include the sqljdbc.jar file, sqljdbc4.jar file, the sqljdbc41.jar file, or the sqljdbc42.jar file. If using JDBC Driver 6.2, set the classpath to include the mssql-jdbc-6.2.1.jre7.jar or mssql-jdbc-6.2.1.jre8.jar. If the classpath is missing an entry, your application will throw the common "Class not found" exception.

For Microsoft JDBC Driver 6.2

The mssql-jdbc-6.2.1.jre7.jar or mssql-jdbc-6.2.1.jre8.jar files are installed in the following location:

<installation directory>\sqljdbc_<version>\<language>\mssql-jdbc-6.2.1.jre7.jar

<installation directory>\sqljdbc_<version>\<language>\mssql-jdbc-6.2.1.jre8.jar

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

CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 6.2 for SQL Server\sqljdbc_6.2\enu\mssql-jdbc-6.2.1.jre8.jar

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

CLASSPATH =.:/home/usr1/mssqlserverjdbc/Driver/sqljdbc_6.2/enu/mssql-jdbc-6.2.1.jre8.jar

You must make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server, such as either mssql-jdbc-6.2.1.jre7.jar or mssql-jdbc-6.2.1.jre8.jar.

For Microsoft JDBC Driver 4.0, 4.1, 4.2, and 6.0

The sqljdbc.jar file, sqljdbc4.jar file, sqljdbc41.jar, or sqljdbc42.jar file are installed in the following location:

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

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

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

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

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

CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 6.0 for SQL Server\sqljdbc_4.2\enu\sqljdbc42.jar

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

CLASSPATH =.:/home/usr1/mssqlserverjdbc/Driver/sqljdbc_4.2/enu/sqljdbc42.jar

You must make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server, such as either sqljdbc.jar, sqljdbc4.jar, sqljdbc41.jar, or sqljdbc42.jar.

Note

On Windows systems, directory names longer than the 8.3 filename convention or folder names with spaces may cause problems with classpaths. If you suspect these types of issues, you should temporarily move the sqljdbc.jar file, sqljdbc4.jar file, or the sqljdbc41.jar file into a simple directory name such as C:\Temp, change the classpath, and determine whether that addresses the problem.

Applications that are 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 the classpath on the Java command line that runs the application by using the java -classpath option.

Applications that run in an IDE

Each IDE vendor provides a different method for setting the classpath in its IDE. Just setting the classpath in the operating system will not work. You must add sqljdbc.jar, sqljdbc4.jar, or sqljdbc41.jar to the IDE classpath.

Servlets and JSPs

Servlets and JSPs are run in a servlet/JSP engine such as Tomcat. The classpath must be set according to the servlet/JSP engine documentation. Just setting the classpath in the operating system will not work. Some servlet/JSP engines provide setup screens that you can use to set the classpath of the engine. In that situation, you must append the correct JDBC Driver JAR file to the existing engine 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, such as lib, during engine installation. The engine driver classpath can also be specified in an engine specific configuration file.

Enterprise Java Beans

Enterprise Java Beans (EJB) are run in an EJB container. EJB containers are sourced from various vendors. Java applets run in a browser but are downloaded from a 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 HTML archive tab of the applet, for example, <applet ... archive=sqljdbc.jar>.

Making a Simple Connection to a Database

Using the sqljdbc.jar class library, applications must first register the driver as follows:

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

When the driver is loaded, you can establish a connection by using a 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 the JDBC API 4.0, the DriverManager.getConnection method is enhanced to load JDBC drivers automatically. Therefore, applications do not need to call the Class.forName method to register or load the driver when using the sqljdbc4.jar, sqljdbc41.jar, or sqljdbc42.jar class library.

When the getConnection method of the DriverManager class is called, an appropriate driver is located from the set of registered JDBC drivers. sqljdbc4.jar, sqljdbc41.jar, or sqljdbc42.jar file includes "META-INF/services/java.sql.Driver" file, which contains the com.microsoft.sqlserver.jdbc.SQLServerDriver as a registered driver. The existing applications, which currently load the drivers by using the Class.forName method, will continue to work without modification.

Note

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

For more information about how to connect with data sources and use a connection URL, see Building the Connection URL and Setting the Connection Properties.

See Also

Overview of the JDBC Driver

Using the JDBC Driver的更多相关文章

  1. Class.forName("com.mysql.jdbc.Driver") ;

    try { Class.forName("com.mysql.jdbc.Driver") ; } catch(ClassNotFoundException e) { System. ...

  2. JDBC Driver Types

    JDBC Driver Types Type1: JDBC-ODBC Bridge Driver Type2: JDBC-Native API Type3: JDBC-Net Pure Java Ty ...

  3. [bigdata] 启动CM出现 “JDBC Driver class not found: com.mysql.jdbc.Driver” 以及“Error creating bean with name 'serverLogFetcherImpl'”问题的解决方法

    问题:“JDBC Driver class not found: com.mysql.jdbc.Driver”  通过以下命令启动cm [root@hadoop1 ~]# /etc/init.d/cl ...

  4. java中myeclipse连接mysql问题(java.lang.ClassNotFoundException: com.mysql.jdbc.Driver)

    java中myeclipse连接mysql问题(java.lang.ClassNotFoundException: com.mysql.jdbc.Driver) 1.往项目中添加mysql-conne ...

  5. JDBC driver connection string大全

    Database   / data source URL format /   driver name Value Default port MySQL URL format: jdbc:mysql: ...

  6. java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 错误的解决办法

    java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 错误的解决办法 (2011-05-05 16:08:05) 转载▼ ...

  7. unkow jdbc driver : http://maven.apache.org

    报了这么一个错,找了很久才找到问题出在哪里,具体为什么会什么出现现在还不怎么懂,只是现在能让它继续跑起来 这个错是因为我的spring-mybatis.xml文件读取不了jdbc.properties ...

  8. 【转】关于Class.forName(“com.mysql.jdbc.Driver”)

    原文:http://www.cnblogs.com/gaojing/archive/2012/03/23/2413638.html 传统的使用jdbc来访问数据库的流程为: Class.forName ...

  9. ORACLE11g JDBC Driver

    http://blog.163.com/z_rx/blog/static/276363762011312947507/ ORACLE服务器端安装程序找到相应目录"x$\app\Adminis ...

  10. JNDI数据源局部配置(解决Cannot create JDBC driver of class '' for connect URL 'null')

    最开始,我是借鉴 孤傲苍狼的JNDI教程去做的,他讲得很详细,但是坏处也就是因为他讲的太详细.查了很多书,都是建议说不要用全局去配置JNDI,一是要修改tomcat的server.xml,容易破坏to ...

随机推荐

  1. acoj-1735 输油管道 【中位数】

    题目链接:http://acdream.info/problem?pid=1735 输油管道 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 2 ...

  2. info信息总结

    查询info信息 [[NSBundle mainBundle] infoDictionary] 以下是对应的输出 { BuildMachineOSBuild = 15C50; CFBundleDeve ...

  3. ios 7.1企业证书无线安装

    ios升级到7.1时,企业证书http服务器发布无线安装不能使用,需要使用https服务器. 测试了几种方式 1.测试时自签名https证书测试无效,提示不能连接到服务器 2.使用dropbox共享连 ...

  4. jQuery的节点操作

    1.创建节点 var aa = $("<div id='cccc'>插入的内容</div>") var bb = $("<a href='h ...

  5. python性能测试脚本-乾颐堂

    废话不多说,直接上代码. import httplib import urllib import time import json     class Transaction(object):     ...

  6. [Selenium] Java代码获取屏幕分辨率

    import java.awt.Dimension; import java.awt.GraphicsEnvironment; import java.awt.Insets; import java. ...

  7. Hbase 系列(一)基本概念

    Hbase 系列(一)基本概念 HBase 是 Apache 旗下一个高可靠性.高性能.面向列.可伸缩的分布式存储系统.利用 HBase 技术可在廉价 PC 服务器上搭建起大规模的存储化集群.使用 H ...

  8. Please do not register multiple Pages in undefined.js 小程序报错的几种解决方案

    Wed Jun 27 2018 09:25:43 GMT+0800 (中国标准时间) Page 注册错误,Please do not register multiple Pages in undefi ...

  9. Django路由控制总览

    今日内容:(路由层) 1 简单配置 -第一个参数是正则表达式(如果要精准匹配:'^publish/$') -第二个参数是视图函数(不要加括号) -url(r'^admin/', admin.site. ...

  10. Linux的磁盘分区(二)

    LVM逻辑卷机制 PV(Physical Volume 物理卷) - 物理分区或整个物理磁盘 - 由PE(Physical Extent,基本单元)租场 VG(Volume Group 卷组) - 一 ...