用java和mysql实现访客管理系统的黑名单增删改查功能,并将代码分为action、dao、pojo、service四个部分。代码中要求实现与数据库连接,并且详细写出getter和setter构造方法
时间: 2023-11-28 12:47:41 浏览: 94
首先,我们需要创建一个访客信息的POJO类,包含访客姓名、年龄、性别、电话号码、邮箱、是否在黑名单中等属性。代码如下:
public class Guest {
private int id; //访客ID
private String name; //访客姓名
private int age; //访客年龄
private String gender; //访客性别
private String phone; //访客电话号码
private String email; //访客邮箱
private boolean inBlackList; //是否在黑名单中
public Guest() {
}
public Guest(int id, String name, int age, String gender, String phone, String email, boolean inBlackList) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.email = email;
this.inBlackList = inBlackList;
}
//getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isInBlackList() {
return inBlackList;
}
public void setInBlackList(boolean inBlackList) {
this.inBlackList = inBlackList;
}
}
接下来,我们创建一个GuestDao类,用于数据库访问。代码如下:
public class GuestDao {
private Connection conn = null;
private PreparedStatement pstmt = null;
public GuestDao(Connection conn) {
this.conn = conn; //获取数据库连接
}
//添加访客信息到数据库
public boolean addGuest(Guest guest) throws SQLException {
String sql = "insert into guest(name,age,gender,phone,email,inBlackList) values(?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, guest.getName());
pstmt.setInt(2, guest.getAge());
pstmt.setString(3, guest.getGender());
pstmt.setString(4, guest.getPhone());
pstmt.setString(5, guest.getEmail());
pstmt.setBoolean(6, guest.isInBlackList());
int count = pstmt.executeUpdate();
if (count > 0) {
return true;
} else {
return false;
}
}
//从数据库中删除指定ID的访客信息
public boolean deleteGuest(int id) throws SQLException {
String sql = "delete from guest where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int count = pstmt.executeUpdate();
if (count > 0) {
return true;
} else {
return false;
}
}
//更新数据库中指定ID的访客信息
public boolean updateGuest(Guest guest) throws SQLException {
String sql = "update guest set name=?,age=?,gender=?,phone=?,email=?,inBlackList=? where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, guest.getName());
pstmt.setInt(2, guest.getAge());
pstmt.setString(3, guest.getGender());
pstmt.setString(4, guest.getPhone());
pstmt.setString(5, guest.getEmail());
pstmt.setBoolean(6, guest.isInBlackList());
pstmt.setInt(7, guest.getId());
int count = pstmt.executeUpdate();
if (count > 0) {
return true;
} else {
return false;
}
}
//根据ID从数据库中查询访客信息
public Guest findGuestById(int id) throws SQLException {
String sql = "select * from guest where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
Guest guest = new Guest();
guest.setId(rs.getInt("id"));
guest.setName(rs.getString("name"));
guest.setAge(rs.getInt("age"));
guest.setGender(rs.getString("gender"));
guest.setPhone(rs.getString("phone"));
guest.setEmail(rs.getString("email"));
guest.setInBlackList(rs.getBoolean("inBlackList"));
return guest;
} else {
return null;
}
}
//查询所有访客信息
public List<Guest> findAllGuest() throws SQLException {
String sql = "select * from guest";
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
List<Guest> list = new ArrayList<>();
while (rs.next()) {
Guest guest = new Guest();
guest.setId(rs.getInt("id"));
guest.setName(rs.getString("name"));
guest.setAge(rs.getInt("age"));
guest.setGender(rs.getString("gender"));
guest.setPhone(rs.getString("phone"));
guest.setEmail(rs.getString("email"));
guest.setInBlackList(rs.getBoolean("inBlackList"));
list.add(guest);
}
return list;
}
}
然后,我们创建一个GuestService类,用于业务处理。代码如下:
public class GuestService {
private Connection conn = null;
private GuestDao guestDao = null;
public GuestService() {
conn = DBHelper.getConnection(); //获取数据库连接
guestDao = new GuestDao(conn);
}
//添加访客信息到数据库
public boolean addGuest(Guest guest) throws SQLException {
return guestDao.addGuest(guest);
}
//从数据库中删除指定ID的访客信息
public boolean deleteGuest(int id) throws SQLException {
return guestDao.deleteGuest(id);
}
//更新数据库中指定ID的访客信息
public boolean updateGuest(Guest guest) throws SQLException {
return guestDao.updateGuest(guest);
}
//根据ID从数据库中查询访客信息
public Guest findGuestById(int id) throws SQLException {
return guestDao.findGuestById(id);
}
//查询所有访客信息
public List<Guest> findAllGuest() throws SQLException {
return guestDao.findAllGuest();
}
}
最后,我们创建一个GuestAction类,用于与前端界面交互。代码如下:
public class GuestAction {
private GuestService guestService = null;
public GuestAction() {
guestService = new GuestService();
}
//添加访客信息到数据库
public boolean addGuest(Guest guest) throws SQLException {
return guestService.addGuest(guest);
}
//从数据库中删除指定ID的访客信息
public boolean deleteGuest(int id) throws SQLException {
return guestService.deleteGuest(id);
}
//更新数据库中指定ID的访客信息
public boolean updateGuest(Guest guest) throws SQLException {
return guestService.updateGuest(guest);
}
//根据ID从数据库中查询访客信息
public Guest findGuestById(int id) throws SQLException {
return guestService.findGuestById(id);
}
//查询所有访客信息
public List<Guest> findAllGuest() throws SQLException {
return guestService.findAllGuest();
}
}
以上就是一个基于Java和MySQL实现的访客管理系统的黑名单增删改查功能的代码,将代码分为action、dao、pojo、service四个部分,实现了与数据库的连接,并且详细写出了getter和setter构造方法。
阅读全文