One, set up SQL Server server: 1, start → programs → Microsoft SQL Server 2005 → configuration tools → SQL Server configuration Manager (Confirm SQL Ser Ver Management Studio "closed" 2, "SQL Server 2005 service" stopped service "SQLEXPRESS" (default is Startup state) 3, "SQL Server 2005 Network Configuration" → "MSSQLSERVER protocol", start "TCP/IP" (default is disabled), and then double-click "TCP/IP" to enter the property settings, in "IP address", confirm "IPAll" in the "TCP port" is 1433 4, "SQL Server 2005 Service" Start the service SQL Server (MSSQLSERVER) (the default is stop state) 5, close SQL Server Configuration Manager (you can start SQL Server Management Studio).
The statements for loading drivers and URLs in SQL Server 2005 are "Com.microsoft.sqlserver.jdbc.SQLServerDriver" "jdbc:sqlserver://localhost:1433; Databasename=jsptest "Note the difference between the two
JDBC connects SQL Server 2005 whole process
1. Install the JDBC driver.
Unzip the downloaded driver, generate a directory by default (Microsoft SQL Server 2005 JDBC Driver), copy this directory to the C:\Program files\ directory, set the path path in the system environment variable, add: C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.1\chs\sqljdbc.jar (write your own installation path just fine) Note: If there are other values in path, be aware of the application when adding;
If you need to configure the port, see below. 1, "start" → "programs" → "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 settings, in "IP address", you can configure "IPAll" in the "TCP port", the default is 1433. 4. Restart SQL Server or restart the computer.
Create a database open SQL Server Management Studio, log on to connect to the SQL Server server, create a new database, name teaching Test 1 in Eclipse, open eclipse, file → new → project → Java project, project name Test2, in Eclipse, select window → preferences ... → "java" → "Installed JRE", select the installed JRE, click "edit" → "add external", select%programfiles%\sqljdbc_ 1.1\CHS\SQLJDBC.JAR3, you can see Sqljdbc.jar in the JRE system Library of the test project, if there are no right-click Project test→ "Build path" → "Configuration Build path ..." → "Java build Path" → "Library" → "add external JAR ... ", choose%PROGRAMFILES%\SQLJDBC_1.1\CHS\SQLJDBC.JAR4, write Java code, as follows:
Package com.test2; Import java.sql.*;p Ublic class Demo1 {
public static void Main (string[] args) {
Define the required variables
PreparedStatement PS = null;
Connection ct = null;
ResultSet rs = null;
try {
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = drivermanager.getconnection ("jdbc:sqlserver://localhost:1433;databasename=teaching", "sa", "12345"); PS = ct.preparestatement ("SELECT * from course");
Rs=ps.executequery ();
} catch (Exception e) {
Todo:handle exception E.printstacktrace ();
}finally{
try {
if (PS! = null) {
Ps.close ();
}
if (ct! = null) {
Ct.close ();
}
} catch (Exception E2) {
Todo:handle exception
E2.printstacktrace ();
}
}
}
}