用java连接数据库写一个有登录页面进入商品库存管理(需要增删改查中文注释)
时间: 2024-02-05 19:04:26 浏览: 24
好的,我可以为您提供一个简单的Java登录页面,并且演示如何用Java连接MySQL数据库并实现商品库存管理。以下是详细步骤:
1. 创建Java项目并添加MySQL连接器
首先,您需要创建一个Java项目,在该项目中添加 MySQL 连接器。您可以在此处下载最新版本的 MySQL Connector/J:https://dev.mysql.com/downloads/connector/j/
将下载的 JAR 文件添加到您的项目的classpath中。
2. 创建登录页面
在您的 Java 项目中,创建一个登录页面。您可以使用Swing或JavaFX创建登录页面。
下面是一个简单的Swing登录页面示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener {
JLabel l1, l2, l3;
JTextField tf1;
JPasswordField pf2;
JButton b1, b2;
Login() {
setTitle("登录");
setSize(300, 200);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("用户名:");
l1.setBounds(40, 20, 100, 20);
add(l1);
tf1 = new JTextField();
tf1.setBounds(150, 20, 100, 20);
add(tf1);
l2 = new JLabel("密码:");
l2.setBounds(40, 50, 100, 20);
add(l2);
pf2 = new JPasswordField();
pf2.setBounds(150, 50, 100, 20);
add(pf2);
b1 = new JButton("登录");
b1.setBounds(40, 100, 80, 30);
b1.addActionListener(this);
add(b1);
b2 = new JButton("取消");
b2.setBounds(150, 100, 80, 30);
b2.addActionListener(this);
add(b2);
}
public void actionPerformed(ActionEvent ae) {
String uname = tf1.getText();
String pass = pf2.getText();
if (ae.getSource() == b1) {
if (uname.equals("admin") && pass.equals("admin")) {
JOptionPane.showMessageDialog(this, "登录成功");
dispose();
new InventoryManagement();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误");
}
} else {
dispose();
}
}
public static void main(String[] args) {
new Login().setVisible(true);
}
}
```
3. 创建商品库存管理页面
创建一个新的 Java 类,名为 InventoryManagement,该类将显示商品库存管理界面。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InventoryManagement extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6;
JTextField tf1, tf2, tf3, tf4, tf5;
JButton b1, b2, b3, b4;
JTextArea ta;
InventoryManagement() {
setTitle("商品库存管理");
setSize(500, 400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("商品编号:");
l1.setBounds(20, 20, 100, 20);
add(l1);
tf1 = new JTextField();
tf1.setBounds(120, 20, 100, 20);
add(tf1);
l2 = new JLabel("商品名称:");
l2.setBounds(20, 50, 100, 20);
add(l2);
tf2 = new JTextField();
tf2.setBounds(120, 50, 100, 20);
add(tf2);
l3 = new JLabel("商品规格:");
l3.setBounds(20, 80, 100, 20);
add(l3);
tf3 = new JTextField();
tf3.setBounds(120, 80, 100, 20);
add(tf3);
l4 = new JLabel("商品单价:");
l4.setBounds(20, 110, 100, 20);
add(l4);
tf4 = new JTextField();
tf4.setBounds(120, 110, 100, 20);
add(tf4);
l5 = new JLabel("商品数量:");
l5.setBounds(20, 140, 100, 20);
add(l5);
tf5 = new JTextField();
tf5.setBounds(120, 140, 100, 20);
add(tf5);
b1 = new JButton("新增");
b1.setBounds(20, 180, 80, 30);
b1.addActionListener(this);
add(b1);
b2 = new JButton("查询");
b2.setBounds(110, 180, 80, 30);
b2.addActionListener(this);
add(b2);
b3 = new JButton("修改");
b3.setBounds(200, 180, 80, 30);
b3.addActionListener(this);
add(b3);
b4 = new JButton("删除");
b4.setBounds(290, 180, 80, 30);
b4.addActionListener(this);
add(b4);
ta = new JTextArea();
ta.setBounds(20, 220, 440, 120);
add(ta);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String cmd = ae.getActionCommand();
if (cmd.equals("新增")) {
addProduct();
} else if (cmd.equals("查询")) {
searchProduct();
} else if (cmd.equals("修改")) {
updateProduct();
} else if (cmd.equals("删除")) {
deleteProduct();
}
}
private void addProduct() {
String id = tf1.getText();
String name = tf2.getText();
String spec = tf3.getText();
double price = Double.parseDouble(tf4.getText());
int quantity = Integer.parseInt(tf5.getText());
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
// 执行插入语句
PreparedStatement ps = conn.prepareStatement("INSERT INTO products (id, name, spec, price, quantity) VALUES (?, ?, ?, ?, ?)");
ps.setString(1, id);
ps.setString(2, name);
ps.setString(3, spec);
ps.setDouble(4, price);
ps.setInt(5, quantity);
ps.executeUpdate();
ta.setText("新增商品成功");
} catch (Exception e) {
e.printStackTrace();
ta.setText("新增商品失败:" + e.getMessage());
}
}
private void searchProduct() {
String id = tf1.getText();
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
// 执行查询语句
PreparedStatement ps = conn.prepareStatement("SELECT * FROM products WHERE id = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
tf2.setText(rs.getString("name"));
tf3.setText(rs.getString("spec"));
tf4.setText(Double.toString(rs.getDouble("price")));
tf5.setText(Integer.toString(rs.getInt("quantity")));
ta.setText("");
} else {
ta.setText("没有找到编号为 " + id + " 的商品");
}
} catch (Exception e) {
e.printStackTrace();
ta.setText("查询商品失败:" + e.getMessage());
}
}
private void updateProduct() {
String id = tf1.getText();
String name = tf2.getText();
String spec = tf3.getText();
double price = Double.parseDouble(tf4.getText());
int quantity = Integer.parseInt(tf5.getText());
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
// 执行更新语句
PreparedStatement ps = conn.prepareStatement("UPDATE products SET name = ?, spec = ?, price = ?, quantity = ? WHERE id = ?");
ps.setString(1, name);
ps.setString(2, spec);
ps.setDouble(3, price);
ps.setInt(4, quantity);
ps.setString(5, id);
int result = ps.executeUpdate();
if (result > 0) {
ta.setText("修改商品成功");
} else {
ta.setText("没有找到编号为 " + id + " 的商品");
}
} catch (Exception e) {
e.printStackTrace();
ta.setText("修改商品失败:" + e.getMessage());
}
}
private void deleteProduct() {
String id = tf1.getText();
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
// 执行删除语句
PreparedStatement ps = conn.prepareStatement("DELETE FROM products WHERE id = ?");
ps.setString(1, id);
int result = ps.executeUpdate();
if (result > 0) {
ta.setText("删除商品成功");
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
} else {
ta.setText("没有找到编号为 " + id + " 的商品");
}
} catch (Exception e) {
e.printStackTrace();
ta.setText("删除商品失败:" + e.getMessage());
}
}
public static void main(String[] args) {
new InventoryManagement();
}
}
```
4. 创建MySQL数据库
在您的MySQL数据库中,创建一个名为“inventory”的新数据库,并创建一个名为“products”的新表,该表包含以下列:
- id:VARCHAR
- name:VARCHAR
- spec:VARCHAR
- price:DOUBLE
- quantity:INT
```sql
CREATE DATABASE inventory;
USE inventory;
CREATE TABLE products (
id VARCHAR(10) PRIMARY KEY,
name VARCHAR(50),
spec VARCHAR(50),
price DOUBLE,
quantity INT
);
```
现在您可以编译并运行您的Java程序,以查看它是否可以连接到MySQL数据库并实现商品库存管理了。