jdbc connection

sqljdbc.jar 2005 is a Java program to connect to the database mssql driver, the Java Database Connectivity (JDBC) driver of Microsoft SQL Server 2005. The SQL Server 2005 JDBC Driver download is free to all SQL Server users and provides access to SQL Server 2000 and SQL Server 2005 from any Java application, application server, or Java-enabled applet. This is a Type 4 JDBC driver that provides database connectivity through the standard JDBC application programming interface (API) in J2EE (Java2 Enterprise Edition). 

Install or decompress the JDBC driver for the SqlServer2005 database 
, and obtain the sqljdbc.jar file, which is the JDBC driver. Put sqljdbc.jar on the classpath. (The web application is placed under WEB-INF/lib) The SQL Server 2005 JDBC driver is JDBC 3.0 compliant and runs on Java Development Kit (JDK) version 1.4 and later. All major application servers including BEA WebLogic, IBM WebSphere, JBoss and Sun have been tested.

java connection sqlserver2005 database

First you have to download the driver to the Microsoft website to download Microsoft JDBC Driver 4.0 for SQL Server 

Open and copy the unzipped folder to %ProgramFiles% (C:\Program Files if the system is on the C drive). 

[Note] The decompressed folder of sqljdbc_4.0.2206.100_chs.exe contains two files, sqljdbc.jar and sqljdbc4.jar. We use the sqljdbc4.jar file. 

set classpath

The JDBC driver is not included in the Java SDK. Therefore, if you want to use this driver, you must set the classpath to include the sqljdbc.jar file. If the classpath is missing the sqljdbc.jar entry, the application throws the usual "class not found" exception.

The installation location of the sqljdbc.jar file is as follows:  

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

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

CLASSPATH =.;%ProgramFiles%\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\chs\sqljdbc4.jar

Set up SQL Server server

I am using SQL Server 2005, these are all defaults, and generally do not need to be configured.

See below if you need to configure the port.

1. "Start" → "Programs" → "Microsoft SQL Server 2008" → "Configuration Tools" → "SQL Server Configuration Manager" → "SQL Server 2005 Network Configuration" → "MSSQLSERVER Protocol"

2. If "TCP/IP" is not enabled, right-click and select "Start".

4. Restart SQL Server.

Test in Eclipse

1. Open Eclipse, "File" → "New" → "Project" → "Java Project", the project name is Test

2. In Eclipse, select "Window" → "Preferences..." → "Java" → "Installed JRE", select the installed JRE, click "Edit" → "Add External", select %ProgramFiles% \Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\en\sqljdbc4.jar

3、在Test项目的“JRE 系统库”中可以看见sqljdbc.jar,

如果没有可以右键单击项目Test→“构建路径”→“配置构建路径...”→“Java 构建路径”→“库”→“添加外部JAR...”,选择%ProgramFiles%\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\chs\sqljdbc4.jar

4、编写Java代码,如下:

   import java.sql.*;

   public class Test {

    public static void main(String[] srg) {

    String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //

加载JDBC驱动

String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=test"; //连接服务器和数据库test

    String userName = "sa"; //默认用户名

String userPwd = "123456"; //安装sql server 2005时的密码

   Connection dbConn;

   try {

    Class.forName(driverName);

    dbConn = DriverManager.getConnection(dbURL, userName, userPwd);

   System.out.println("Connection Successful!"); //如果连接成功 

控制台输出Connection Successful!

   } catch (Exception e) {

   e.printStackTrace();

   }

   }

   }

  如果成功测试结果为:Connection Successful

用Java连接sqlserver数据库时候几个jar包的区别

***msbase.jar、mssqlserver.jar、msutil.jar(支持sql2000):

早期的连接数据库Jar包:在连接数据时候需要手动导入驱动包。即手动导入class.forName(驱动名称);

***SQL Server JDBC Driver 2.0----sqljdbc.jar和sqljdbc4.jar

新版JDBC连接数据库Jar包,支持sql2005,sql2008

 Sqljdbc.jar

使用 sqljdbc.jar 类库时,应用程序必须首先按class.forName(驱动名称)注册驱动程序。Jdk1.6以上版本不推荐使用.

Sqljdbc4.jar

在 JDBC API 4.0 中,DriverManager.getConnection 方法得到了增强,可自动加载 JDBC Driver。因此,使用sqljdbc4.jar 类库时,应用程序无需调用 Class.forName 方法来注册或加载驱动程序。调用 DriverManager 类的 getConnection 方法时,会从已注册的 JDBC Driver 集中找到相应的驱动程序。sqljdbc4.jar 文件包括“META-INF/services/java.sql.Driver”文件,后者包含.sqlserver.jdbc.SQLServerDriver 作为已注册的驱动程序。现有的应用程序(当前通过使用 Class.forName 方法加载驱动程序)将继续工作,而无需修改。

注意: sqljdbc4.jar 类库要求使用 6.0 或更高版本的 Java 运行时环境 (JRE)。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325807434&siteId=291194637