package com.ltstruts;
import java.sql.*;
class TestDB{
private final String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private final String url = "jdbc:microsoft:sqlserver://localhost:1433;selectMethod=cursor;DatabaseName=pubs";
private final String password = "";
private final String userName = "sa";
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
public Connection getConnection(){
try{
Class.forName(Driver);
conn = DriverManager.getConnection(url,userName,password);
}catch(Exception e){
e.printStackTrace();
}
try{
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from authors";
rs = stmt.executeQuery(sql);
}catch(Exception e){
e.printStackTrace();
}
try{
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void main(String []args){
TestDB tt=new TestDB();
tt.getConnection();
}
}
注意 事项: 怎么 导入SQL的包
在 TOOL->CONFIG->直接导入你要 加入的 三个包
运行结果:
172-32-1176
White
213-46-8915
Green
238-95-7766
Carson
267-41-2394
O'Leary
274-80-9391
Straight
341-22-1782
Smith
409-56-7008
Bennet
427-17-2319
Dull
472-27-2349
Gringlesby
486-29-1786
Locksley
527-72-3246
Greene
648-92-1872
Blotchet-Halls
672-71-3249
Yokomoto
712-45-1867
del Castillo
722-51-5454
DeFrance
724-08-9931
Stringer
724-80-9391
MacFeather
756-30-7391
Karsen
807-91-6654
Panteley
846-92-7186
Hunter
893-72-1158
McBadden
899-46-2035
Ringer
998-72-3567
Ringer