用java和MySQL实现一个访客管理系统,这个系统有一个黑名单功能,这个黑名单功能可以进行增删改查功能,并且这个系统已经包含异常处理,请给出详细代码。并详细解释这些代码的意思。
时间: 2023-06-17 12:08:48 浏览: 242
jfinal简单实例,可实现对数据的增删改查操作,有详细的代码解释,sql文件(MySQL)
3星 · 编辑精心推荐
以下是用Java和MySQL实现访客管理系统的代码,包含黑名单的增删改查功能和异常处理:
1. 首先创建一个Visitor类,包含访客的姓名、电话和是否在黑名单中的状态。
```
public class Visitor {
private String name;
private String phone;
private boolean isBlacklisted;
public Visitor(String name, String phone, boolean isBlacklisted) {
this.name = name;
this.phone = phone;
this.isBlacklisted = isBlacklisted;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public boolean isBlacklisted() {
return isBlacklisted;
}
public void setBlacklisted(boolean blacklisted) {
isBlacklisted = blacklisted;
}
}
```
2. 创建一个VisitorDAO类,处理与访客数据库的交互。
```
import java.sql.*;
public class VisitorDAO {
private final String url = "jdbc:mysql://localhost:3306/visitor_management_system";
private final String user = "root";
private final String password = "password";
public VisitorDAO() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public void addVisitor(Visitor visitor) {
String sql = "INSERT INTO visitors (name, phone, is_blacklisted) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, visitor.getName());
pstmt.setString(2, visitor.getPhone());
pstmt.setBoolean(3, visitor.isBlacklisted());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Visitor getVisitor(String phone) {
String sql = "SELECT * FROM visitors WHERE phone = ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, phone);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
boolean isBlacklisted = rs.getBoolean("is_blacklisted");
Visitor visitor = new Visitor(name, phone, isBlacklisted);
return visitor;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void updateVisitor(Visitor visitor) {
String sql = "UPDATE visitors SET is_blacklisted = ? WHERE phone = ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setBoolean(1, visitor.isBlacklisted());
pstmt.setString(2, visitor.getPhone());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteVisitor(String phone) {
String sql = "DELETE FROM visitors WHERE phone = ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, phone);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. 创建一个VisitorManagementSystem类,用于管理访客和黑名单。
```
import java.util.Scanner;
public class VisitorManagementSystem {
private VisitorDAO visitorDAO;
public VisitorManagementSystem() {
visitorDAO = new VisitorDAO();
}
public void addVisitor() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter visitor name: ");
String name = scanner.nextLine();
System.out.print("Enter visitor phone: ");
String phone = scanner.nextLine();
Visitor visitor = new Visitor(name, phone, false);
visitorDAO.addVisitor(visitor);
}
public void searchVisitor() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter visitor phone: ");
String phone = scanner.nextLine();
Visitor visitor = visitorDAO.getVisitor(phone);
if (visitor != null) {
System.out.println("Name: " + visitor.getName());
System.out.println("Phone: " + visitor.getPhone());
System.out.println("Blacklisted: " + visitor.isBlacklisted());
} else {
System.out.println("Visitor not found.");
}
}
public void updateVisitor() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter visitor phone: ");
String phone = scanner.nextLine();
Visitor visitor = visitorDAO.getVisitor(phone);
if (visitor != null) {
System.out.println("Name: " + visitor.getName());
System.out.println("Phone: " + visitor.getPhone());
System.out.println("Blacklisted: " + visitor.isBlacklisted());
System.out.print("Is blacklisted? (true/false): ");
boolean isBlacklisted = scanner.nextBoolean();
visitor.setBlacklisted(isBlacklisted);
visitorDAO.updateVisitor(visitor);
} else {
System.out.println("Visitor not found.");
}
}
public void deleteVisitor() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter visitor phone: ");
String phone = scanner.nextLine();
visitorDAO.deleteVisitor(phone);
}
public static void main(String[] args) {
VisitorManagementSystem system = new VisitorManagementSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add visitor");
System.out.println("2. Search visitor");
System.out.println("3. Update visitor");
System.out.println("4. Delete visitor");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
system.addVisitor();
break;
case 2:
system.searchVisitor();
break;
case 3:
system.updateVisitor();
break;
case 4:
system.deleteVisitor();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
}
}
}
}
```
4. 创建一个MySQL数据库,名为visitor_management_system。其中包含一个名为visitors的表,存储访客的姓名、电话和是否在黑名单中的状态。
```
CREATE DATABASE IF NOT EXISTS visitor_management_system;
USE visitor_management_system;
CREATE TABLE IF NOT EXISTS visitors (
name VARCHAR(255) NOT NULL,
phone VARCHAR(20) NOT NULL PRIMARY KEY,
is_blacklisted BOOLEAN NOT NULL DEFAULT false
);
```
在上述代码中,创建了一个访客类Visitor,包含访客的姓名、电话和是否在黑名单中的状态。创建了一个VisitorDAO类,处理与访客数据库的交互。在VisitorDAO类中,使用了JDBC连接MySQL数据库的方法,包含增加访客、查找访客、更新访客和删除访客的方法。创建了一个VisitorManagementSystem类,用于管理访客和黑名单。在VisitorManagementSystem类中,使用了Scanner类来获取用户输入,包含添加访客、查找访客、更新访客和删除访客的方法。最后创建了一个MySQL数据库,名为visitor_management_system,其中包含一个名为visitors的表,存储访客的姓名、电话和是否在黑名单中的状态。
以上是访客管理系统的详细代码和解释。
阅读全文