Java通过JDBC来连接SqlServer数据库

Java通过JDBC来连接SqlServer数据库

 

0.       安装配置Java运行的环境,就不废话了

1.       下载JDBC的驱动程序http://msdn.microsoft.com/en-us/data/aa937724.aspx,这个页面包括一些驱动和文档,以及Windows版本和Unix版本

2.       1.11.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也可以访问CHSSqlServer2008

4.       刚刚开始学习Java,欢迎各位指点,有错误的话请留言告诉,谢谢!!


JDBC包中自带了几个Sample,我的测试程序就是那几个Demo(connectDS.javaconnectURL.java)
JDBC三个版本的驱动可以在本站下载

ContractedBlock.gif ExpandedBlockStart.gif 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(150);
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 != nulltry { rs.close(); } catch(Exception e) {}
44          if (cstmt != nulltry { cstmt.close(); } catch(Exception e) {}
45          if (con != nulltry { con.close(); } catch(Exception e) {}
46          System.exit(1);
47       }
48    }
49 }
50 
51 

ContractedBlock.gif ExpandedBlockStart.gif 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 != nulltry { rs.close(); } catch(Exception e) {}
39                 if (stmt != nulltry { stmt.close(); } catch(Exception e) {}
40                 if (con != nulltry { con.close(); } catch(Exception e) {}
41         }
42     }
43 }
44 
45 

 

 

作者:Tengs2000
出处:http//tengs2000.cnblogs.com
欢迎大家访问我CSDN的Blog,地址:CSDN
支持原创,希望大家和我一样如果是转载就把原作者注上,也算是对原作者作品的支持

转载于:https://www.cnblogs.com/tengs2000/articles/1321381.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值