第4个回答 2011-09-09
public class BaseDao {
private String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url = "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=books";
private String userName = "sa";
private String passWord = "sa";
protected Connection con;
protected PreparedStatement pstm;
protected ResultSet rs;
// 取得数据库连接
protected void getConnection() throws Exception {
Class.forName(driver);
con = DriverManager.getConnection(url, userName, passWord);
}
// 释放资源
protected void closeAll() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pstm != null) {
try {
pstm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
if (con != null && con.isClosed() == false) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}