1. Preparation: Prepare related software (except Eclipse, open source software can be downloaded from the website)
<1>. Microsoft SQL Server Edition Express
Download Address: Http://download.microsoft.com/download/0/9/0/09020fab-d2c3-4a8c-b9e0-db53a7a30ae8/SQLEXPR_CHS.EXE
<2>. SQL Server Management Studio
Download Address: http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID= c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796
<3>. SQL Server driver for JDBC
Download Address: Http://download.microsoft.com/download/8/B/D/8BDABAE2-B6EA-41D4-B903-7916EF3690EF/sqljdbc_1.2.2323.101_enu.exe
2. All downloaded after the start of the installation, the first two are database software, normal installation can (note that the database login do not use Windows authentication)
<1> extract JDBC to any location, such as decompression to the C disk under Program files, and in the installation directory to find Sqljdbc.jar file, get the path to start configuration environment variables
Append C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar after environment variable classpath
<2> set up SQLExpress server:
A. Open SQL Server Configuration Manager-> sqlexpress Protocol-> TCP/IP
B. Right-click to start TCP/IP
C. Double-click the Enter attribute to set the TCP port in IP all in IP address to 1433
D. Restart the SQLEXPRESS server in the SQL Server 2005 service
E. Shutting down SQL Server Configuration Manager
<3> open a newly installed SQL Server Management Studio, connect the SQLExpress server, create a new database, name sample
<4> Open Eclipse
A. New Project-> Java-> Java project, named Test
B. Select Eclipse-> window-> Preferences->java->installed JRE Edit already installed JDK, find directory add Sqljdbc.jar
C. Right-click Test in the Table of Contents window, select Build path->configure builds path ..., add extension jar file, add Sqljdbc.jar to it
<5> write Java code to test the connection database
Program code:
Import java.sql.*;
public class Test {public
static void Main (string[] srg) {
String drivername = ' Com.microsoft.sqlserver.jdbc.SQL Serverdriver "; Load JDBC Driver
String dburl = "jdbc:sqlserver://localhost:1433;" Databasename=sample "; Connect server and Database sample
string userName = "sa";//default username
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 the SQLExpress server is disabled by default and the port number is not configured, reset
2. If you have previously used java to connect SQL Server 2000, you should note:
The statement that loads the driver and URL paths in SQL Server 2000 is
String drivername = "Com.microsoft.
jdbc.
SQL Server . SQLServerDriver ";
String dburl = "
jdbc:
Microsoft:SQL Server://localhost:1433;" Databasename=sample ";
The statements that load the driver and URL in SQL Server 2005 are
String drivername = "Com.microsoft.
SQL Server.
JDBC . SQLServerDriver ";
String Dburl = "
jdbc:sqlserver://localhost:1433; Databasename=sample ";
if the spelling error will not find the driver .