Contact Java or JSP, will inevitably use the database SQL Server 2000/2005 (I use the 2005 Standard Version [9.0.3054] test), through their own search and research, using JDBC Connection to SQL Server success, hereby arrange the method ( Use Eclipse 3.2) as follows.
Preparatory work
First, install SQL Server 2000/2005 in the operating system, if the system contains 2000 and 2005, remember to deactivate one, only one line.
Then, download Microsoft SQL Server Driver JDBC 1.1 from Microsoft Web site, or use this address to download directly.
Unzip the Sqljdbc_1.1.1501.101_chs.exe and copy the sqljdbc_1.1 to%programfiles% (if the system is in C disk, C:Program Files).
Set Classpath
The JDBC driver is not included in the Java SDK. Therefore, if you want to use the driver, you must set the CLASSPATH to include the Sqljdbc.jar file. If Classpath is missing a Sqljdbc.jar entry, the application throws a common exception "class not found".
The Sqljdbc.jar files are installed in the following locations:
< installation directory >sqljdbc_< version >< language >sqljdbc.jar
The following is an example of a CLASSPATH statement for a Windows application:
CLASSPATH =.; %programfiles%sqljdbc_1.1chssqljdbc.jar
The following is an instance of the CLASSPATH statement for the Unix/linux application:
CLASSPATH =.:/ Home/usr1/mssqlserver2005jdbc/driver/sqljdbc_1.1/chs/sqljdbc.jar
Note: In the Window system, if the directory name is longer than 8.3 or if the folder name contains spaces, it will cause problems with the classpath. If you suspect such a problem, you should temporarily move the Sqljdbc.jar file to a directory with a simple name, such as C:temp, change the classpath, and then test to see if this solves the problem.
Applications that run directly at the command prompt
Configure Classpath in the operating system. Appends Sqljdbc.jar to the system's classpath. Alternatively, using the Java-classpath option, you can specify classpath on the Java command line that is running this application.
Set up SQL Server server
I'm using the SQL Server 2005 Standard Version SP2, which is all by default, and is generally not configured. If you need to configure the port, see below.
1, "Start" → "program" → "Microsoft SQL server 2005" → "Configuration Tools" → "SQL Server Configuration Manager" → "SQL Server 2005 Network Configuration" → "mssqlserver Protocol"
2, if "TCP/IP" is not enabled, right-click to select "Start".
3, double click "TCP/IP" to enter the property setting, in "IP address", can configure "TCP port" in "Ipall", default is 1433.
4. Restart SQL Server or reboot the computer.
Creating a Database
Open SQL Server Management Studio, log on to connect to the SQL Server server, create a new database, name test
Testing in Eclipse
1, open Eclipse, "file" → "new" → "Project" → "Java Project", Project name is test
2. In eclipse, select window → preferences ... → "Java" → "Installed JRE", select the installed JRE, click "edit" → "add external", select%programfiles%sqljdbc_1.1chssqljdbc.jar
3. You can see Sqljdbc.jar in the "JRE system Library" of the test project, if you don't have the option to right-click the project test→ "Build path →" Configure build path ... "→" Java build path → library → "add external jar ...", select%programfiles %sqljdbc_1.1chssqljdbc.jar
4, write Java code, as follows:
Import java.sql.*;
public class Test {
public static void Main (string[] SRG) {
String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; Load JDBC Driver
String Dburl = "jdbc:sqlserver://localhost:1433;" Databasename=test "; Connecting Server and Database test
String userName = "sa"; Default User Name
String userpwd = "123456"; Password
Connection Dbconn;
try {
Class.forName (drivername);
Dbconn = Drivermanager.getconnection (Dburl, UserName, userpwd);
System.out.println ("Connection successful!"); If the connection succeeds to the console output connection successful!
catch (Exception e) {
E.printstacktrace ();
}
}
}
Note:
1, because SQL Express this version of the server is disabled by default and the port number is not configured, so to reset
2, if you have previously used Java to connect SQL Server 2000, you should pay attention to:
The statement that loads the driver and URL paths in SQL Server 2000 is
String drivername = "Com.microsoft.jdbc.sqlserver.SQLServerDriver";
String Dburl = "jdbc:microsoft:sqlserver://localhost:1433;" Databasename=sample ";
The statements that load the driver and URL in SQL Server 2005 are
String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
String Dburl = "jdbc:sqlserver://localhost:1433;" Databasename=sample ";
If the spelling error will not find the driver.