diff --git a/db/dbuml.db3 b/db/dbuml.db3 index 1dff0f4..929c474 100644 Binary files a/db/dbuml.db3 and b/db/dbuml.db3 differ diff --git a/src/DML.java b/src/DML.java index 978ad96..58f0499 100644 --- a/src/DML.java +++ b/src/DML.java @@ -4,8 +4,8 @@ import org.junit.Test; public class DML { - // 数据库连接字符串 - private static final String URL = "jdbc:sqlite:D:/uml/db/dbuml.db3"; + // 使用相对路径 + private static final String URL = "jdbc:sqlite:db/dbuml.db3"; @Test public void create() { @@ -48,4 +48,28 @@ public class DML { e.printStackTrace(); } } + + @Test + public void createInboundTable() { + 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" + + ");"; + + stmt.execute(sql); + System.out.println("入库记录表创建成功"); + + } catch (Exception e) { + e.printStackTrace(); + } + } } \ No newline at end of file diff --git a/src/jdbc.java b/src/jdbc.java index c230765..3c30f80 100644 --- a/src/jdbc.java +++ b/src/jdbc.java @@ -6,7 +6,7 @@ public class jdbc { public static void main(String[] args) { // 定义数据库连接参数 String driver = "org.sqlite.JDBC"; // SQLite JDBC驱动 - String url = "jdbc:sqlite:./db/dbuml.db3"; // 数据库文件路径 + String url = "jdbc:sqlite:db/dbuml.db3"; // 数据库文件路径 String user = ""; // SQLite不需要用户名 String password = ""; // SQLite不需要密码 diff --git a/src/util/DatabaseUtil.java b/src/util/DatabaseUtil.java new file mode 100644 index 0000000..e1d176a --- /dev/null +++ b/src/util/DatabaseUtil.java @@ -0,0 +1,15 @@ +package util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DatabaseUtil { + private static final String URL = "jdbc:sqlite:db/dbuml.db3"; + + public static Connection getConnection() throws SQLException { + return DriverManager.getConnection(URL); + } + + // ... 其他代码保持不变 ... +} \ No newline at end of file diff --git a/src/view/InboundView.java b/src/view/InboundView.java index a347592..a4c1772 100644 --- a/src/view/InboundView.java +++ b/src/view/InboundView.java @@ -13,6 +13,7 @@ import model.Goods; import javafx.collections.FXCollections; import java.util.List; import java.util.stream.Collectors; +import java.sql.*; public class InboundView extends BorderPane { @@ -25,6 +26,7 @@ public class InboundView extends BorderPane { public InboundView() { initializeUI(); + loadInboundRecords(); } private void initializeUI() { @@ -181,12 +183,34 @@ public class InboundView extends BorderPane { // 创建入库记录 String id = "IN" + System.currentTimeMillis(); + LocalDateTime now = LocalDateTime.now(); + + // 修改数据库连接为相对路径 + String url = "jdbc:sqlite:db/dbuml.db3"; + try (Connection conn = DriverManager.getConnection(url)) { + String sql = "INSERT INTO inbound_records (id, goods_id, quantity, supplier, inbound_time, operator, remark) " + + "VALUES (?, ?, ?, ?, ?, ?, ?)"; + + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { + pstmt.setString(1, id); + pstmt.setString(2, goodsName); + pstmt.setInt(3, quantity); + pstmt.setString(4, supplier); + pstmt.setString(5, now.toString()); + pstmt.setString(6, "当前用户"); + pstmt.setString(7, remark); + + pstmt.executeUpdate(); + } + } + + // 创建记录对象并添加到表格 InboundRecord record = new InboundRecord( id, goodsName, quantity, supplier, - LocalDateTime.now(), + now, "当前用户", remark ); @@ -194,21 +218,22 @@ public class InboundView extends BorderPane { // 添加到表格 tableView.getItems().add(record); - // 更新总计 + // ��新总计 updateTotal(); // 清空输入 handleClear(); - showAlert("成功", "入库记录已保存!"); - - // 在handleSave方法中添加日志记录 + // 添加操作日志 String details = String.format("入库货物:%s,数量:%d,供应商:%s", goodsName, quantity, supplier); DataManager.getInstance().addOperationLog("入库", goodsName, "当前用户", details); + showAlert("成功", "入库记录已保存!"); + } catch (Exception e) { showAlert("错误", "保存失败:" + e.getMessage()); + e.printStackTrace(); } } @@ -226,4 +251,31 @@ public class InboundView extends BorderPane { alert.setContentText(content); alert.showAndWait(); } + + // 添加加载数据的方法 + private void loadInboundRecords() { + String url = "jdbc:sqlite:db/dbuml.db3"; + try (Connection conn = DriverManager.getConnection(url)) { + String sql = "SELECT * FROM inbound_records ORDER BY inbound_time DESC"; + try (Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql)) { + + while (rs.next()) { + InboundRecord record = new InboundRecord( + rs.getString("id"), + rs.getString("goods_id"), + rs.getInt("quantity"), + rs.getString("supplier"), + LocalDateTime.parse(rs.getString("inbound_time")), + rs.getString("operator"), + rs.getString("remark") + ); + tableView.getItems().add(record); + } + } + } catch (Exception e) { + e.printStackTrace(); + showAlert("错误", "加载入库记录失败:" + e.getMessage()); + } + } } \ No newline at end of file