然后再使用JUI图形界面来实现
时间: 2024-03-03 21:52:37 浏览: 76
好的,使用JUI图形界面可以更方便地管理和操作数据库。我们可以使用Swing库来实现简单的图形界面,并将其与JDBC代码结合起来实现仓库管理系统。
以下是一个示例代码,实现了添加货物和查询货物信息两个功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class WarehouseManagementSystemGUI extends JFrame implements ActionListener {
// JDBC驱动器名称和数据库URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/warehouse";
// 数据库凭据
static final String USER = "root";
static final String PASS = "password";
// 组件
private JLabel nameLabel, quantityLabel, priceLabel;
private JTextField nameText, quantityText, priceText;
private JButton addButton, searchButton;
private JTextArea resultArea;
public WarehouseManagementSystemGUI() {
// 初始化组件
nameLabel = new JLabel("Name:");
nameText = new JTextField(20);
quantityLabel = new JLabel("Quantity:");
quantityText = new JTextField(20);
priceLabel = new JLabel("Price:");
priceText = new JTextField(20);
addButton = new JButton("Add");
addButton.addActionListener(this);
searchButton = new JButton("Search");
searchButton.addActionListener(this);
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
// 设置布局
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameText);
inputPanel.add(quantityLabel);
inputPanel.add(quantityText);
inputPanel.add(priceLabel);
inputPanel.add(priceText);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(searchButton);
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.add(new JScrollPane(resultArea), BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(resultPanel, BorderLayout.SOUTH);
// 添加到窗口
setContentPane(mainPanel);
setTitle("Warehouse Management System");
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 添加货物
String name = nameText.getText();
int quantity = Integer.parseInt(quantityText.getText());
double price = Double.parseDouble(priceText.getText());
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO goods (name, quantity, price) VALUES (?, ?, ?)")) {
stmt.setString(1, name);
stmt.setInt(2, quantity);
stmt.setDouble(3, price);
stmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Added successfully.");
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
}
} else if (e.getSource() == searchButton) {
// 查询货物信息
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("SELECT * FROM goods");
resultArea.setText("");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int quantity = rs.getInt("quantity");
double price = rs.getDouble("price");
resultArea.append(String.format("ID: %d, Name: %s, Quantity: %d, Price: %.2f\n", id, name, quantity, price));
}
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
new WarehouseManagementSystemGUI();
}
}
```
上面的代码实现了一个简单的添加货物和查询货物信息的界面,用户可以在文本框中输入货物的信息,点击按钮即可执行相应的操作。您可以根据自己的需求来修改其中的代码,添加更多功能,例如删除、修改货物信息等。
阅读全文