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. JSONArray的初始化的形式

    1 转义字符形式 [    {        \"ID\": \"1900036295\",        \"DEPT\": \" ...

  2. 数论知识总结——史诗大作(这是一个flag)

    1.快速幂 计算a^b的快速算法,例如,3^5,我们把5写成二进制101,3^5=3^1*1+3^2*2+3^4*1 ll fast(ll a,ll b){ll ans=;,a=mul(a,a)))a ...

  3. jquery实现全选,取消,反选的功能&实现左侧菜单

    1.全选,取消,反选的例子 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  4. sqlserver datetime的bug?

    sqlserver datetime 的毫秒的个位似乎存在bug,只有0.3.7这三个值,比如: 2018-01-20 23:59:59:999会变成2018-01-21 00:00:00.000 2 ...

  5. windows安装mysql-5.7压缩版详细教程

    前言 今天安装mysql数据库,进入mysql官网,对于windows来说有两个版本的文件可下载,一是.msi安装文件,二是.biz压缩包.以前一直用安装文件进行安装,百试不爽.今天突然不想用安装文件 ...

  6. 查看binlog的简单方法!

    今天学到一个牛逼的东西,不用打开binlog文件就可以查看binlog里的event事件. 命令为:show binlog events in 'mysql-bin.000001' from 4 li ...

  7. Debian 如何使用测试版更新软件包到最新的版本

    #vim /etc/apt/sources.list 将版本代号替换成 testing 然后更新,应可以安装最新的软件包了.

  8. jvm编译环境搭建 win Vc篇

    /************************************************************** 技术博客 http://www.cnblogs.com/itdef/   ...

  9. Shell编程-04-Shell中变量数值计算

    目录 算术运算符 算术运算命令 数值运算用法 算术运算符     在任何一门形式的语言中均会存在算术运算的情况,Shell常见的运算符如下所示: 运算符 含义 + - * / % 加 减 乘 除 求余 ...

  10. Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序

    坑爹的System.Data.SQLite. 先给出下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki ...