入库功能连接数据库

master
王城 6 months ago
parent 25b88059fe
commit d3ef202c8b

Binary file not shown.

@ -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();
}
}
}

@ -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不需要密码

@ -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);
}
// ... 其他代码保持不变 ...
}

@ -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);
// 新总计
// <EFBFBD><EFBFBD>新总计
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());
}
}
}
Loading…
Cancel
Save