java与mysql数据库做一个图书管理系统
时间: 2024-02-18 17:00:28 浏览: 101
java实现的完整图书管理系统+mysql+完整数据库脚本
5星 · 资源好评率100%
一个完整的图书管理系统需要包括以下功能:添加书籍、删除书籍、查询书籍、借阅书籍、归还书籍等。下面是一个简单的Java与MySQL数据库结合的图书管理系统示例,您可以参考以下代码:
```java
import java.sql.*;
import java.util.Scanner;
public class LibrarySystem {
private static final String url = "jdbc:mysql://localhost:3306/library";
private static final String user = "root";
private static final String password = "password";
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.添加书籍 2.删除书籍 3.查询书籍 4.借阅书籍 5.归还书籍 6.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addBook(con, scanner);
break;
case 2:
deleteBook(con, scanner);
break;
case 3:
queryBook(con, scanner);
break;
case 4:
borrowBook(con, scanner);
break;
case 5:
returnBook(con, scanner);
break;
case 6:
System.out.println("退出系统");
con.close();
System.exit(0);
break;
default:
System.out.println("无效选择,请重新输入!");
break;
}
}
} catch (Exception e) {
System.out.println(e);
}
}
private static void addBook(Connection con, Scanner scanner) throws SQLException {
System.out.println("请输入书名:");
String title = scanner.next();
System.out.println("请输入作者:");
String author = scanner.next();
System.out.println("请输入ISBN号:");
String isbn = scanner.next();
PreparedStatement stmt = con.prepareStatement("INSERT INTO books (title, author, isbn) VALUES (?, ?, ?)");
stmt.setString(1, title);
stmt.setString(2, author);
stmt.setString(3, isbn);
int rowsInserted = stmt.executeUpdate();
if (rowsInserted > 0) {
System.out.println("添加书籍成功!");
} else {
System.out.println("添加书籍失败!");
}
}
private static void deleteBook(Connection con, Scanner scanner) throws SQLException {
System.out.println("请输入要删除的书籍的ISBN号:");
String isbn = scanner.next();
PreparedStatement stmt = con.prepareStatement("DELETE FROM books WHERE isbn = ?");
stmt.setString(1, isbn);
int rowsDeleted = stmt.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("删除书籍成功!");
} else {
System.out.println("删除书籍失败!");
}
}
private static void queryBook(Connection con, Scanner scanner) throws SQLException {
System.out.println("请输入要查询的书籍的ISBN号:");
String isbn = scanner.next();
PreparedStatement stmt = con.prepareStatement("SELECT * FROM books WHERE isbn = ?");
stmt.setString(1, isbn);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
System.out.println("书名:" + rs.getString("title"));
System.out.println("作者:" + rs.getString("author"));
System.out.println("ISBN号:" + rs.getString("isbn"));
System.out.println("状态:" + (rs.getInt("status") == 0 ? "未借出" : "已借出"));
} else {
System.out.println("未找到该书籍!");
}
}
private static void borrowBook(Connection con, Scanner scanner) throws SQLException {
System.out.println("请输入要借阅的书籍的ISBN号:");
String isbn = scanner.next();
PreparedStatement stmt = con.prepareStatement("SELECT status FROM books WHERE isbn = ?");
stmt.setString(1, isbn);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
if (rs.getInt("status") == 0) {
PreparedStatement updateStmt = con.prepareStatement("UPDATE books SET status = 1 WHERE isbn = ?");
updateStmt.setString(1, isbn);
int rowsUpdated = updateStmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("借阅书籍成功!");
} else {
System.out.println("借阅书籍失败!");
}
} else {
System.out.println("该书籍已经借出!");
}
} else {
System.out.println("未找到该书籍!");
}
}
private static void returnBook(Connection con, Scanner scanner) throws SQLException {
System.out.println("请输入要归还的书籍的ISBN号:");
String isbn = scanner.next();
PreparedStatement stmt = con.prepareStatement("SELECT status FROM books WHERE isbn = ?");
stmt.setString(1, isbn);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
if (rs.getInt("status") == 1) {
PreparedStatement updateStmt = con.prepareStatement("UPDATE books SET status = 0 WHERE isbn = ?");
updateStmt.setString(1, isbn);
int rowsUpdated = updateStmt.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("归还书籍成功!");
} else {
System.out.println("归还书籍失败!");
}
} else {
System.out.println("该书籍未被借出!");
}
} else {
System.out.println("未找到该书籍!");
}
}
}
```
这个图书管理系统实现了基本的添加、删除、查询、借阅和归还书籍的功能。当您在MySQL数据库中创建了一个名为“books”的表时,可以在此基础上进行扩展。希望这可以帮助您开始编写Java代码来管理您的图书馆!
阅读全文