用Java代码创建一个图书管理系统,要求有增加功能,查询功能,删除功能,修改功能,登录功能,异常处理机制
时间: 2024-03-23 22:37:07 浏览: 57
使用Java开发的图书管理系统
好的,我可以帮您提供一个简单的 Java 代码示例:
```
import java.sql.*;
import java.util.Scanner;
public class BookManagementSystem {
private static final String DB_URL = "jdbc:mysql://localhost:3306/bookdb";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
private static Connection conn = null;
private static PreparedStatement pstmt = null;
private static ResultSet rs = null;
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
try {
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
System.out.println("连接数据库成功!");
login();
} catch (SQLException e) {
System.out.println("连接数据库失败:" + e.getMessage());
}
}
private static void login() {
System.out.print("请输入用户名:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
if (username.equals("admin") && password.equals("admin")) {
System.out.println("登录成功!");
showMenu();
} else {
System.out.println("用户名或密码错误,请重新登录!");
login();
}
}
private static void showMenu() {
System.out.println("请选择要执行的操作:");
System.out.println("1. 添加图书");
System.out.println("2. 查询图书");
System.out.println("3. 删除图书");
System.out.println("4. 修改图书");
System.out.println("5. 退出系统");
int choice = scanner.nextInt();
scanner.nextLine(); // 消耗掉输入的回车符
switch (choice) {
case 1:
addBook();
break;
case 2:
queryBook();
break;
case 3:
deleteBook();
break;
case 4:
updateBook();
break;
case 5:
System.out.println("感谢使用图书管理系统!");
System.exit(0);
default:
System.out.println("输入有误,请重新选择要执行的操作!");
showMenu();
}
}
private static void addBook() {
try {
System.out.print("请输入书名:");
String title = scanner.nextLine();
System.out.print("请输入作者:");
String author = scanner.nextLine();
System.out.print("请输入出版日期(格式为 yyyy-MM-dd):");
String pubDate = scanner.nextLine();
System.out.print("请输入出版社:");
String publisher = scanner.nextLine();
System.out.print("请输入ISBN:");
String isbn = scanner.nextLine();
String sql = "INSERT INTO book(title, author, pub_date, publisher, isbn) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, author);
pstmt.setString(3, pubDate);
pstmt.setString(4, publisher);
pstmt.setString(5, isbn);
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("添加图书成功!");
} else {
System.out.println("添加图书失败!");
}
showMenu();
} catch (SQLException e) {
System.out.println("添加图书失败:" + e.getMessage());
}
}
private static void queryBook() {
try {
System.out.print("请输入要查询的书名或作者关键字:");
String keyword = scanner.nextLine();
String sql = "SELECT * FROM book WHERE title LIKE ? OR author LIKE ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + keyword + "%");
pstmt.setString(2, "%" + keyword + "%");
rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println("查询结果如下:");
System.out.println("书名\t作者\t出版日期\t出版社\tISBN");
do {
String title = rs.getString("title");
String author = rs.getString("author");
String pubDate = rs.getString("pub_date");
String publisher = rs.getString("publisher");
String isbn = rs.getString("isbn");
System.out.println(title + "\t" + author + "\t" + pubDate + "\t" + publisher + "\t" + isbn);
} while (rs.next());
} else {
System.out.println("没有查询到相关图书!");
}
showMenu();
} catch (SQLException e) {
System.out.println("查询图书失败:" + e.getMessage());
}
}
private static void deleteBook() {
try {
System.out.print("请输入要删除的图书的ISBN:");
String isbn = scanner.nextLine();
String sql = "DELETE FROM book WHERE isbn = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, isbn);
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("删除图书成功!");
} else {
System.out.println("删除图书失败!");
}
showMenu();
} catch (SQLException e) {
System.out.println("删除图书失败:" + e.getMessage());
}
}
private static void updateBook() {
try {
System.out.print("请输入要修改的图书的ISBN:");
String isbn = scanner.nextLine();
String sql = "SELECT * FROM book WHERE isbn = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, isbn);
rs = pstmt.executeQuery();
if (rs.next()) {
System.out.print("请输入新的书名(原书名为 " + rs.getString("title") + "):");
String title = scanner.nextLine();
System.out.print("请输入新的作者(原作者为 " + rs.getString("author") + "):");
String author = scanner.nextLine();
System.out.print("请输入新的出版日期(格式为 yyyy-MM-dd,原出版日期为 " + rs.getString("pub_date") + "):");
String pubDate = scanner.nextLine();
System.out.print("请输入新的出版社(原出版社为 " + rs.getString("publisher") + "):");
String publisher = scanner.nextLine();
sql = "UPDATE book SET title = ?, author = ?, pub_date = ?, publisher = ? WHERE isbn = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, author);
pstmt.setString(3, pubDate);
pstmt.setString(4, publisher);
pstmt.setString(5, isbn);
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("修改图书成功!");
} else {
System.out.println("修改图书失败!");
}
} else {
System.out.println("没有找到要修改的图书!");
}
showMenu();
} catch (SQLException e) {
System.out.println("修改图书失败:" + e.getMessage());
}
}
}
```
这个代码示例实现了以下功能:
1. 连接 MySQL 数据库;
2. 提供登录功能;
3. 提供菜单功能,用户可以选择要执行的操作;
4. 实现添加图书、查询图书、删除图书、修改图书等功能;
5. 对异常进行了简单的处理。
当然,这个代码示例还有很多不足之处,您可以根据自己的需求进行修改和完善。
阅读全文