清表删表

master
金安民 6 months ago
parent 8e37edb3d1
commit e2ef0e068e

@ -7,69 +7,113 @@ public class DML {
// 使用相对路径
private static final String URL = "jdbc:sqlite:db/dbuml.db3";
@Test
public void create() {
public void createInboundTable() {
try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) {
// 创建数据表的 SQL 语句
String sql = "create table if not exists xslist (" +
"id integer primary key AUTOINCREMENT not null ," +
"sno text not null," +
"sname text," +
"lx1 integer," +
"lx2 integer" +
// 创建入库记录表
String sql = "CREATE TABLE IF NOT EXISTS inbound_records (" +
"id TEXT PRIMARY KEY NOT NULL," +
"goods_id TEXT NOT NULL," +
"quantity INTEGER NOT NULL," +
"supplier TEXT," +
"inbound_time TIMESTAMP," +
"operator TEXT," +
"remark TEXT" +
");";
// 执行 SQL 语句
stmt.execute(sql);
System.out.println("数据表 创建成功");
System.out.println("入库记录表创建成功");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void deleteAll() {
public void createWarehouseTable() {
try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) {
// 删除所有记录的 SQL 语句
String sql = "DELETE FROM xslist";
// 创建仓库存放数据表
String sql = "CREATE TABLE IF NOT EXISTS warehouse (" +
"id TEXT PRIMARY KEY NOT NULL," +
"goods_id TEXT NOT NULL," +
"goods_name TEXT NOT NULL," +
"quantity INTEGER NOT NULL," +
"operation_type TEXT NOT NULL," +
"operator TEXT," +
"operation_time TIMESTAMP," +
"supplier TEXT," +
"customer TEXT," +
"remark TEXT" +
");";
// 执行 SQL 语句
int result = stmt.executeUpdate(sql);
System.out.println("删除了 " + result + " 条记录");
stmt.execute(sql);
System.out.println("仓库数据表创建成功");
} catch (SQLException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void createInboundTable() {
public void createLogTable() {
try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) {
// 创建入库记录表
String sql = "CREATE TABLE IF NOT EXISTS inbound_records (" +
"id TEXT PRIMARY KEY NOT NULL," +
"goods_id TEXT NOT NULL," +
"quantity INTEGER NOT NULL," +
"supplier TEXT," +
"inbound_time TIMESTAMP," +
"operator TEXT," +
"remark TEXT" +
// 创建操作日志数据表
String sql = "CREATE TABLE IF NOT EXISTS operation_logs (" +
"日志id TEXT PRIMARY KEY NOT NULL," + //日志id
"operation_type TEXT NOT NULL," + // 操作类型(入库、出库、修改等)
"operation_target TEXT NOT NULL," + // 操作对象货物ID或名称
"operator TEXT," + // 操作人
"operation_time TIMESTAMP," + // 操作时间
"details TEXT" + // 操作详情
");";
stmt.execute(sql);
System.out.println("入库记录表创建成功");
System.out.println("日志数据表创建成功");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void clearTable() {
try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) {
String sql = "DELETE FROM 表名";
int result = stmt.executeUpdate(sql);
System.out.println("已清空数据表,删除了 " + result + " 条记录");
} catch (SQLException e) {
e.printStackTrace();
}
} //清空指定数据表数据
@Test
public void dropTable() {
try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) {
// 删除表的SQL 语句
String sql = "DROP TABLE IF EXISTS 表名";
// 执行 SQL 语句
stmt.execute(sql);
System.out.println(" 表删除成功");
} catch (SQLException e) {
e.printStackTrace();
}
} //删除指定数据表
}

@ -1,15 +1,81 @@
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.time.LocalDateTime;
import model.InboundRecord;
import model.OutboundRecord;
import model.OperationLog;
public class DatabaseUtil {
private static final String URL = "jdbc:sqlite:db/dbuml.db3";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL);
// 记录入库操作
public static void recordInbound(InboundRecord record) {
try (Connection conn = DriverManager.getConnection(URL)) {
String sql = "INSERT INTO warehouse (id, goods_id, goods_name, quantity, operation_type, " +
"operator, operation_time, supplier, remark) " +
"VALUES (?, ?, ?, ?, 'IN', ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, record.getId());
pstmt.setString(2, record.getGoodsId());
pstmt.setString(3, ""); // 需要从Goods获取名称
pstmt.setInt(4, record.getQuantity());
pstmt.setString(5, record.getOperator());
pstmt.setString(6, record.getInboundTime().toString());
pstmt.setString(7, record.getSupplier());
pstmt.setString(8, record.getRemark());
pstmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 记录出库操作
public static void recordOutbound(OutboundRecord record) {
try (Connection conn = DriverManager.getConnection(URL)) {
String sql = "INSERT INTO warehouse (id, goods_id, goods_name, quantity, operation_type, " +
"operator, operation_time, customer, remark) " +
"VALUES (?, ?, ?, ?, 'OUT', ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, record.getId());
pstmt.setString(2, record.getGoodsId());
pstmt.setString(3, ""); // 需要从Goods获取名称
pstmt.setInt(4, record.getQuantity());
pstmt.setString(5, record.getOperator());
pstmt.setString(6, record.getOutboundTime().toString());
pstmt.setString(7, record.getCustomer());
pstmt.setString(8, record.getRemark());
pstmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 记录操作日志
public static void recordOperationLog(OperationLog log) {
try (Connection conn = DriverManager.getConnection(URL)) {
String sql = "INSERT INTO operation_logs (id, operation_type, operation_target, " +
"operator, operation_time, details) " +
"VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, log.getId());
pstmt.setString(2, log.getOperationType());
pstmt.setString(3, log.getOperationTarget());
pstmt.setString(4, log.getOperator());
pstmt.setString(5, log.getOperationTime().toString());
pstmt.setString(6, log.getDetails());
pstmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// ... 其他代码保持不变 ...
}
Loading…
Cancel
Save