My development environment is: jdk1.5+jbuilder2006+sql_server2000 SP3
First, the configuration environment, including the download jdbc FRO microsft sql_server2000 Driver (under the Microsoft Official website, is the SP3 version, here will not write the specific address, the network is quite many, but the attention version, I under the SP3). After the download is found EXE file, double-click the installation. Default installation directory: C:\Program Files\Microsoft SQL Server Driver for JDBC, where C:\Program Files\Microsoft SQL Server, Driver for The three extensions under the Jdbc\lib directory are. Jar is the drive we want. Then configure the environment variables:
Add the following statement to the CLASSPATH environment variable:
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar;
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;
If you have the correct configuration of the JDK environment variables on the machine should have classpath this environment variable, if not the new one.
In fact, the purpose of the environment variable is to allow the system to find the driver, if not, execution of the following section of code will produce can not find the driver exception, resulting in the program can not be executed.
After doing these things, I used ultraedit This software first wrote the connection database code to test, the code is as follows:
/* Try to connct sql_server database*/
Import java.sql.*;
public class sql_db_test{
Connection con; Statement STA;
ResultSet rs;
String Sql_driver;
String Sql_url;
String User;
String pwd;
Public sql_db_test () {
sql_driver= "com.microsoft.jdbc.sqlserver.SQLServerDriver";//Driver
sql_url= jdbc: Microsoft:sqlserver://localhost:1433;databasename=manage ";//
Statement One, where manage is the name of the database to be accessed
user=" Xiaolin "; The database user name you want to access
pwd= "123456";//password to access the database
init ();
}
public void init ()
{
try{
Class.forName (sql_driver);//Load Driver
System.out.println ("Driver is OK");
Con=drivermanager.getconnection (SQL_URL,USER,PWD);
System.out.println ("Connection is OK");
Sta=con.createstatement ();
Rs=sta.executequery ("SELECT * from clothing table");//executequery
while (Rs.next ())
System.out.println ( Rs.getstring ("style"));
}catch (Exception e)
{
E.printstacktrace ();//printstacktrace ();
}
}
public static void main (STRIng[] args
{
new sql_db_test ();
}
}
After you save the top code, execute it in the command prompt.
Javac Sql_db_test.java//Compile normal pass
Java sql_db_test//Generates an exception, as follows
driver is OK
java.sql.SQLException: [microsoft][sqlserver driver for Jdbc]error establis
Hing Socke T.
at Com.microsoft.jdbc.base.BaseExceptions.createException (Unknown Source)
at Com.microsoft.jdbc.base.BaseExceptions.getException (Unknown Source)
at Com.microsoft.jdbc.base.BaseExceptions.getException (Unknown Source)
at Com.microsoft.jdbc.sqlserver.tds.tdsconnection.<init> (Unknown Source)
at Com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open (Unknown Sou
Rce)
at Com.microsoft.jdbc.base.BaseConnection.getNewImplConnection (Unknown S
Ource)
at Com.microsoft.jdbc.base.BaseConnection.open (Unknown Source)
at Com.microsoft.jdbc.base.BaseDriver.connect ( Unknown Source)
at Java.sql.DriverManager.getConnection (drivermanager.java:523)
at Java.sql.DriverManager.getConnection (drivermanager.java:171)
at Dbtest.init (dbtest.java:32)
at dbtest.< Init> (dbtest.java:25)
at Dbtest.main (DBTEST.JAVA:46)
Press any key to continue ...