From f9271f3a74d5521756b92317f009452bfa6b9381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=9F=8E?= <2304508674@qq.com> Date: Wed, 18 Dec 2024 01:02:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=93=E5=AD=98=E4=BF=A1=E6=81=AF=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/dbuml.db3 | Bin 36864 -> 36864 bytes src/view/GoodsManagementView.java | 62 ++++++++++++++++-------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/db/dbuml.db3 b/db/dbuml.db3 index e90e02ae4078b1cc09695d9ebda166dcbe4cd533..917b9a642640d75aedfe03e02f885a24ecc260c5 100644 GIT binary patch delta 34 qcmZozz|^pSX@WGP?nD`9M%|4G+4-EidY*1 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();