MySQL是一種廣泛使用的開源關系型數據庫管理系統,它被廣泛用于各種應用程序的開發。圖書館管理系統是一種基于MySQL數據庫的應用程序,用于管理圖書館的圖書、借閱信息和讀者信息等。
MySQL圖書館管理系統的源代碼可以用來學習MySQL數據庫的基礎知識和應用程序開發,它包括以下幾個部分:
1、數據庫設計 CREATE DATABASE `library` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; USE library; CREATE TABLE `book_info` ( `book_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '圖書編號', `book_name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL COMMENT '圖書名稱', `publisher` varchar(255) COLLATE utf8mb4_general_ci NOT NULL COMMENT '出版社', `author` varchar(255) COLLATE utf8mb4_general_ci NOT NULL COMMENT '作者', `price` float NOT NULL COMMENT '價格', PRIMARY KEY (`book_id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE `reader_info` ( `reader_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '讀者編號', `reader_name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL COMMENT '讀者姓名', `sex` varchar(10) COLLATE utf8mb4_general_ci NOT NULL COMMENT '性別', `phone` varchar(20) COLLATE utf8mb4_general_ci NOT NULL COMMENT '電話', PRIMARY KEY (`reader_id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE `borrow_info` ( `borrow_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '借閱編號', `book_id` int(11) NOT NULL COMMENT '圖書編號', `reader_id` int(11) NOT NULL COMMENT '讀者編號', `borrow_date` date NOT NULL DEFAULT '0000-00-00' COMMENT '借書日期', `return_date` date NOT NULL DEFAULT '0000-00-00' COMMENT '還書日期', PRIMARY KEY (`borrow_id`), KEY `book_id` (`book_id`), KEY `reader_id` (`reader_id`), CONSTRAINT `borrow_info_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `book_info` (`book_id`), CONSTRAINT `borrow_info_ibfk_2` FOREIGN KEY (`reader_id`) REFERENCES `reader_info` (`reader_id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
2、應用程序開發
import java.sql.*; public class LibrarySystem { 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 = "123456"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASSWORD); stmt = conn.createStatement(); String sql = "SELECT book_name, publisher, author, price FROM book_info"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { String bookName = rs.getString("book_name"); String publisher = rs.getString("publisher"); String author = rs.getString("author"); float price = rs.getFloat("price"); System.out.println("書名:" + bookName + ",出版社:" + publisher + ",作者:" + author + ",價格:" + price); } rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } } }
以上是一個簡單的使用MySQL數據庫的Java應用程序,它可以連接到MySQL數據庫,查詢圖書館的圖書信息,并輸出到控制臺。
綜上所述,MySQL圖書館管理系統的源代碼是一個非常實用的學習MySQL數據庫和應用程序開發的資源。