1. Installation: SQL Server Driver for JDBC Service Pack 3
Download install JDBC SP3
Http://www.jb51.net/softs/234108.html
Inside the installation package
Follow the prompts to install it. There are three files to use after success:
C:/Program Files/microsoft SQL Server Driver for Jdbc/lib/msbase.jar
C:/Program Files/microsoft SQL Server Driver for Jdbc/lib/msutil.jar
C:/Program Files/microsoft SQL Server Driver for Jdbc/lib/mssqlserver.jar
2. Test code
New class file Connect.java.
Package test;
Import java.*;
Import Java.sql.Driver;
public class connect{private java.sql.Connection con = null;
Private final String url = "jdbc:microsoft:sqlserver://";
Private final String servername= "localhost";
Private final String portnumber = "1433";
Private final String databasename= "dbtest";
Private final String userName = "sa";
Private final String password = "123456"; Informs the driver to use Server a side-cursor,//which permits more than one active statement/on a connectio
N. private final String SelectMethod = "cursor"; Constructor public Connect () {} private String getConnectionUrl () {return url+servername+ ":" +portnumber+;d A
Tabasename= "+databasename+"; selectmethod= "+selectmethod+"; ";} Private Java.sql.Connection getconnection () {try{class.forname ("Com.microsoft.jdbc.sqlserver.SQLServerDriver
");
con = java.sql.DriverManager.getConnection (getConnectionUrl (), Username,password); if (con!=null) System.out.println ("Connection successful!");
}catch (Exception e) {e.printstacktrace ();
System.out.println ("Error Trace in Getconnection ():" + e.getmessage ());
return con; }/* Display the driver properties, database details */public void displaydbproperties () {JAVA.SQL.D
Atabasemetadata DM = null;
Java.sql.ResultSet rs = null;
try{con= this.getconnection ();
if (con!=null) {dm = Con.getmetadata ();
SYSTEM.OUT.PRINTLN ("Driver information");
System.out.println ("/tdriver Name:" + dm.getdrivername ());
System.out.println ("/tdriver Version:" + dm.getdriverversion ());
SYSTEM.OUT.PRINTLN ("/ndatabase information");
System.out.println ("/tdatabase Name:" + dm.getdatabaseproductname ());
System.out.println ("/tdatabase Version:" + dm.getdatabaseproductversion ());
System.out.println ("avalilable catalogs");rs = Dm.getcatalogs ();
while (Rs.next ()) {System.out.println ("/tcatalog:" + rs.getstring (1));
} rs.close ();
rs = null;
CloseConnection ();
}else System.out.println ("Error:no active Connection");
}catch (Exception e) {e.printstacktrace ();
} dm=null;
private void CloseConnection () {try{if (con!=null) con.close ();
Con=null;
}catch (Exception e) {e.printstacktrace ();
} public static void Main (string[] args) throws Exception {Connect mydbtest = new Connect ();
Mydbtest.displaydbproperties (); }
}
Source of code:
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;313100
------------------------------------------
Console output after success:
Connection successful!
Driver Information
Driver Name:sqlserver
Driver version:2.2.0040
Database Information
Database Name:microsoft SQL Server
Database Version:microsoft SQL Server 2000-8.00.760 (Intel X86)
Dec 17 2002 14:22:05
Copyright (c) 1988-2003 Microsoft Corporation
Enterprise Edition on Windows NT 5.2 (build 3790:)
Avalilable Catalogs
Catalog:dbtest
...........
3. Question:
In the test console old output the following error!
Look for the information for a long time. It says that the path to the three jar files after the JDBC installation is in the environment variable. But I tried!
Java.lang.ClassNotFoundException:com.microsoft.jdbc.sqlserver.SQLServerDriver
.........
Error Trace in Getconnection (): Com.microsoft.jdbc.sqlserver.SQLServerDriver
Error:no Active Connection
Ask someone else to find a way:
Package Explorer--> Package name right-click Build path--> Configuration Build path-->java build path--> library--> add external jar
It's OK to choose the three jars.
The following three JDBC files are added.