public class BaseDao {
Connection conn =null;
PreparedStatement stmt = null;
ResultSet res = null;
public int insertUpdateDelete(String sql,Object...objects){
int num = 0;
try {
conn = DBUtil.getConn();
stmt = conn.prepareStatement(sql);
if (objects != null){
for (int i = 0; i < objects.length; i++){
stmt.setObject(i+1,objects[i]);
}
}
num = stmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
DBUtil.close(null,stmt,conn);
}
return num;
}
public ResultSet select(String sql,Object...objects){
try {
conn = DBUtil.getConn();
stmt = conn.prepareStatement(sql);
if (objects != null){
for (int i = 0;i<objects.length;i++){
stmt.setObject(i+1,objects[i]);
}
}
res = stmt.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return res;
}
}
封装BaseDao 简化crud操作
于 2023-04-05 15:11:35 首次发布