Java通过JDBC来连接SqlServer数据库
0. 安装配置Java运行的环境,就不废话了
1. 下载JDBC的驱动程序http://msdn.microsoft.com/en-us/data/aa937724.aspx,这个页面包括一些驱动和文档,以及Windows版本和Unix版本
2. 1.1,1.2版本有本地化版本,目前最新的版本可以支持SqlServer2008但是只有ENU的
1.1,1.2:支持SQL Server2000,SQL Server2005
2.0 :支持SQL Server2000,SQL Server2005,SQL Server2008
由于只是在DOS下面执行的,没有使用Java编辑器,需要将解压出来的sqljdbc.jar添加到CLASSPATH中,这个应该会吧,哈哈!直接将jar看成一个文件夹,将整个路径贴到CLASSPATH中!至于使用Eclipse等工具时这个步骤可能是多余的啊,由于本人刚开始学也不知道!
3. JDBC2.0 ENU也可以访问CHS的SqlServer2008。
4. 刚刚开始学习Java,欢迎各位指点,有错误的话请留言告诉,谢谢!!
JDBC包中自带了几个Sample,我的测试程序就是那几个Demo(connectDS.java与connectURL.java)
JDBC三个版本的驱动可以在本站下载
connectDS.java
1 import java.sql.*;
2 import com.microsoft.sqlserver.jdbc.*;
3
4 public class connectDS {
5
6 public static void main(String[] args) {
7
8 // Declare the JDBC objects.
9 Connection con = null;
10 CallableStatement cstmt = null;
11 ResultSet rs = null;
12
13 try {
14 // Establish the connection.
15 SQLServerDataSource ds = new SQLServerDataSource();
16 ds.setUser("sa");//数据库用户名
17 ds.setPassword("sa");//数据库密码
18 ds.setServerName("HIOF-SHUAIT");//服务器名
19 ds.setPortNumber(1433); //端口号
20 ds.setDatabaseName("AdventureWorks");//访问的数据库
21 con = ds.getConnection();
22
23 // Execute a stored procedure that returns some data.
24 cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");
25 cstmt.setInt(1, 50);
26 rs = cstmt.executeQuery();
27
28 // Iterate through the data in the result set and display it.
29 while (rs.next()) {
30 System.out.println("EMPLOYEE: " + rs.getString("LastName") +
31 ", " + rs.getString("FirstName"));
32 System.out.println("MANAGER: " + rs.getString("ManagerLastName") +
33 ", " + rs.getString("ManagerFirstName"));
34 System.out.println();
35 }
36 }
37
38 // Handle any errors that may have occurred.
39 catch (Exception e) {
40 e.printStackTrace();
41 }
42 finally {
43 if (rs != null) try { rs.close(); } catch(Exception e) {}
44 if (cstmt != null) try { cstmt.close(); } catch(Exception e) {}
45 if (con != null) try { con.close(); } catch(Exception e) {}
46 System.exit(1);
47 }
48 }
49 }
50
51
connectURL.java
1 import java.sql.*;
2 public class connectURL {
3
4 public static void main(String[] args) {
5
6 // Create a variable for the connection string.
7 String connectionUrl = "jdbc:sqlserver://hiof-shuait\\sql2008:1433;" +
8 "databaseName=AdventureWorks;user=sa;password=sa";
9 //connectionUrl中的sql2008是我服务器上的sql2008别名
10 // Declare the JDBC objects.
11 Connection con = null;
12 Statement stmt = null;
13 ResultSet rs = null;
14
15 try {
16 System.out.println("Print the record.");
17 // Establish the connection.
18 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
19 con = DriverManager.getConnection(connectionUrl);
20
21 // Create and execute an SQL statement that returns some data.
22 String SQL = "SELECT TOP 20 * FROM Person.Contact";
23 stmt = con.createStatement();
24 rs = stmt.executeQuery(SQL);
25
26 // Iterate through the data in the result set and display it.
27 while (rs.next()) {
28 System.out.println(rs.getString(4) + " " + rs.getString(6));
29 }
30 }
31
32 // Handle any errors that may have occurred.
33 catch (Exception e) {
34 e.printStackTrace();
35 }
36
37 finally {
38 if (rs != null) try { rs.close(); } catch(Exception e) {}
39 if (stmt != null) try { stmt.close(); } catch(Exception e) {}
40 if (con != null) try { con.close(); } catch(Exception e) {}
41 }
42 }
43 }
44
45