Merge pull request '登录页面,库存管理页面' (#1) from lcm into master
Reviewed-on: #1
This commit is contained in:
BIN
lib/mysql-connector-j-8.4.0.jar
Normal file
BIN
lib/mysql-connector-j-8.4.0.jar
Normal file
Binary file not shown.
48
src/main/java/com/example/Home.java
Normal file
48
src/main/java/com/example/Home.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.example;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class Home extends JFrame implements ActionListener {
|
||||
|
||||
JMenuBar menuBar;
|
||||
JMenu menu;
|
||||
// 库存菜单 进货菜单 销售菜单
|
||||
JMenuItem StockMenuItem,InventoryMenuItem,SalesMenuItem;
|
||||
|
||||
Home() {
|
||||
super("城市进销存信息管理系统");
|
||||
menuBar = new JMenuBar();
|
||||
menu = new JMenu("菜单");
|
||||
StockMenuItem = new JMenuItem("库存信息管理");
|
||||
InventoryMenuItem = new JMenuItem("进货信息管理");
|
||||
SalesMenuItem = new JMenuItem("销售信息管理");
|
||||
menu.add(StockMenuItem);
|
||||
menu.add(InventoryMenuItem);
|
||||
menu.add(SalesMenuItem);
|
||||
menuBar.add(menu);
|
||||
setJMenuBar(menuBar);
|
||||
StockMenuItem.addActionListener(this);
|
||||
InventoryMenuItem.addActionListener(this);
|
||||
SalesMenuItem.addActionListener(this);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100,100,500,500);
|
||||
setVisible(true);
|
||||
}
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(e.getSource()==StockMenuItem){
|
||||
new StockProductManagement();
|
||||
this.setVisible(false);
|
||||
}
|
||||
if(e.getSource()==InventoryMenuItem){
|
||||
new InventoryProductManagement();
|
||||
this.setVisible(false);
|
||||
}
|
||||
if(e.getSource()==SalesMenuItem){
|
||||
new SalesProductManagement();
|
||||
this.setVisible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/main/java/com/example/Jdbc.java
Normal file
41
src/main/java/com/example/Jdbc.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.example;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
public class Jdbc {
|
||||
private Connection conn;
|
||||
private Statement stmt;
|
||||
private ResultSet rs;
|
||||
|
||||
Jdbc(){
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
String password = "root";
|
||||
String username = "root";
|
||||
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/su?useSSL=false&serverTimezone=UTC", username, password);
|
||||
stmt=conn.createStatement();
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet query(String sql) throws SQLException {
|
||||
rs=stmt.executeQuery(sql);
|
||||
return rs;
|
||||
}
|
||||
|
||||
public void update(String sql) throws SQLException {
|
||||
stmt.executeUpdate(sql);
|
||||
}
|
||||
public void close() throws SQLException {
|
||||
if(rs!=null){
|
||||
rs.close();
|
||||
}
|
||||
if(stmt!=null){
|
||||
stmt.close();
|
||||
}
|
||||
if(conn!=null){
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,39 +4,66 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Random;
|
||||
|
||||
public class Main extends JFrame implements ActionListener {
|
||||
|
||||
JButton jb1,jb2,jb3;
|
||||
|
||||
JButton jb1,jb2;
|
||||
JTextField tf1,tf3;
|
||||
JPasswordField tf2;
|
||||
JLabel jl1,jl2,jl3,jl4,jl5;
|
||||
Random rand = new Random();
|
||||
Main(){
|
||||
super("超市进销存信息管理系统");
|
||||
jb1=new JButton("进货商品信息");
|
||||
jb2=new JButton("销售商品信息");
|
||||
jb3=new JButton("库存商品信息");
|
||||
super("登录");
|
||||
jb1=new JButton("登录");
|
||||
jb2=new JButton("取消");
|
||||
tf1=new JTextField(6);
|
||||
tf2=new JPasswordField(6);
|
||||
tf3=new JTextField(6);
|
||||
jl1=new JLabel("用户名",JLabel.CENTER);
|
||||
jl2=new JLabel("密码",JLabel.CENTER);
|
||||
jl3=new JLabel("验证码",JLabel.CENTER);
|
||||
jl4=new JLabel("验证码:",JLabel.CENTER);
|
||||
jl5=new JLabel("",JLabel.CENTER);
|
||||
jb1.addActionListener(this);
|
||||
jb2.addActionListener(this);
|
||||
jb3.addActionListener(this);
|
||||
setLayout(new GridLayout(3,3));
|
||||
setLayout(new GridLayout(5,2));
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
add(jb1);
|
||||
add(jb2);
|
||||
add(jb3);
|
||||
setBounds(100,100,500,500);
|
||||
add(jl1);add(tf1);
|
||||
add(jl2);add(tf2);
|
||||
add(jl3);add(tf3);
|
||||
add(jl4);add(jl5);
|
||||
add(jb1);add(jb2);
|
||||
jl5.setText(Integer.toString(rand.nextInt(100)+100));
|
||||
setBounds(500,100,500,500);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
if(e.getSource()==jb1){
|
||||
new InventoryProductManagement();//进入进货商品信息管理系统
|
||||
setVisible(false);
|
||||
Jdbc jdbc = new Jdbc();
|
||||
String sql = "select * from user where username='"+tf1.getText()+"' and password='"+tf2.getText()+"'"; ;
|
||||
try {
|
||||
ResultSet rs = jdbc.query(sql);
|
||||
jdbc.close();
|
||||
if(rs.next()&&tf3.getText().equals(jl5.getText())) {
|
||||
JOptionPane.showMessageDialog(null, "登录成功");
|
||||
new Home();
|
||||
this.setVisible(false);
|
||||
}
|
||||
else{
|
||||
JOptionPane.showMessageDialog(null,"登录失败");
|
||||
jl5.setText(Integer.toString(rand.nextInt(100)+100));
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}else if(e.getSource()==jb2){
|
||||
new SalesProductManagement();//进入销售商品信息管理系统
|
||||
setVisible(false);
|
||||
}else if(e.getSource()==jb3){
|
||||
new StockProductManagement();//进入库存商品信息管理系统
|
||||
setVisible(false);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,176 @@
|
||||
package com.example;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class StockProductManagement extends JFrame implements ActionListener {
|
||||
|
||||
JMenuBar menuBar;
|
||||
JMenu menu;
|
||||
JMenuItem menuItem1,menuItem2,menuItem3;
|
||||
private JTable table;
|
||||
private final DefaultTableModel tableModel;
|
||||
StockProductManagement() {
|
||||
super("库存商品信息管理");
|
||||
menuBar = new JMenuBar();
|
||||
setJMenuBar(menuBar);
|
||||
menu = new JMenu("菜单");
|
||||
menuBar.add(menu);
|
||||
menuItem1 = new JMenuItem("添加商品");
|
||||
menuItem2 = new JMenuItem("删除商品");
|
||||
menu.add(menuItem1);
|
||||
menu.add(menuItem2);
|
||||
// menu.add(menuItem3);
|
||||
menuItem1.addActionListener(this);
|
||||
menuItem2.addActionListener(this);
|
||||
// menuItem3.addActionListener(this);
|
||||
setBounds(100,100,500,500);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// 表头
|
||||
String[] columnNames = {"商品名称", "商品数量", "进货单价", "销售单价", "单个利润", "已售数量"};
|
||||
tableModel = new DefaultTableModel(columnNames, 0);
|
||||
table = new JTable(tableModel);
|
||||
|
||||
// 滚动面板
|
||||
JScrollPane scrollPane = new JScrollPane(table);
|
||||
add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
// 初始化加载数据库数据
|
||||
loadData();
|
||||
setVisible(true);
|
||||
}
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (e.getSource() == menuItem1) {
|
||||
JFrame frame = new JFrame();
|
||||
frame.setTitle("添加商品");
|
||||
frame.setLayout(new GridLayout(5, 2));
|
||||
JLabel nameLabel = new JLabel("商品名称:");
|
||||
JTextField nameField = new JTextField();
|
||||
JLabel countLabel = new JLabel("商品数量:");
|
||||
JTextField countField = new JTextField();
|
||||
JLabel purchasePriceLabel = new JLabel("进货单价:");
|
||||
JTextField purchasePriceField = new JTextField();
|
||||
JLabel sellingPriceLabel = new JLabel("销售单价:");
|
||||
JTextField sellingPriceField = new JTextField();
|
||||
JButton addButton = new JButton("添加");
|
||||
JButton cancelButton = new JButton("取消");
|
||||
frame.add(nameLabel);
|
||||
frame.add(nameField);
|
||||
frame.add(countLabel);
|
||||
frame.add(countField);
|
||||
frame.add(purchasePriceLabel);
|
||||
frame.add(purchasePriceField);
|
||||
frame.add(sellingPriceLabel);
|
||||
frame.add(sellingPriceField);
|
||||
frame.add(addButton);
|
||||
frame.add(cancelButton);
|
||||
addButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String name = nameField.getText();
|
||||
int count = Integer.parseInt(countField.getText());
|
||||
double purchasePrice = Double.parseDouble(purchasePriceField.getText());
|
||||
double sellingPrice = Double.parseDouble(sellingPriceField.getText());
|
||||
// 调用数据库添加方法
|
||||
Jdbc jdbc = new Jdbc();
|
||||
try {
|
||||
jdbc.update("INSERT INTO commodity (name, count, purchase_price, selling_price, profit, quantity_sold) VALUES ('" + name + "', " + count + ", " + purchasePrice + ", " + sellingPrice + ", " + (sellingPrice - purchasePrice) + ", 0)");
|
||||
jdbc.close();
|
||||
// 刷新数据
|
||||
loadData();
|
||||
// 关闭弹窗
|
||||
frame.dispose();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
JOptionPane.showMessageDialog(frame, "添加商品失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
cancelButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
frame.dispose();
|
||||
}
|
||||
});
|
||||
frame.setSize(300, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
else if (e.getSource() == menuItem2) {
|
||||
JFrame frame = new JFrame();
|
||||
frame.setTitle("删除商品");
|
||||
frame.setLayout(new GridLayout(2, 2));
|
||||
JLabel nameLabel = new JLabel("商品名称:");
|
||||
JTextField nameField = new JTextField();
|
||||
JButton deleteButton = new JButton("删除");
|
||||
JButton cancelButton = new JButton("取消");
|
||||
frame.add(nameLabel);
|
||||
frame.add(nameField);
|
||||
frame.add(deleteButton);
|
||||
frame.add(cancelButton);
|
||||
deleteButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String name = nameField.getText();
|
||||
// 调用数据库删除方法
|
||||
Jdbc jdbc = new Jdbc();
|
||||
try {
|
||||
jdbc.update("DELETE FROM commodity WHERE name='" + name + "'");
|
||||
jdbc.close();
|
||||
// 刷新数据
|
||||
loadData();
|
||||
// 关闭弹窗
|
||||
frame.dispose();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
JOptionPane.showMessageDialog(frame, "删除商品失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
cancelButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
frame.dispose();
|
||||
}
|
||||
});
|
||||
frame.setSize(300, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadData() {
|
||||
Jdbc jdbc = new Jdbc();
|
||||
try {
|
||||
ResultSet rs = jdbc.query("SELECT name,count,purchase_price,selling_price,profit,quantity_sold FROM commodity");
|
||||
// 清空旧数据
|
||||
tableModel.setRowCount(0);
|
||||
// 填充新数据
|
||||
while (rs.next()) {
|
||||
Object[] row = {
|
||||
rs.getString("name"),
|
||||
rs.getInt("count"),
|
||||
rs.getDouble("purchase_price"),
|
||||
rs.getDouble("selling_price"),
|
||||
rs.getDouble("profit"),
|
||||
rs.getInt("quantity_sold")
|
||||
};
|
||||
tableModel.addRow(row);
|
||||
}
|
||||
rs.close();
|
||||
jdbc.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
JOptionPane.showMessageDialog(this, "加载数据失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
new StockProductManagement();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user