packagecom.jdbc.dao;import java.sql.*;public classBaseDAO {//打开数据库链接
publicConnection getConn()
{
Connection conn= null;try{//加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//打开链接
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName = epetDB","sa","sa");
}catch(ClassNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnconn;
}//(重写)关闭链接
public voidClose(Connection conn,PreparedStatement pstmt,ResultSet rs)
{try{//关闭结果集
if (rs != null) {
rs.close();
}//关闭PerparedStatement对象
if (pstmt != null) {
pstmt.close();
}//关闭链接
if (conn != null) {
conn.close();
}
}catch(Exception e) {//TODO: handle exception
}
}//(重写)关闭链接
public voidClose(Connection conn,PreparedStatement pstmt)
{try{//关闭PerparedStatement对象
if (pstmt != null) {
pstmt.close();
}//关闭链接
if (conn != null) {
conn.close();
}
}catch(Exception e) {//TODO: handle exception
}
}//增删改操作
public intUpdate(String sql,Object[] parm)
{int iRet = 0;
Connection conn= null;
PreparedStatement pstmt= null;try{
conn=getConn();
pstmt=conn.prepareStatement(sql);//循环赋值参数
for (int i = 0; i < parm.length; i++) {//为预编译sql设置参数
pstmt.setObject(i+1, parm);
}//执行SQL语句
iRet =pstmt.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally{
Close(conn,pstmt);
}returniRet;
}
}