连接需要三个包:
mssqlserver.jar
msutil.jar
msbase.jar代码如下:
package com;
import java.sql.*;
import java.util.ArrayList;public class ReadSQL {
public ReadSQL(){}
public ResultSet SQLT() {
    ResultSet rst=null;
    try {
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
      Connection con = java.sql.DriverManager.getConnection
          ("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dfl_zzm",
           "sa", "sa");
      Statement stmt = con.createStatement();
      rst = stmt.executeQuery("select * from TPeopleInfo");
    }
    catch (ClassNotFoundException e) {
      System.out.println(e);
    }
    catch (SQLException e) {
      System.out.println("SQL not found!");}
     catch (InstantiationException e) {
     System.out.println("SQL not found!");
    }
    catch (IllegalAccessException e) {
    System.out.println("SQL not found!");
   }
    return rst;
  }
//把数据转成二维数组形式,好传到客户端
public String[][] RSql(ResultSet inrst){
ArrayList<String[]> RSqlAL=new ArrayList<String[]>();
String str[];
String OutRSql[][];
int colNumber=0;
//int rowNumber=0;
try {
ResultSetMetaData metadata=inrst.getMetaData();
//得到列数
colNumber=metadata.getColumnCount();
while(inrst.next()){
str=new String[colNumber];
for (int i=0; i<colNumber;i++){
str[i]=inrst.getString(i+1);
}
RSqlAL.add(str);
}
} catch (SQLException e) {
e.printStackTrace();
}
//数据转入数组,以便传送到客户端
OutRSql=new String[RSqlAL.size()][colNumber];
for (int i=0;i<RSqlAL.size();i++){
for(int j=0;j<colNumber;j++){
OutRSql[i][j]=((String[])(RSqlAL.get(i)))[j];
}
}
//打印显示
for(int i=0;i<OutRSql.length;i++){
   for(int j =0;j<colNumber;j++){
   System.out.print(OutRSql[i][j]+" ");
   }
   System.out.println();
   }
return OutRSql;
}
public static void main(String []args) throws SQLException {
    ReadSQL ss=new ReadSQL();
    ResultSet res=ss.SQLT();    
    ss.RSql(res);
  
  }}