diff --git a/db/dbuml.db3 b/db/dbuml.db3 index e90e02a..917b9a6 100644 Binary files a/db/dbuml.db3 and b/db/dbuml.db3 differ diff --git a/src/view/GoodsManagementView.java b/src/view/GoodsManagementView.java index 0a1a56e..63c97c2 100644 --- a/src/view/GoodsManagementView.java +++ b/src/view/GoodsManagementView.java @@ -11,6 +11,7 @@ import model.Goods; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.time.LocalDateTime; @@ -145,19 +146,16 @@ public class GoodsManagementView extends BorderPane { String url = "jdbc:sqlite:db/dbuml.db3"; List goodsList = new ArrayList<>(); try (Connection conn = DriverManager.getConnection(url)) { - String sql = "SELECT id, goods_id as name, quantity, " + - "supplier, inbound_time, operator, remark " + - "FROM warehouse " + - "ORDER BY id"; // 不再使用GROUP BY,直接显示所有记录 + String sql = "SELECT * FROM warehouse ORDER BY inbound_time DESC"; try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { while (rs.next()) { Goods goods = new Goods( - rs.getString("id"), - rs.getString("name"), - rs.getInt("quantity"), - rs.getString("remark") + rs.getString("id"), + rs.getString("goods_id"), + rs.getInt("quantity"), + rs.getString("remark") ); goods.setInboundTime(LocalDateTime.parse(rs.getString("inbound_time"))); goods.setOperator(rs.getString("operator")); @@ -198,33 +196,41 @@ public class GoodsManagementView extends BorderPane { return; } - // 查找货物对象 - Goods existingGoods = DataManager.getInstance().getGoods(id); - if (existingGoods == null) { - // 插入新货物 - Goods newGoods = new Goods(String.format("NO%014d", System.currentTimeMillis()), - name, quantity, remark); - newGoods.setInboundTime(LocalDateTime.now()); - newGoods.setOperator("当前用户"); - newGoods.setSupplier(supplier); - DataManager.getInstance().addGoodsToDatabase(newGoods); - } else { - // 更新现有货物 - existingGoods.setName(name); - existingGoods.setQuantity(quantity); - existingGoods.setSupplier(supplier); - existingGoods.setRemark(remark); - DataManager.getInstance().updateGoodsInDatabase(existingGoods); + // 检查是否存在该记录 + String url = "jdbc:sqlite:db/dbuml.db3"; + try (Connection conn = DriverManager.getConnection(url)) { + String checkSql = "SELECT * FROM warehouse WHERE id = ?"; + try (PreparedStatement pstmt = conn.prepareStatement(checkSql)) { + pstmt.setString(1, id); + ResultSet rs = pstmt.executeQuery(); + + if (rs.next()) { + // 记录存在,执行更新操作 + String updateSql = "UPDATE warehouse SET goods_id = ?, quantity = ?, " + + "supplier = ?, remark = ? WHERE id = ?"; + try (PreparedStatement updateStmt = conn.prepareStatement(updateSql)) { + updateStmt.setString(1, name); + updateStmt.setInt(2, quantity); + updateStmt.setString(3, supplier); + updateStmt.setString(4, remark); + updateStmt.setString(5, id); + + updateStmt.executeUpdate(); + showAlert("成功", "货物信息已更新!"); + } + } else { + showAlert("错误", "未找到指定ID的货物记录!"); + return; + } + } } - // 更新表格 + // 更新表格显示 loadGoodsData(); // 清空输入 handleClear(); - showAlert("成功", "货物信息已更新!"); - } catch (Exception e) { showAlert("错误", "保存失败:" + e.getMessage()); e.printStackTrace();