java代码写图书管理系统连接数据库,要求两个表,一个读者表(有用户名和密码两个数据),一个书籍表(有书名,ISBN,作者,出版社四个数据),要增删改查的代码
时间: 2024-03-05 14:49:45 浏览: 211
好的,下面是Java代码实现图书管理系统连接数据库,包括读者表和书籍表的增删改查操作。请确认你已经创建了名为“library”的数据库,并在其中创建了名为“reader”和“book”的两个表。
```java
import java.sql.*;
public class LibraryManagementSystem {
// 数据库连接信息
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/library";
private static final String USER = "root";
private static final String PASSWORD = "password";
private Connection conn = null;
private Statement stmt = null;
// 构造函数
public LibraryManagementSystem() {
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开连接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
// 创建语句
System.out.println("创建语句...");
stmt = conn.createStatement();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
}
}
// 关闭连接
public void close() {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
} // 什么都不做
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
System.out.println("已关闭数据库连接。");
}
// 读者表的增删改查
public void addReader(String username, String password) {
try {
String sql = "INSERT INTO reader (username, password) VALUES ('" + username + "', '" + password + "')";
stmt.executeUpdate(sql);
System.out.println("已添加读者:" + username);
} catch (SQLException se) {
se.printStackTrace();
}
}
public void deleteReader(String username) {
try {
String sql = "DELETE FROM reader WHERE username='" + username + "'";
stmt.executeUpdate(sql);
System.out.println("已删除读者:" + username);
} catch (SQLException se) {
se.printStackTrace();
}
}
public void updateReader(String username, String newPassword) {
try {
String sql = "UPDATE reader SET password='" + newPassword + "' WHERE username='" + username + "'";
stmt.executeUpdate(sql);
System.out.println("已修改读者密码:" + username);
} catch (SQLException se) {
se.printStackTrace();
}
}
public void queryReader(String username) {
try {
String sql = "SELECT * FROM reader WHERE username='" + username + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
String password = rs.getString("password");
System.out.println("读者信息:用户名:" + username + ",密码:" + password);
} else {
System.out.println("读者不存在:" + username);
}
} catch (SQLException se) {
se.printStackTrace();
}
}
// 书籍表的增删改查
public void addBook(String bookname, String ISBN, String author, String publisher) {
try {
String sql = "INSERT INTO book (bookname, ISBN, author, publisher) VALUES ('" + bookname + "', '" + ISBN + "', '" + author + "', '" + publisher + "')";
stmt.executeUpdate(sql);
System.out.println("已添加书籍:" + bookname);
} catch (SQLException se) {
se.printStackTrace();
}
}
public void deleteBook(String bookname) {
try {
String sql = "DELETE FROM book WHERE bookname='" + bookname + "'";
stmt.executeUpdate(sql);
System.out.println("已删除书籍:" + bookname);
} catch (SQLException se) {
se.printStackTrace();
}
}
public void updateBook(String bookname, String newPublisher) {
try {
String sql = "UPDATE book SET publisher='" + newPublisher + "' WHERE bookname='" + bookname + "'";
stmt.executeUpdate(sql);
System.out.println("已修改书籍出版社:" + bookname);
} catch (SQLException se) {
se.printStackTrace();
}
}
public void queryBook(String bookname) {
try {
String sql = "SELECT * FROM book WHERE bookname='" + bookname + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
String ISBN = rs.getString("ISBN");
String author = rs.getString("author");
String publisher = rs.getString("publisher");
System.out.println("书籍信息:书名:" + bookname + ",ISBN:" + ISBN + ",作者:" + author + ",出版社:" + publisher);
} else {
System.out.println("书籍不存在:" + bookname);
}
} catch (SQLException se) {
se.printStackTrace();
}
}
// 测试
public static void main(String[] args) {
LibraryManagementSystem lms = new LibraryManagementSystem();
lms.addReader("Alice", "123456");
lms.addReader("Bob", "654321");
lms.addBook("Java编程思想", "978-7-121-34023-2", "Bruce Eckel", "机械工业出版社");
lms.addBook("Thinking in Java", "978-7-115-20551-1", "Bruce Eckel", "机械工业出版社");
lms.queryReader("Alice");
lms.queryReader("Charlie");
lms.updateReader("Bob", "abcdef");
lms.deleteReader("Charlie");
lms.queryBook("Java编程思想");
lms.queryBook("Python机器学习");
lms.updateBook("Thinking in Java", "人民邮电出版社");
lms.deleteBook("Python机器学习");
lms.close();
}
}
```
注意:上述代码中的数据库连接信息、表名、字段名等需要根据实际情况进行修改。为了防止 SQL 注入攻击,这里没有使用 PreparedStatement,实际使用中建议使用 PreparedStatement。
阅读全文