用java和MySQL实现访客管理系统的黑名单的增删改查功能的代码,并且这个代码里包含了异常处理。
时间: 2023-06-24 16:05:44 浏览: 104
以下是一个简单的访客管理系统的黑名单增删改查功能的Java代码,使用MySQL作为数据库,并包含异常处理:
```java
import java.sql.*;
import java.util.Scanner;
public class BlacklistManagementSystem {
// 定义数据库连接信息
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/guestbook";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
// 定义常量表示SQL语句
private static final String INSERT_SQL = "INSERT INTO blacklist(name, reason) VALUES (?, ?)";
private static final String DELETE_SQL = "DELETE FROM blacklist WHERE id = ?";
private static final String UPDATE_SQL = "UPDATE blacklist SET name = ?, reason = ? WHERE id = ?";
private static final String SELECT_SQL = "SELECT * FROM blacklist WHERE id = ?";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.添加黑名单 2.删除黑名单 3.修改黑名单 4.查询黑名单 5.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addBlacklist();
break;
case 2:
deleteBlacklist();
break;
case 3:
updateBlacklist();
break;
case 4:
selectBlacklist();
break;
case 5:
System.out.println("再见!");
return;
default:
System.out.println("无效的操作!");
break;
}
}
}
// 添加黑名单
private static void addBlacklist() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = scanner.nextLine();
System.out.println("请输入原因:");
String reason = scanner.nextLine();
try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
PreparedStatement stmt = conn.prepareStatement(INSERT_SQL)) {
stmt.setString(1, name);
stmt.setString(2, reason);
int count = stmt.executeUpdate();
if (count > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} catch (SQLException e) {
System.out.println("添加失败:" + e.getMessage());
}
}
// 删除黑名单
private static void deleteBlacklist() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的黑名单id:");
int id = scanner.nextInt();
scanner.nextLine(); // 读取多余的换行符
try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
PreparedStatement stmt = conn.prepareStatement(DELETE_SQL)) {
stmt.setInt(1, id);
int count = stmt.executeUpdate();
if (count > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
} catch (SQLException e) {
System.out.println("删除失败:" + e.getMessage());
}
}
// 修改黑名单
private static void updateBlacklist() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的黑名单id:");
int id = scanner.nextInt();
scanner.nextLine(); // 读取多余的换行符
System.out.println("请输入姓名:");
String name = scanner.nextLine();
System.out.println("请输入原因:");
String reason = scanner.nextLine();
try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
PreparedStatement stmt = conn.prepareStatement(UPDATE_SQL)) {
stmt.setString(1, name);
stmt.setString(2, reason);
stmt.setInt(3, id);
int count = stmt.executeUpdate();
if (count > 0) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败!");
}
} catch (SQLException e) {
System.out.println("修改失败:" + e.getMessage());
}
}
// 查询黑名单
private static void selectBlacklist() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的黑名单id:");
int id = scanner.nextInt();
scanner.nextLine(); // 读取多余的换行符
try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
PreparedStatement stmt = conn.prepareStatement(SELECT_SQL)) {
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
System.out.println("姓名:" + rs.getString("name"));
System.out.println("原因:" + rs.getString("reason"));
} else {
System.out.println("未找到该黑名单!");
}
} catch (SQLException e) {
System.out.println("查询失败:" + e.getMessage());
}
}
}
```
这个代码使用了Java的JDBC API来连接MySQL数据库,并使用了PreparedStatement来执行带参数的SQL语句,以避免SQL注入攻击。在每个操作中都包含了异常处理代码,以避免程序崩溃并能够给用户友好的提示信息。
阅读全文