货物信息连接数据库

master
xzk 6 months ago
parent 06421d49b1
commit cba3cb7438

@ -1,8 +1,9 @@
<component name="libraryTable">
<library name="junit-4.12">
<library name="junit-4.12" type="repository">
<properties maven-id="junit:junit:4.13.1" />
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />

Binary file not shown.

@ -116,4 +116,33 @@ public class DML {
} //删除指定数据表
@Test
public void createGoodsTable() {
try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement()) {
// 创建货物信息表
String sql = "CREATE TABLE IF NOT EXISTS goods (" +
"id TEXT PRIMARY KEY NOT NULL," +
"name TEXT NOT NULL," +
"specification TEXT," +
"unit TEXT," +
"price REAL," +
"category TEXT," +
"quantity INTEGER," +
"remark TEXT," +
"inbound_time TIMESTAMP," +
"operator TEXT," +
"supplier TEXT" +
");";
stmt.execute(sql);
System.out.println("货物信息表创建成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -45,6 +45,10 @@ public class Main extends Application {
primaryStage.setTitle("仓库管理系统");
primaryStage.show();
// 创建 goods 表
DML dml = new DML();
dml.createGoodsTable();
} catch (Exception e) {
e.printStackTrace();
}

@ -31,64 +31,43 @@ public class DataManager {
}
private void initializeData() {
// 从数据库加载数据
loadDataFromDatabase();
}
private void loadDataFromDatabase() {
String url = "jdbc:sqlite:db/dbuml.db3";
try (Connection conn = DriverManager.getConnection(url)) {
// 加载商品数据
loadGoods(conn);
// 加载供应商数据
loadSuppliers(conn);
// 加载库存数据
loadInventory(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
private void loadGoods(Connection conn) throws SQLException {
String sql = "SELECT * FROM goods";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String specification = rs.getString("specification");
String unit = rs.getString("unit");
double price = rs.getDouble("price");
String category = rs.getString("category");
String remark = rs.getString("remark");
Goods goods = new Goods(id, name, specification, unit, price, category, 0, remark);
goodsMap.put(id, goods);
}
}
}
private void loadSuppliers(Connection conn) throws SQLException {
String sql = "SELECT supplier_name FROM suppliers";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
suppliers.add(rs.getString("supplier_name"));
}
}
}
private void loadInventory(Connection conn) throws SQLException {
String sql = "SELECT goods_id, quantity FROM inventory";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
String goodsId = rs.getString("goods_id");
int quantity = rs.getInt("quantity");
inventory.put(goodsId, quantity);
}
}
// 电子产品类
addGoodsWithStock("G001", "苹果手机", "iPhone 15", "台", 6999.00, "电子产品", 50);
addGoodsWithStock("G002", "联想笔记本", "ThinkPad T14", "台", 5999.00, "电子产品", 30);
addGoodsWithStock("G003", "华为平板", "MatePad Pro", "台", 3999.00, "电子产品", 40);
addGoodsWithStock("G004", "戴尔显示器", "27寸 4K", "台", 2399.00, "电子产品", 25);
// 办公家具类
addGoodsWithStock("G005", "办公桌", "1.4米", "张", 799.00, "办公家具", 100);
addGoodsWithStock("G006", "办公椅", "人体工学", "把", 599.00, "办公家具", 150);
addGoodsWithStock("G007", "文件柜", "三层", "个", 459.00, "办公家具", 80);
addGoodsWithStock("G008", "会议桌", "2.4米", "张", 1599.00, "办公家具", 20);
// 办公用品类
addGoodsWithStock("G009", "打印纸", "A4 70g", "箱", 89.00, "办公用品", 200);
addGoodsWithStock("G010", "签字笔", "0.5mm黑色", "盒", 15.00, "办公用品", 500);
addGoodsWithStock("G011", "订书机", "标准型", "个", 35.00, "办公用品", 300);
addGoodsWithStock("G012", "文件夹", "A4蓝色", "个", 8.00, "办公用品", 1000);
// 添加供应商
suppliers.add("苹果官方旗舰店");
suppliers.add("联想专卖店");
suppliers.add("华为授权店");
suppliers.add("戴尔官方店");
suppliers.add("京东自营");
suppliers.add("天猫超市");
suppliers.add("办公伙伴专营店");
suppliers.add("文具批发商");
suppliers.add("家具城");
suppliers.add("办公用品直营店");
}
private void addGoodsWithStock(String id, String name, String specification,
String unit, double price, String category, int initialStock) {
Goods goods = new Goods(id, name, specification, unit, price, category, initialStock, "");
goodsMap.put(id, goods);
inventory.put(id, initialStock);
}
public void addGoods(Goods goods) {
@ -100,26 +79,14 @@ public class DataManager {
public static List<InventoryReport> generateInventoryReport() {
List<InventoryReport> reports = new ArrayList<>();
// 数据库连接URL
String url = "jdbc:sqlite:db/dbuml.db3";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
// 使用与InboundView相同的表结构
String sql = "SELECT " +
" goods_id, " +
" goods_id as goods_name, " + // 因为InboundView中使用goods_id存储的是货物名称
" SUM(quantity) as total_quantity, " +
" GROUP_CONCAT(DISTINCT supplier) as supplier, " +
" CASE " +
" WHEN SUM(quantity) < 10 THEN '库存偏低' " +
" ELSE '库存正常' " +
" END as remark " +
"FROM inbound_records " +
"GROUP BY goods_id " +
"ORDER BY goods_id";
ResultSet rs = stmt.executeQuery(sql);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT goods_id, goods_name, SUM(quantity) as total_quantity, supplier, remark FROM inbound_records GROUP BY goods_id")) {
while (rs.next()) {
String goodsId = rs.getString("goods_id");
String goodsName = rs.getString("goods_name");
@ -186,4 +153,64 @@ public class DataManager {
})
.collect(Collectors.toList());
}
public void addGoodsToDatabase(Goods goods) {
String url = "jdbc:sqlite:db/dbuml.db3";
String sql = "INSERT INTO goods (id, name, specification, unit, price, category, quantity, remark, inbound_time, operator, supplier) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (Connection conn = DriverManager.getConnection(url);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, goods.getId());
pstmt.setString(2, goods.getName());
pstmt.setString(3, goods.getSpecification());
pstmt.setString(4, goods.getUnit());
pstmt.setDouble(5, goods.getPrice());
pstmt.setString(6, goods.getCategory());
pstmt.setInt(7, goods.getQuantity());
pstmt.setString(8, goods.getRemark());
pstmt.setString(9, goods.getInboundTime().toString());
pstmt.setString(10, goods.getOperator());
pstmt.setString(11, goods.getSupplier());
pstmt.executeUpdate();
addGoods(goods); // 同步内存中的数据
addOperationLog("新增", goods.getId(), "当前用户", "新增货物: " + goods.getName());
} catch (SQLException e) {
e.printStackTrace();
showAlert("错误", "添加货物失败:" + e.getMessage());
}
}
public void updateGoodsInDatabase(Goods goods) {
String url = "jdbc:sqlite:db/dbuml.db3";
String sql = "UPDATE goods SET name = ?, specification = ?, unit = ?, price = ?, category = ?, " +
"quantity = ?, remark = ?, inbound_time = ?, operator = ?, supplier = ? WHERE id = ?";
try (Connection conn = DriverManager.getConnection(url);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, goods.getName());
pstmt.setString(2, goods.getSpecification());
pstmt.setString(3, goods.getUnit());
pstmt.setDouble(4, goods.getPrice());
pstmt.setString(5, goods.getCategory());
pstmt.setInt(6, goods.getQuantity());
pstmt.setString(7, goods.getRemark());
pstmt.setString(8, goods.getInboundTime().toString());
pstmt.setString(9, goods.getOperator());
pstmt.setString(10, goods.getSupplier());
pstmt.setString(11, goods.getId());
pstmt.executeUpdate();
addOperationLog("更新", goods.getId(), "当前用户", "更新货物: " + goods.getName());
} catch (SQLException e) {
e.printStackTrace();
showAlert("错误", "更新货物失败:" + e.getMessage());
}
}
private void showAlert(String , String s) {
}
}

@ -1,6 +1,10 @@
package model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Goods {
private String id;
@ -53,6 +57,48 @@ public class Goods {
this.supplier = "";
}
public static void insertIntoDatabase(Connection conn, Goods goods) throws SQLException {
String sql = "INSERT INTO goods (id, name, specification, unit, price, category, quantity, remark, inbound_time, operator, supplier) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
try {
pstmt.setString(1, goods.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
pstmt.setString(2, goods.getName());
pstmt.setString(3, goods.getSpecification());
pstmt.setString(4, goods.getUnit());
pstmt.setDouble(5, goods.getPrice());
pstmt.setString(6, goods.getCategory());
pstmt.setInt(7, goods.getQuantity());
pstmt.setString(8, goods.getRemark());
pstmt.setString(9, goods.getInboundTime() != null ? goods.getInboundTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) : null);
pstmt.setString(10, goods.getOperator());
pstmt.setString(11, goods.getSupplier());
pstmt.executeUpdate();
}
}
public static void updateInDatabase(Connection conn, Goods goods) throws SQLException {
String sql = "UPDATE goods SET name = ?, specification = ?, unit = ?, price = ?, category = ?, " +
"quantity = ?, remark = ?, inbound_time = ?, operator = ?, supplier = ? WHERE id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, goods.getName());
pstmt.setString(2, goods.getSpecification());
pstmt.setString(3, goods.getUnit());
pstmt.setDouble(4, goods.getPrice());
pstmt.setString(5, goods.getCategory());
pstmt.setInt(6, goods.getQuantity());
pstmt.setString(7, goods.getRemark());
pstmt.setString(8, goods.getInboundTime() != null ? goods.getInboundTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) : null);
pstmt.setString(9, goods.getOperator());
pstmt.setString(10, goods.getSupplier());
pstmt.setString(11, goods.getId());
pstmt.executeUpdate();
}
}
// Getters and Setters
public String getId() { return id; }
public void setId(String id) { this.id = id; }

@ -8,10 +8,13 @@ import javafx.scene.layout.*;
import javafx.scene.control.cell.PropertyValueFactory;
import model.DataManager;
import model.Goods;
import model.InventoryManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class GoodsManagementView extends BorderPane {
@ -64,7 +67,6 @@ public class GoodsManagementView extends BorderPane {
Label idLabel = new Label("货物ID");
idField = new TextField();
idField.setPromptText("请输入货物ID");
idField.setEditable(false); // ID 不可编辑
Label nameLabel = new Label("货物名称:");
nameField = new TextField();
@ -119,7 +121,7 @@ public class GoodsManagementView extends BorderPane {
buttonBox.setAlignment(Pos.CENTER_RIGHT);
buttonBox.setPadding(new Insets(10, 0, 0, 0));
Button saveButton = new Button("修改");
Button saveButton = new Button("保存");
saveButton.setOnAction(e -> handleSave());
Button clearButton = new Button("清空");
@ -175,7 +177,34 @@ public class GoodsManagementView extends BorderPane {
}
private void loadGoodsData() {
List<Goods> goodsList = DataManager.getInstance().getAllGoods();
String url = "jdbc:sqlite:db/dbuml.db3";
List<Goods> goodsList = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(url)) {
String sql = "SELECT * FROM goods";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Goods goods = new Goods(
rs.getString("id"),
rs.getString("name"),
rs.getString("specification"),
rs.getString("unit"),
rs.getDouble("price"),
rs.getString("category"),
rs.getInt("quantity"),
rs.getString("remark")
);
goods.setInboundTime(LocalDateTime.parse(rs.getString("inbound_time")));
goods.setOperator(rs.getString("operator"));
goods.setSupplier(rs.getString("supplier"));
goodsList.add(goods);
}
}
} catch (Exception e) {
e.printStackTrace();
showAlert("错误", "加载货物信息失败:" + e.getMessage());
}
tableView.setItems(FXCollections.observableArrayList(goodsList));
}
@ -223,20 +252,24 @@ public class GoodsManagementView extends BorderPane {
// 查找货物对象
Goods existingGoods = DataManager.getInstance().getGoods(id);
if (existingGoods == null) {
showAlert("错误", "货物ID不存在");
return;
// 插入新货物
Goods newGoods = new Goods(id, name, specification, unit, price, category, quantity, "");
newGoods.setInboundTime(LocalDateTime.now());
newGoods.setOperator("当前用户");
DataManager.getInstance().addGoodsToDatabase(newGoods);
} else {
// 更新现有货物
existingGoods.setName(name);
existingGoods.setSpecification(specification);
existingGoods.setUnit(unit);
existingGoods.setPrice(price);
existingGoods.setCategory(category);
existingGoods.setQuantity(quantity);
DataManager.getInstance().updateGoodsInDatabase(existingGoods);
}
// 更新货物对象
existingGoods.setName(name);
existingGoods.setSpecification(specification);
existingGoods.setUnit(unit);
existingGoods.setPrice(price);
existingGoods.setCategory(category);
existingGoods.setQuantity(quantity);
// 更新表格
tableView.refresh();
loadGoodsData();
// 清空输入
handleClear();
@ -245,6 +278,7 @@ public class GoodsManagementView extends BorderPane {
} catch (Exception e) {
showAlert("错误", "保存失败:" + e.getMessage());
e.printStackTrace(); // 添加堆栈跟踪以便调试
}
}
@ -275,4 +309,6 @@ public class GoodsManagementView extends BorderPane {
alert.setContentText(content);
alert.showAndWait();
}
}

Loading…
Cancel
Save