用Java创建商品表,商品分类表、用户表和商品维护表,并插入信息
时间: 2024-02-22 14:00:21 浏览: 59
好的,我可以为您提供Java代码示例来创建这些表和插入信息。首先需要确保已经安装了Java开发环境和MySQL数据库,并且在MySQL数据库中创建了相应的数据库。
以下是Java代码示例:
```java
import java.sql.*;
public class CreateTables {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";
static final String USER = "root";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开一个连接
System.out.println("连接到数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 创建数据库
String sql = "CREATE DATABASE mydb";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("数据库已创建...");
// 选择数据库
conn = DriverManager.getConnection(DB_URL + "mydb", USER, PASS);
// 创建商品分类表
sql = "CREATE TABLE category " +
"(id INT NOT NULL AUTO_INCREMENT, " +
" name VARCHAR(255), " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
System.out.println("商品分类表已创建...");
// 创建商品表
sql = "CREATE TABLE product " +
"(id INT NOT NULL AUTO_INCREMENT, " +
" name VARCHAR(255), " +
" price DECIMAL(10,2), " +
" category_id INT, " +
" PRIMARY KEY ( id ), " +
" FOREIGN KEY (category_id) REFERENCES category(id))";
stmt.executeUpdate(sql);
System.out.println("商品表已创建...");
// 创建用户表
sql = "CREATE TABLE user " +
"(id INT NOT NULL AUTO_INCREMENT, " +
" name VARCHAR(255), " +
" email VARCHAR(255), " +
" password VARCHAR(255), " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
System.out.println("用户表已创建...");
// 创建商品维护表
sql = "CREATE TABLE product_maintenance " +
"(id INT NOT NULL AUTO_INCREMENT, " +
" product_id INT, " +
" user_id INT, " +
" maintenance_date DATE, " +
" description VARCHAR(255), " +
" PRIMARY KEY ( id ), " +
" FOREIGN KEY (product_id) REFERENCES product(id), " +
" FOREIGN KEY (user_id) REFERENCES user(id))";
stmt.executeUpdate(sql);
System.out.println("商品维护表已创建...");
// 插入商品分类数据
sql = "INSERT INTO category (name) VALUES " +
"('电脑'), " +
"('手机'), " +
"('家电')";
stmt.executeUpdate(sql);
System.out.println("商品分类数据已插入...");
// 插入商品数据
sql = "INSERT INTO product (name, price, category_id) VALUES " +
"('华为MateBook X Pro', 7999.00, 1), " +
"('iPhone 12 Pro', 8499.00, 2), " +
"('小米电视', 2799.00, 3)";
stmt.executeUpdate(sql);
System.out.println("商品数据已插入...");
// 插入用户数据
sql = "INSERT INTO user (name, email, password) VALUES " +
"('张三', 'zhangsan@example.com', 'password'), " +
"('李四', 'lisi@example.com', 'password'), " +
"('王五', 'wangwu@example.com', 'password')";
stmt.executeUpdate(sql);
System.out.println("用户数据已插入...");
// 插入商品维护数据
sql = "INSERT INTO product_maintenance (product_id, user_id, maintenance_date, description) VALUES " +
"(1, 1, '2020-01-01', '更换键盘'), " +
"(1, 2, '2020-02-01', '更换电池'), " +
"(2, 3, '2020-03-01', '更换屏幕')";
stmt.executeUpdate(sql);
System.out.println("商品维护数据已插入...");
// 完成
System.out.println("完成...");
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
```
请注意,这只是一个示例,您可能需要根据您的需求修改表结构和插入数据的方式。
阅读全文