1.
Installation: SQL Server 2000 driver for JDBC Service Pack 3
Download and install JDBC SP3
Http://www.microsoft.com/downloads/details.aspx? Familyid = 07287b11-0502-461a-b138-2aa54bfdc03a & displaylang = en
Inside setup.exe
Follow the prompts to install it. After the installation is successful, three files will be used:
C: \ Program Files \ Microsoft SQL Server 2000 driver
For JDBC \ Lib \ msbase. Jar
C: \ Program Files \ Microsoft SQL Server 2000 driver
JDBC \ Lib \ msutil. Jar
C: \ Program Files \ Microsoft SQL Server 2000 driver
JDBC \ Lib \ MSSQLServer. Jar
2. TestCode
Create a 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 connection.
Private final string selectmethod = "cursor ";
// Constructor
Public connect (){}
Private string getconnectionurl (){
Return
URL + servername + ":" + portnumber + "; databasename =" + 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. databasemetadata 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 ();
}
}
Code Source:
Http://support.microsoft.com/default.aspx? SCID = KB; ZH-CN; 313100
------------------------------------------
After success, the console outputs:
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. Problem:
The following error is output in the console during the test!
I have been searching for information for a long time. I have said that I can put the path of the Three jar files installed in JDBC into the environment variable, but I have tried it!
Java. Lang. classnotfoundexception:
Com. Microsoft. JDBC. sqlserver. sqlserverdriver
.........
Error trace in
Getconnection (): COM. Microsoft. JDBC. sqlserver. sqlserverdriver
Error: No
Active connection
Ask other people to find a solution:
Package resource manager --> package name right-click "build path" --> Configure build path --> JAVA build path --> library --> Add external jar
Select the Three jar files.
The last three JDBC files are available.
Learn JSP slowly. depressed ......
Online Poetry
Http://www.chinapoesy.com