package ceet.ac.cn.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import ceet.ac.cn.model.Admin; public class AdminDao { public List<Admin> getAllAdmin(){ //查询所有信息 List<Admin> list = new ArrayList<Admin>(); //创建集合 Connection conn = DbHelper.getConnection(); String sql = "select * from admin"; //SQL查询语句 try { PreparedStatement pst = conn.prepareStatement(sql); ResultSet rst = pst.executeQuery(); while (rst.next()) { Admin admin = new Admin(); admin.setId(rst.getInt("id")); //得到ID admin.setUsername(rst.getString("username")); admin.setUserpwd(rst.getString("userpwd")); list.add(admin); } rst.close(); //关闭 pst.close(); //关闭 } catch (SQLException e) { e.printStackTrace(); //抛出异常 } return list; //返回一个集合 } public boolean addAdmin(Admin admin){ //添加信息 String sql = "INSERT INTO
admin(
id,
username,
userpwd) VALUES (?,?,?)"; //添加的SQL语句 Connection conn = DbHelper.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setInt(1, admin.getId()); pst.setString(2, admin.getUsername()); pst.setString(3, admin.getUserpwd()); int count = pst.executeUpdate(); pst.close(); return count>0?true:false; //是否添加的判断 } catch (SQLException e) { e.printStackTrace(); } return false; } public boolean updateAdmin(Admin admin){ //修改 String sql = "UPDATE
adminSET
username=?,
userpwd=? WHERE
id` = ?"; //修改的SQL语句,根据ID修改 Connection conn = DbHelper.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, admin.getUsername()); pst.setString(2, admin.getUserpwd()); pst.setInt(3, admin.getId()); //根据的ID int count = pst.executeUpdate(); pst.close(); //关闭 return count>0?true:false; //是否修改的判断 } catch (SQLException e) { e.printStackTrace(); } return false; } public boolean deleteAdmin(int id){ //删除 String sql = “delete from admin where id = ?”; //删除的SQL语句,根据ID删除 Connection conn = DbHelper.getConnection(); try { PreparedStatement pst = co
如何使用jsp和servrlt实现增加和删除功能
最新推荐文章于 2022-09-19 22:31:46 发布