使用java+sql用登录注册页面写商品库存管理系统(功能中文注释)
时间: 2023-12-13 18:04:13 浏览: 33
好的,我可以为您提供一些指导。以下是一个简单的示例:
1. 登录页面
用户输入用户名和密码,系统将其与数据库中的信息进行比较。如果用户名和密码匹配,则用户被授权访问商品库存管理系统。
2. 注册页面
用户输入用户名、密码和其他必要的信息,然后将其保存到数据库中。
3. 商品添加页面
用户输入商品的名称、数量、价格等信息,然后将其保存到数据库中。
4. 商品删除页面
用户选择要删除的商品,然后将其从数据库中删除。
5. 商品编辑页面
用户选择要编辑的商品,然后输入新的信息并将其保存到数据库中。
6. 商品查询页面
用户输入查询条件(如商品名称、价格范围等),系统将其与数据库中的信息进行比较,并返回相应的结果。
以下是一个示例代码实现:
```
public class Product {
private int id;
private String name;
private int quantity;
private double price;
// 构造方法
public Product(int id, String name, int quantity, double price) {
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
}
// getter 和 setter 方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class ProductDAO {
private Connection connection;
// 构造方法
public ProductDAO(Connection connection) {
this.connection = connection;
}
// 添加商品
public void addProduct(Product product) throws SQLException {
String sql = "INSERT INTO products (name, quantity, price) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, product.getName());
statement.setInt(2, product.getQuantity());
statement.setDouble(3, product.getPrice());
statement.executeUpdate();
}
// 删除商品
public void deleteProduct(int id) throws SQLException {
String sql = "DELETE FROM products WHERE id=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
statement.executeUpdate();
}
// 更新商品
public void updateProduct(Product product) throws SQLException {
String sql = "UPDATE products SET name=?, quantity=?, price=? WHERE id=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, product.getName());
statement.setInt(2, product.getQuantity());
statement.setDouble(3, product.getPrice());
statement.setInt(4, product.getId());
statement.executeUpdate();
}
// 查询商品
public List<Product> getProducts(String name, double minPrice, double maxPrice) throws SQLException {
String sql = "SELECT * FROM products WHERE name LIKE ? AND price >= ? AND price <= ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, "%" + name + "%");
statement.setDouble(2, minPrice);
statement.setDouble(3, maxPrice);
ResultSet resultSet = statement.executeQuery();
List<Product> products = new ArrayList<>();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String productName = resultSet.getString("name");
int quantity = resultSet.getInt("quantity");
double price = resultSet.getDouble("price");
Product product = new Product(id, productName, quantity, price);
products.add(product);
}
return products;
}
}
public class Login {
public static void main(String[] args) throws SQLException {
// 连接到数据库
String url = "jdbc:mysql://localhost:3306/productsdb";
String username = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
// 创建 ProductDAO 对象
ProductDAO productDAO = new ProductDAO(connection);
// 添加商品
Product product1 = new Product(1, "苹果", 10, 5.0);
productDAO.addProduct(product1);
// 查询商品
List<Product> products = productDAO.getProducts("苹果", 0.0, 10.0);
for (Product product : products) {
System.out.println(product.getName() + " " + product.getQuantity() + " " + product.getPrice());
}
// 更新商品
Product product2 = new Product(1, "苹果", 20, 6.0);
productDAO.updateProduct(product2);
// 查询商品
products = productDAO.getProducts("苹果", 0.0, 10.0);
for (Product product : products) {
System.out.println(product.getName() + " " + product.getQuantity() + " " + product.getPrice());
}
// 删除商品
productDAO.deleteProduct(1);
// 查询商品
products = productDAO.getProducts("苹果", 0.0, 10.0);
for (Product product : products) {
System.out.println(product.getName() + " " + product.getQuantity() + " " + product.getPrice());
}
// 关闭连接
connection.close();
}
}
```
以上代码是一个简单的示例,您可以根据自己的实际需求进行修改和扩展。