帮我写一个基于Java并连接mysql控制台版的图书管理系统,请给出具体源代码
时间: 2024-02-15 22:04:18 浏览: 114
由于一个完整的基于Java并连接MySQL的图书管理系统代码量较大,我无法在此处给出完整的代码。以下是一个简单的示例,供您参考:
```
import java.sql.*;
public class BookManagementSystem {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private void connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book_management", "root", "password");
stmt = conn.createStatement();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
private void close() {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void showMenu() {
System.out.println("Welcome to Book Management System!");
System.out.println("1. Add book");
System.out.println("2. Delete book");
System.out.println("3. Modify book");
System.out.println("4. Search book");
System.out.println("5. Borrow book");
System.out.println("6. Return book");
System.out.println("7. Exit");
}
private void addBook() {
try {
System.out.println("Enter book ID:");
int id = Integer.parseInt(System.console().readLine());
System.out.println("Enter book name:");
String name = System.console().readLine();
System.out.println("Enter book author:");
String author = System.console().readLine();
stmt.executeUpdate("INSERT INTO book (id, name, author) VALUES (" + id + ", '" + name + "', '" + author + "')");
} catch (SQLException e) {
e.printStackTrace();
}
}
private void deleteBook() {
try {
System.out.println("Enter book ID:");
int id = Integer.parseInt(System.console().readLine());
stmt.executeUpdate("DELETE FROM book WHERE id = " + id);
} catch (SQLException e) {
e.printStackTrace();
}
}
private void modifyBook() {
try {
System.out.println("Enter book ID:");
int id = Integer.parseInt(System.console().readLine());
System.out.println("Enter new book name:");
String name = System.console().readLine();
System.out.println("Enter new book author:");
String author = System.console().readLine();
stmt.executeUpdate("UPDATE book SET name = '" + name + "', author = '" + author + "' WHERE id = " + id);
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchBook() {
try {
System.out.println("Enter book name or author:");
String keyword = System.console().readLine();
rs = stmt.executeQuery("SELECT * FROM book WHERE name LIKE '%" + keyword + "%' OR author LIKE '%" + keyword + "%'");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String author = rs.getString("author");
System.out.println("ID: " + id + ", Name: " + name + ", Author: " + author);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void borrowBook() {
try {
System.out.println("Enter book ID:");
int id = Integer.parseInt(System.console().readLine());
stmt.executeUpdate("UPDATE book SET borrowed = true WHERE id = " + id);
} catch (SQLException e) {
e.printStackTrace();
}
}
private void returnBook() {
try {
System.out.println("Enter book ID:");
int id = Integer.parseInt(System.console().readLine());
stmt.executeUpdate("UPDATE book SET borrowed = false WHERE id = " + id);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void run() {
connect();
while (true) {
showMenu();
System.out.println("Select an option:");
int option = Integer.parseInt(System.console().readLine());
switch (option) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
modifyBook();
break;
case 4:
searchBook();
break;
case 5:
borrowBook();
break;
case 6:
returnBook();
break;
case 7:
close();
System.exit(0);
default:
System.out.println("Invalid option!");
}
}
}
public static void main(String[] args) {
BookManagementSystem system = new BookManagementSystem();
system.run();
}
}
```
请注意,这只是一个简单的示例,它没有包含异常处理、密码加密等重要功能,仅供参考。如果您需要更加完整的代码实现,建议您参考一些开源的图书管理系统代码。
阅读全文