圖書管理系統(tǒng)是現(xiàn)代化圖書管理的重要工具之一,它可以幫助管理員更高效地管理圖書館內(nèi)的圖書資料。為了方便管理員的工作,我們開發(fā)了基于Java和MySQL的圖書管理系統(tǒng),以下是源碼部分的介紹。
// 連接數(shù)據(jù)庫 public void connect() throws SQLException { String url = "jdbc:mysql://localhost:3306/library"; String user = "root"; String password = "123456"; connection = DriverManager.getConnection(url, user, password); } // 添加圖書信息 public void addBook(Book book) throws SQLException { String insertQuery = "INSERT INTO books (title, author, publisher, publishDate) " + "VALUES (?, ?, ?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertQuery); preparedStatement.setString(1, book.getTitle()); preparedStatement.setString(2, book.getAuthor()); preparedStatement.setString(3, book.getPublisher()); preparedStatement.setDate(4, book.getPublishDate()); preparedStatement.executeUpdate(); } // 刪除圖書信息 public void deleteBook(int bookId) throws SQLException { String deleteQuery = "DELETE FROM books WHERE id=?"; PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery); preparedStatement.setInt(1, bookId); preparedStatement.executeUpdate(); } // 修改圖書信息 public void updateBook(Book book) throws SQLException { String updateQuery = "UPDATE books SET title=?, author=?, publisher=?, publishDate=? WHERE id=?"; PreparedStatement preparedStatement = connection.prepareStatement(updateQuery); preparedStatement.setString(1, book.getTitle()); preparedStatement.setString(2, book.getAuthor()); preparedStatement.setString(3, book.getPublisher()); preparedStatement.setDate(4, book.getPublishDate()); preparedStatement.setInt(5, book.getId()); preparedStatement.executeUpdate(); } // 查詢圖書信息 public ListsearchBook(String keyword) throws SQLException { String searchQuery = "SELECT * FROM books WHERE title LIKE ? " + "OR author LIKE ? OR publisher LIKE ?"; PreparedStatement preparedStatement = connection.prepareStatement(searchQuery); preparedStatement.setString(1, "%" + keyword + "%"); preparedStatement.setString(2, "%" + keyword + "%"); preparedStatement.setString(3, "%" + keyword + "%"); ResultSet resultSet = preparedStatement.executeQuery(); List books = new ArrayList<>(); while (resultSet.next()) { Book book = new Book(); book.setId(resultSet.getInt("id")); book.setTitle(resultSet.getString("title")); book.setAuthor(resultSet.getString("author")); book.setPublisher(resultSet.getString("publisher")); book.setPublishDate(resultSet.getDate("publishDate")); books.add(book); } return books; }
以上是Java MySQL圖書管理系統(tǒng)源碼部分的介紹,其中包括連接數(shù)據(jù)庫、添加、刪除、修改、查詢等基本操作。通過這些代碼,我們可以更加方便地對圖書館內(nèi)的圖書信息進行管理,大大提高了工作效率。
上一篇mysql 34道題
下一篇mysql 3308