標籤:
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 block30 e.printStackTrace();31 }32 }33 }
Java連結SqlServer資料庫