Admin Dao Implementation in Java: CRUD Operations for Admin Users
public class AdminDaoImpl implements IAdminDao {
public List
String sql = 'select * from admin order by name';
Connection conn = DBUtil.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
List<Admin> list = new ArrayList<Admin>();
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next()){
Admin a = new Admin();
a.setId(rs.getInt('id'));
a.setName(rs.getString('name'));
a.setPassword(rs.getString('password'));
a.setAge(rs.getInt('age'));
a.setSex(rs.getString('sex'));
a.setTel(rs.getString('tel'));
a.setPhone(rs.getString('phone'));
a.setAddr(rs.getString('addr'));
a.setMemo(rs.getString('memo'));
list.add(a);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(rs);
DBUtil.close(stmt);
DBUtil.close(conn);
}
return list;
}
内容:public Admin getAdminById(int id) {
String sql = 'select * from admin where id=?';
Connection conn = DBUtil.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
Admin a = null;
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if(rs.next()){
a = new Admin();
a.setId(rs.getInt('id'));
a.setName(rs.getString('name'));
a.setPassword(rs.getString('password'));
a.setAge(rs.getInt('age'));
a.setSex(rs.getString('sex'));
a.setTel(rs.getString('tel'));
a.setPhone(rs.getString('phone'));
a.setAddr(rs.getString('addr'));
a.setMemo(rs.getString('memo'));
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(rs);
DBUtil.close(stmt);
DBUtil.close(conn);
}
return a;
}
public Admin getAdminByName(String name) {
String sql = 'select * from admin where name=?';
Connection conn = DBUtil.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
Admin a = null;
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
rs = stmt.executeQuery();
if(rs.next()){
a = new Admin();
a.setId(rs.getInt('id'));
a.setName(rs.getString('name'));
a.setPassword(rs.getString('password'));
a.setAge(rs.getInt('age'));
a.setSex(rs.getString('sex'));
a.setTel(rs.getString('tel'));
a.setPhone(rs.getString('phone'));
a.setAddr(rs.getString('addr'));
a.setMemo(rs.getString('memo'));
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(rs);
DBUtil.close(stmt);
DBUtil.close(conn);
}
return a;
}
public boolean addAdmin(Admin admin) {
String sql = 'insert into admin(name,password,age,sex,tel,phone,addr,memo) values(?,?,?,?,?,?,?,?)';
Connection conn = DBUtil.getConnection();
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, admin.getName());
stmt.setString(2, admin.getPassword());
stmt.setInt(3, admin.getAge());
stmt.setString(4, admin.getSex());
stmt.setString(5, admin.getTel());
stmt.setString(6, admin.getPhone());
stmt.setString(7, admin.getAddr());
stmt.setString(8, admin.getMemo());
int count = stmt.executeUpdate();
if(count > 0){
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(stmt);
DBUtil.close(conn);
}
return false;
}
public boolean updateAdmin(Admin admin) {
String sql = 'update admin set name=?,password=?,age=?,sex=?,tel=?,phone=?,addr=?,memo=? where id=?';
Connection conn = DBUtil.getConnection();
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, admin.getName());
stmt.setString(2, admin.getPassword());
stmt.setInt(3, admin.getAge());
stmt.setString(4, admin.getSex());
stmt.setString(5, admin.getTel());
stmt.setString(6, admin.getPhone());
stmt.setString(7, admin.getAddr());
stmt.setString(8, admin.getMemo());
stmt.setInt(9, admin.getId());
int count = stmt.executeUpdate();
if(count > 0){
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(stmt);
DBUtil.close(conn);
}
return false;
}
public boolean deleteAdmin(int id) {
String sql = 'delete from admin where id=?';
Connection conn = DBUtil.getConnection();
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
int count = stmt.executeUpdate();
if(count > 0){
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(stmt);
DBUtil.close(conn);
}
return false;
}
原文地址: https://www.cveoy.top/t/topic/n4Si 著作权归作者所有。请勿转载和采集!