public class Dao {
protected static String dbClassName =
"com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库连接驱动类
protected static String dbUrl = "jdbc:sqlserver://localhost:1433;"
+ "DatabaseName=db_library;";//数据库连接 URL
protected static String dbUser = "sa"; //数据库用户名
protected static String dbPwd = "123"; //数据库密码
private static Connection conn = null; //数据库连接对象
private static String ISBN;
private Dao() { //构造方法
try {
if (conn == null) { //如果连接对象为空
Class.forName(dbClassName); //加载驱动类
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);//获得连
接对象
}
} catch (Exception ee) {
ee.printStackTrace();
}
}
private static ResultSet executeQuery(String sql) { //查询方法
try {
if(conn==null) new Dao(); //如果连接对象为空,则重新调用构造方
法
return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE).executeQuery(sql);//执行查询
} catch (SQLException e) {
e.printStackTrace();
return null; //返回 null 值
} finally {
}
}
private static int executeUpdate(String sql) { //更新方法
try {
if(conn==null) new Dao(); //如果连接对象为空,则重新调用构造方法
return conn.createStatement().executeUpdate(sql);//执行更新
} catch (SQLException e) {
e.printStackTrace();