mysql图书管理系统代码
时间: 2023-12-25 20:29:12 浏览: 96
以下是一个简单的MySQL图书管理系统的代码示例:
```java
import java.sql.*;
public class BookManagementSystem {
private static final String DB_URL = "jdbc:mysql://localhost:3306/book_db";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static void main(String[] args) {
try {
// 连接数据库
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
// 创建表
Statement stmt = conn.createStatement();
String createTableSql = "CREATE TABLE IF NOT EXISTS books (id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100), author VARCHAR(100), price DECIMAL(10, 2))";
stmt.executeUpdate(createTableSql);
// 插入数据
String insertSql = "INSERT INTO books (title, author, price) VALUES ('Java Programming', 'John Smith', 29.99)";
stmt.executeUpdate(insertSql);
// 查询数据
String selectSql = "SELECT * FROM books";
ResultSet rs = stmt.executeQuery(selectSql);
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
double price = rs.getDouble("price");
System.out.println("ID: " + id + ", Title: " + title + ", Author: " + author + ", Price: " + price);
}
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
请注意,上述代码只是一个简单的示例,实际上需要更加深入地学习Java面向对象编程、GUI编程以及MySQL数据库连接和操作等相关知识,才能够编写出完整可用的图书管理系统代码。
阅读全文