package pkg;
import java.sql.*;
public class Demo02 {
public static void main(String[] args) {
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=???";
String userName="sa";
String userPwd="???";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL,userName,userPwd);
String sql = "select * from ???";
Statement execStat = conn.createStatement();
ResultSet worths = execStat.executeQuery(sql);
while( worths.next() ) {
String id = worths.getString(1);
String idate = worths.getString(2);
String rtype = worths.getString(3);
String ritem = worths.getString(4);
String bal = worths.getString(5);
System.out.println(id+" "+idate+" "+rtype+" "+ritem+" "+bal);
}
}catch(ClassNotFoundException e) {
e.getMessage();
}catch(SQLException e) {
e.getMessage();
}finally {
//释放资源
if(rs != null) {
try {
rs.close();
}catch(SQLException e) {
e.getMessage();
}
}
if(stmt != null) {
try {
stmt.close();
}catch(SQLException e) {
e.getMessage();
}
}
if(conn != null) {
try {
conn.close();
}catch(SQLException e) {
e.getMessage();
}
}
}
}
}
java连接sql sever实例
最新推荐文章于 2022-11-10 17:58:57 发布