库存查询功能更新,查询库存

master
Your Name 6 months ago
parent 80f16a034d
commit 0b1e73aa52

@ -1,214 +1,214 @@
import java.sql.*; //import java.sql.*;
//
import org.junit.Test; //import org.junit.Test;
//
import java.sql.*; //import java.sql.*;
//
import org.junit.Test; //import org.junit.Test;
//
public class DML { //public class DML {
//
// 使用相对路径 // // 使用相对路径
private static final String URL = "jdbc:sqlite:db/dbuml.db3"; // private static final String URL = "jdbc:sqlite:db/dbuml.db3";
//
//
//
@Test // @Test
public void createInboundTable() { // public void createInboundTable() {
try (Connection conn = DriverManager.getConnection(URL); // try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) { // Statement stmt = conn.createStatement()) {
//
//
String sql = "CREATE TABLE IF NOT EXISTS warehouse (" + // String sql = "CREATE TABLE IF NOT EXISTS warehouse (" +
"id TEXT PRIMARY KEY NOT NULL," + // "id TEXT PRIMARY KEY NOT NULL," +
"goods_id TEXT NOT NULL," + // "goods_id TEXT NOT NULL," +
"quantity INTEGER NOT NULL," + // "quantity INTEGER NOT NULL," +
"supplier TEXT," + // "supplier TEXT," +
"inbound_time TIMESTAMP," + // "inbound_time TIMESTAMP," +
"operator TEXT," + // "operator TEXT," +
"remark TEXT" + // "remark TEXT" +
");"; // ");";
//
stmt.execute(sql); // stmt.execute(sql);
System.out.println("仓库数据表创建成功"); // System.out.println("仓库数据表创建成功");
//
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
//
//
//
@Test // @Test
public void createLogTable() { // public void createLogTable() {
try (Connection conn = DriverManager.getConnection(URL); // try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) { // Statement stmt = conn.createStatement()) {
//
// 创建操作日志数据表 // // 创建操作日志数据表
String sql = "CREATE TABLE IF NOT EXISTS operation_logs (" + // String sql = "CREATE TABLE IF NOT EXISTS operation_logs (" +
"id TEXT PRIMARY KEY NOT NULL," + //日志id // "id TEXT PRIMARY KEY NOT NULL," + //日志id
"operation_type TEXT NOT NULL," + // 操作类型(入库、出库、修改等) // "operation_type TEXT NOT NULL," + // 操作类型(入库、出库、修改等)
"operation_target TEXT NOT NULL," + // 操作对象货物ID或名称 // "operation_target TEXT NOT NULL," + // 操作对象货物ID或名称
"operator TEXT," + // 操作人 // "operator TEXT," + // 操作人
"operation_time TIMESTAMP," + // 操作时间 // "operation_time TIMESTAMP," + // 操作时间
"details TEXT" + // 操作详情 // "details TEXT" + // 操作详情
");"; // ");";
//
stmt.execute(sql); // stmt.execute(sql);
System.out.println("日志数据表创建成功"); // System.out.println("日志数据表创建成功");
//
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
//
//
//
@Test // @Test
public void clearTable() { // public void clearTable() {
try (Connection conn = DriverManager.getConnection(URL); // try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) { // Statement stmt = conn.createStatement()) {
//
String sql = "DELETE FROM warehouse"; // String sql = "DELETE FROM warehouse";
int result = stmt.executeUpdate(sql); // int result = stmt.executeUpdate(sql);
System.out.println("已清空数据表,删除了 " + result + " 条记录"); // System.out.println("已清空数据表,删除了 " + result + " 条记录");
//
} catch (SQLException e) { // } catch (SQLException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} //清空指定数据表数据 // } //清空指定数据表数据
@Test // @Test
public void dropTable() { // public void dropTable() {
try (Connection conn = DriverManager.getConnection(URL); // try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) { // Statement stmt = conn.createStatement()) {
//
// 删除表的SQL 语句 // // 删除表的SQL 语句
String sql = "DROP TABLE IF EXISTS inbound_records"; // String sql = "DROP TABLE IF EXISTS inbound_records";
//
// 执行 SQL 语句 // // 执行 SQL 语句
stmt.execute(sql); // stmt.execute(sql);
System.out.println(" 表删除成功"); // System.out.println(" 表删除成功");
//
} catch (SQLException e) { // } catch (SQLException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} //删除指定数据表 // } //删除指定数据表
//
@Test // @Test
public void showCurrentStock() { // public void showCurrentStock() {
try (Connection conn = DriverManager.getConnection(URL); // try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) { // Statement stmt = conn.createStatement()) {
//
// 查询每个商品的当前库存(入库减去出库) // // 查询每个商品的当前库存(入库减去出库)
String sql = "SELECT goods_id, SUM(quantity) as current_stock " + // String sql = "SELECT goods_id, SUM(quantity) as current_stock " +
"FROM warehouse " + // "FROM warehouse " +
"GROUP BY goods_id"; // "GROUP BY goods_id";
ResultSet rs = stmt.executeQuery(sql); // ResultSet rs = stmt.executeQuery(sql);
//
// 打印表头 // // 打印表头
System.out.println("===================== 当前库存状态 ====================="); // System.out.println("===================== 当前库存状态 =====================");
System.out.printf("%-20s %-15s%n", "货物名称", "当前库存"); // System.out.printf("%-20s %-15s%n", "货物名称", "当前库存");
System.out.println("==================================================="); // System.out.println("===================================================");
//
// 打印数据 // // 打印数据
while (rs.next()) { // while (rs.next()) {
System.out.printf("%-20s %-15d%n", // System.out.printf("%-20s %-15d%n",
rs.getString("goods_id"), // rs.getString("goods_id"),
rs.getInt("current_stock") // rs.getInt("current_stock")
); // );
} // }
System.out.println("==================================================="); // System.out.println("===================================================");
//
} catch (SQLException e) { // } catch (SQLException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
//
@Test // @Test
public void showAllTransactions() { // public void showAllTransactions() {
try (Connection conn = DriverManager.getConnection(URL); // try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) { // Statement stmt = conn.createStatement()) {
//
// 查询所有出入库记录 // // 查询所有出入库记录
String sql = "SELECT *, " + // String sql = "SELECT *, " +
"CASE WHEN quantity > 0 THEN '入库' ELSE '出库' END as type " + // "CASE WHEN quantity > 0 THEN '入库' ELSE '出库' END as type " +
"FROM warehouse " + // "FROM warehouse " +
"ORDER BY inbound_time DESC"; // "ORDER BY inbound_time DESC";
ResultSet rs = stmt.executeQuery(sql); // ResultSet rs = stmt.executeQuery(sql);
//
// 打印表头 // // 打印表头
System.out.println("===================== 出入库记录 ====================="); // System.out.println("===================== 出入库记录 =====================");
System.out.printf("%-15s %-15s %-8s %-8s %-15s %-25s%n", // System.out.printf("%-15s %-15s %-8s %-8s %-15s %-25s%n",
"单号", "货物名称", "数量", "类型", "供应商", "时间"); // "单号", "货物名称", "数量", "类型", "供应商", "时间");
System.out.println("==================================================="); // System.out.println("===================================================");
//
// 打印数据 // // 打印数据
while (rs.next()) { // while (rs.next()) {
System.out.printf("%-15s %-15s %-8d %-8s %-15s %-25s%n", // System.out.printf("%-15s %-15s %-8d %-8s %-15s %-25s%n",
rs.getString("id"), // rs.getString("id"),
rs.getString("goods_id"), // rs.getString("goods_id"),
Math.abs(rs.getInt("quantity")), // Math.abs(rs.getInt("quantity")),
rs.getString("type"), // rs.getString("type"),
rs.getString("supplier"), // rs.getString("supplier"),
rs.getString("inbound_time") // rs.getString("inbound_time")
); // );
} // }
System.out.println("==================================================="); // System.out.println("===================================================");
//
} catch (SQLException e) { // } catch (SQLException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
//
@Test // @Test
public void deleteOutboundRecord() { // public void deleteOutboundRecord() {
try (Connection conn = DriverManager.getConnection(URL); // try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) { // Statement stmt = conn.createStatement()) {
//
// 删除出库记录的SQL语句 // // 删除出库记录的SQL语句
String sql = "DELETE FROM warehouse WHERE id = ?"; // String sql = "DELETE FROM warehouse WHERE id = ?";
//
try (PreparedStatement pstmt = conn.prepareStatement(sql)) { // try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
// 这里可以设置要删除的记录ID // // 这里可以设置要删除的记录ID
pstmt.setString(1, "要删除的记录ID"); // pstmt.setString(1, "要删除的记录ID");
int result = pstmt.executeUpdate(); // int result = pstmt.executeUpdate();
//
if (result > 0) { // if (result > 0) {
System.out.println("出库记录删除成功"); // System.out.println("出库记录删除成功");
} else { // } else {
System.out.println("未找到要删除的记录"); // System.out.println("未找到要删除的记录");
} // }
} // }
//
} catch (SQLException e) { // } catch (SQLException e) {
e.printStackTrace(); // e.printStackTrace();
System.out.println("删除出库记录失败:" + e.getMessage()); // System.out.println("删除出库记录失败:" + e.getMessage());
} // }
} // }
//
@Test // @Test
public void createOperationLogsTable() { // public void createOperationLogsTable() {
try (Connection conn = DriverManager.getConnection(URL); // try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) { // Statement stmt = conn.createStatement()) {
//
// 删除已存在的表 // // 删除已存在的表
stmt.execute("DROP TABLE IF EXISTS operation_logs"); // stmt.execute("DROP TABLE IF EXISTS operation_logs");
//
// 创建新表 // // 创建新表
String sql = "CREATE TABLE operation_logs (" + // String sql = "CREATE TABLE operation_logs (" +
"id TEXT PRIMARY KEY NOT NULL," + // "id TEXT PRIMARY KEY NOT NULL," +
"operation_type TEXT NOT NULL," + // "operation_type TEXT NOT NULL," +
"operation_target TEXT NOT NULL," + // "operation_target TEXT NOT NULL," +
"operator TEXT NOT NULL," + // "operator TEXT NOT NULL," +
"operation_time TEXT NOT NULL," + // "operation_time TEXT NOT NULL," +
"details TEXT" + // "details TEXT" +
")"; // ")";
//
stmt.execute(sql); // stmt.execute(sql);
System.out.println("操作日志表创建成功"); // System.out.println("操作日志表创建成功");
//
} catch (SQLException e) { // } catch (SQLException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
} //}
Loading…
Cancel
Save