private static final String URL="jdbc:sqlservlet://localhost:3306;databasename=mstanford";
private static final String USER="sa";
private static final String PASSWORD="root";
//获取连接
public static Connection getConnection(){
Connection con = null; //声明一个连接对象
try {
Class.forName(DRIVER);
//注册驱动
con = DriverManager.getConnection(URL,USER,PASSWORD); //获得连接对象
} catch (ClassNotFoundException e) { //捕获驱动类无法找到异常
e.printStackTrace();
} catch (SQLException e) { //捕获SQL异常
e.printStackTrace();
}
return con;
}
public static void close(Connection con) {//关闭连接对象
if(con != null) { //如果conn连接对象不为空
try {
con.close(); //关闭conn连接对象对象
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement pstmt) {//关闭预处理对象
if(pstmt != null) { //如果pstmt预处理对象不为空
try {
pstmt.close(); //关闭pstmt预处理对象
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs) {//关闭结果集对象
if(rs != null) { //如果rs结果集对象不为null
try {
rs.close(); //关闭rs结果集对象
} catch (SQLException e) {
e.printStackTrace();
}
}
}