How to use MySQL and Java to implement an online book lending system
Introduction:
With the advancement of informatization in modern society, more and more people choose Borrow books on the Internet. In order to facilitate users to borrow books, an efficient and reliable online book lending system needs to be established. MySQL and Java are currently one of the most widely used relational databases and programming languages. This article will introduce how to use MySQL and Java to implement an online book lending system and provide specific code examples.
CREATE TABLE Book ( bookId INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), author VARCHAR(255), publisher VARCHAR(255) ); CREATE TABLE User ( userId INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255) ); CREATE TABLE Borrow ( borrowId INT AUTO_INCREMENT PRIMARY KEY, bookId INT, userId INT, borrowDate DATE, returnDate DATE, FOREIGN KEY (bookId) REFERENCES Book(bookId), FOREIGN KEY (userId) REFERENCES User(userId) );
public class BookDao { public void addBook(Book book) { // 连接数据库 Connection connection = // 连接数据库代码 // 执行插入操作 String sql = "INSERT INTO Book (title, author, publisher) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, book.getTitle()); statement.setString(2, book.getAuthor()); statement.setString(3, book.getPublisher()); statement.executeUpdate(); // 关闭连接 connection.close(); } }
public class BorrowDao { public void borrowBook(int bookId, int userId) { // 连接数据库 Connection connection = // 连接数据库代码 // 执行插入操作 String sql = "INSERT INTO Borrow (bookId, userId, borrowDate) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); statement.setInt(1, bookId); statement.setInt(2, userId); statement.setDate(3, new Date(System.currentTimeMillis())); statement.executeUpdate(); // 关闭连接 connection.close(); } }
public class BorrowDao { public void returnBook(int borrowId) { // 连接数据库 Connection connection = // 连接数据库代码 // 执行更新操作 String sql = "UPDATE Borrow SET returnDate = ? WHERE borrowId = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setDate(1, new Date(System.currentTimeMillis())); statement.setInt(2, borrowId); statement.executeUpdate(); // 关闭连接 connection.close(); } }
The above are just some simple sample codes. In actual development, more complete code needs to be written according to specific needs. You can also use Java's database operation framework, such as MyBatis or Hibernate, to simplify database operations.
Summary:
This article introduces how to use MySQL and Java to implement an online book lending system, and provides specific database design and Java code examples. Through this system, users can conveniently borrow books on the Internet, improving borrowing efficiency and user experience. Of course, developing a complete online book lending system also requires consideration of many other factors, such as user authentication, book search, etc., but the code examples provided in this article can be used as a starting point to help readers further in-depth learning and development.
The above is the detailed content of How to implement an online book lending system using MySQL and Java. For more information, please follow other related articles on the PHP Chinese website!