码迷,mamicode.com
首页 > 数据库 > 详细

Java链接SqlServer数据库

时间:2016-05-29 15:00:14      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

 1 package Util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 import org.omg.CORBA.PRIVATE_MEMBER;
10 
11 public class DBUtil {
12     public static void main(String args[]) throws Exception {
13         final String url = "jdbc:sqlserver://localhost:1433;databasename=stuDB";
14         
15         try {
16             //1.注册驱动17 //            Class.forName("com.mysql.jdbc.Driver");
18             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
19             //2.获得数据链接
20             Connection conn = DriverManager.getConnection(url,"sa","123456");
21             
22             Statement stmt = conn.createStatement();
23             ResultSet rs = stmt.executeQuery("select * from stuInfoTable");
24     
25             while(rs.next()) {
26                 System.out.println(rs.getInt(1)+" "+ rs.getString(2)+" "+rs.getString(3));
27             }
28         } catch (ClassNotFoundException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         }
32     }
33 }

 

Java链接SqlServer数据库

标签:

原文地址:http://www.cnblogs.com/hxsyl/p/5539434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!