使用Java和MySQL数据库实现 图书管理后台功能 1.支持用户注册与登录 如果登录失败或者注册失败,可以重新登录或注册。要求注册的账号密码不为空。登录三次失败,则关闭系统。 2.登录成功后,支持用户进行图书的增删改查操作 输入1,查询所有数据 输入2,根据id查询单条数据 输入3,新增一条数据,需要管理员录入书名、价格(元),id是唯一主键,支持自增 输入4,管理员录入id、书名、价格(元),如果id在库中不存在,则提示该书不存在,修改失败 输入5,管理员输入id,根据id删除库中对应条目。如果id在库中不存在,则提示该书不存在,删除失败 输入0,退出账号并关闭系统
时间: 2024-03-26 15:40:12 浏览: 187
好的,以下是代码实现:
```java
import java.sql.*;
import java.util.Scanner;
public class BookManagementSystem {
private static final String URL = "jdbc:mysql://localhost:3306/book_management_system?useSSL=false&serverTimezone=UTC";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
private static Connection connection;
private static Statement statement;
private static ResultSet resultSet;
public static void main(String[] args) {
try {
// 连接数据库
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
statement = connection.createStatement();
// 用户登录
int loginAttempts = 0;
while (loginAttempts < 3) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
if (login(username, password)) {
System.out.println("登录成功!");
break;
} else {
System.out.println("登录失败,请重新输入!");
loginAttempts++;
}
}
if (loginAttempts == 3) {
System.out.println("登录失败次数达到3次,系统关闭!");
System.exit(0);
}
// 图书管理
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择操作:");
System.out.println("1.查询所有数据");
System.out.println("2.根据id查询单条数据");
System.out.println("3.新增一条数据");
System.out.println("4.修改一条数据");
System.out.println("5.删除一条数据");
System.out.println("0.退出账号并关闭系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
queryAll();
break;
case 2:
System.out.println("请输入id:");
int id = scanner.nextInt();
queryById(id);
break;
case 3:
System.out.println("请输入书名:");
String name = scanner.next();
System.out.println("请输入价格:");
double price = scanner.nextDouble();
insert(name, price);
break;
case 4:
System.out.println("请输入id:");
int updateId = scanner.nextInt();
System.out.println("请输入书名:");
String updateName = scanner.next();
System.out.println("请输入价格:");
double updatePrice = scanner.nextDouble();
update(updateId, updateName, updatePrice);
break;
case 5:
System.out.println("请输入id:");
int deleteId = scanner.nextInt();
delete(deleteId);
break;
case 0:
System.out.println("退出账号并关闭系统!");
System.exit(0);
break;
default:
System.out.println("无效操作!");
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 用户登录
private static boolean login(String username, String password) throws SQLException {
String sql = "SELECT * FROM user WHERE username='" + username + "' AND password='" + password + "'";
resultSet = statement.executeQuery(sql);
return resultSet.next();
}
// 查询所有数据
private static void queryAll() throws SQLException {
String sql = "SELECT * FROM book";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
double price = resultSet.getDouble("price");
System.out.println("id:" + id + ",书名:" + name + ",价格:" + price);
}
}
// 根据id查询单条数据
private static void queryById(int id) throws SQLException {
String sql = "SELECT * FROM book WHERE id=" + id;
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
String name = resultSet.getString("name");
double price = resultSet.getDouble("price");
System.out.println("id:" + id + ",书名:" + name + ",价格:" + price);
} else {
System.out.println("该书不存在!");
}
}
// 新增一条数据
private static void insert(String name, double price) throws SQLException {
String sql = "INSERT INTO book(name, price) VALUES('" + name + "'," + price + ")";
statement.executeUpdate(sql);
System.out.println("新增成功!");
}
// 修改一条数据
private static void update(int id, String name, double price) throws SQLException {
String sql = "SELECT * FROM book WHERE id=" + id;
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
sql = "UPDATE book SET name='" + name + "', price=" + price + " WHERE id=" + id;
statement.executeUpdate(sql);
System.out.println("修改成功!");
} else {
System.out.println("该书不存在!");
}
}
// 删除一条数据
private static void delete(int id) throws SQLException {
String sql = "SELECT * FROM book WHERE id=" + id;
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
sql = "DELETE FROM book WHERE id=" + id;
statement.executeUpdate(sql);
System.out.println("删除成功!");
} else {
System.out.println("该书不存在!");
}
}
}
```
需要注意的是,这里使用了JDBC来连接MySQL数据库,需要先在本地安装并启动MySQL。另外,为了简化代码,这里使用了Statement对象来执行SQL语句,实际应用中应该使用PreparedStatement对象来防止SQL注入攻击。
阅读全文