使用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(); } } ``` 以上代码是一个简单的示例,您可以根据自己的实际需求进行修改和扩展。

相关推荐

最新推荐

recommend-type

基于Java+sql教师信息管理系统课设报告

基于Java+sql教师信息管理系统课设报告,教师信息管理系统主要用于实现用户名的登陆,退出,教师信息的添加,查找, 删除,修改等功能。本系统的开发需要实现以下功能模块:对系统的图形界面化进行 操作,对sql...
recommend-type

商品库存管理系统(论文).docx

目前我国大部分商品库存管理系统信息处理基本上还停留在手工方式,劳动强度大且工作效率低,消费者和管理人员的大量时间都消耗在事务性工作上,致使"人不能尽其才";同时计算机网络技术高速发展,网络管理运用也变得...
recommend-type

java使用JDBC动态创建数据表及SQL预处理的方法

主要介绍了java使用JDBC动态创建数据表及SQL预处理的方法,涉及JDBC操作数据库的连接、创建表、添加数据、查询等相关实现技巧,需要的朋友可以参考下
recommend-type

学生信息管理系统 Java+sql server

很好的实训资料,便于我们实训Java的时候参考,是一个很不错的Java综合编程
recommend-type

JAVA使用JDBC技术操作SqlServer数据库实例代码

本篇文章主要介绍了JAVA使用JDBC技术操作SqlServer数据库实例代码,具有一定的参考价值,有兴趣的可以了解一下。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。