创建·一个图书管理系统,要求有增加功能,查询功能,删除功能,修改功能,登录功能,异常处理机制,
时间: 2024-04-02 16:32:39 浏览: 19
好的,我可以为您提供一些建议。首先,您需要定义一个数据库来存储图书信息,例如书名、作者、出版日期、出版社、ISBN 等。您可以使用 MySQL 等关系型数据库来存储这些信息。然后,您需要设计一个用户界面,让用户能够进行图书信息的增加、查询、删除和修改操作。这个界面可以使用 Java Swing 或 JavaFX 来实现。此外,您需要编写一个登录界面,让用户输入用户名和密码来登录系统。对于异常处理机制,您可以使用 try-catch 语句来捕获异常,并向用户显示错误消息。最后,您需要编写代码来处理用户的请求,将它们传递给数据库并返回相应的结果。希望这些建议对您有所帮助!
相关问题
用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. 对异常进行了简单的处理。
当然,这个代码示例还有很多不足之处,您可以根据自己的需求进行修改和完善。
用java写一个图书管理系统,要有增加,查询,删除,修改,登录功能,还要有异常处理机制
好的,下面是一个简单的图书管理系统的Java代码,包含增加、查询、删除、修改和登录功能,并且有基本的异常处理机制。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
static ArrayList<Book> books = new ArrayList<>();
static Scanner scanner = new Scanner(System.in);
static String username = "admin";
static String password = "123456";
public static void main(String[] args) {
System.out.println("欢迎使用图书管理系统!");
login();
showMenu();
}
// 登录
public static void login() {
System.out.print("请输入用户名:");
String inputUsername = scanner.nextLine();
System.out.print("请输入密码:");
String inputPassword = scanner.nextLine();
if (!inputUsername.equals(username) || !inputPassword.equals(password)) {
System.out.println("登录失败,请重新登录!");
login();
}
}
// 显示菜单
public 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、退出系统");
String choice = 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();
break;
}
}
// 添加书籍
public static void addBook() {
System.out.print("请输入书名:");
String name = scanner.nextLine();
System.out.print("请输入作者:");
String author = scanner.nextLine();
System.out.print("请输入价格:");
double price = scanner.nextDouble();
Book book = new Book(name, author, price);
books.add(book);
System.out.println("添加成功!");
showMenu();
}
// 查询书籍
public static void queryBook() {
if (books.isEmpty()) {
System.out.println("当前没有任何书籍!");
showMenu();
}
System.out.println("当前有以下书籍:");
for (Book book : books) {
System.out.println(book);
}
showMenu();
}
// 删除书籍
public static void deleteBook() {
if (books.isEmpty()) {
System.out.println("当前没有任何书籍!");
showMenu();
}
System.out.print("请输入要删除的书籍编号:");
int index = scanner.nextInt();
if (index < 0 || index >= books.size()) {
System.out.println("输入有误,请重新输入!");
deleteBook();
}
books.remove(index);
System.out.println("删除成功!");
showMenu();
}
// 修改书籍
public static void updateBook() {
if (books.isEmpty()) {
System.out.println("当前没有任何书籍!");
showMenu();
}
System.out.print("请输入要修改的书籍编号:");
int index = scanner.nextInt();
if (index < 0 || index >= books.size()) {
System.out.println("输入有误,请重新输入!");
updateBook();
}
System.out.println("请选择要修改的属性:");
System.out.println("1、书名");
System.out.println("2、作者");
System.out.println("3、价格");
String choice = scanner.nextLine();
switch (choice) {
case "1":
System.out.print("请输入新的书名:");
String name = scanner.nextLine();
books.get(index).setName(name);
System.out.println("修改成功!");
break;
case "2":
System.out.print("请输入新的作者:");
String author = scanner.nextLine();
books.get(index).setAuthor(author);
System.out.println("修改成功!");
break;
case "3":
System.out.print("请输入新的价格:");
double price = scanner.nextDouble();
books.get(index).setPrice(price);
System.out.println("修改成功!");
break;
default:
System.out.println("输入有误,请重新输入!");
updateBook();
break;
}
showMenu();
}
}
class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "书名:" + name + " 作者:" + author + " 价格:" + price;
}
}
```
这段代码实现了一个简单的图书管理系统,包含了基本的增加、查询、删除、修改和登录功能,同时也有一些基本的异常处理机制,例如输入有误时会提示重新输入。您可以根据自己的需求进行修改和扩展。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)