实现一个商品信息管理的原型系统,具有增删查改等基本功能,必须以Java语言实现。可以是字符界面或者图形 界面。 实验的完整步骤应该是首先创建数据库(可以参考教材398,399,407页的代码),然后编写程序。 任务描述: 首先创建一个名为webstore的数据库然后创建一张名为products的表然后可以手工往表里添加若干记录有了数据库和数据作为基础,可以开始如下实验: 功能需求描述:系统启动后显示如下字符界面的系统菜单(菜单宽度没有严格限制): ********请选择菜单********* * 1
时间: 2023-07-23 19:34:28 浏览: 95
好的,这是一个比较完整的Java实现商品信息管理系统的示例代码,包括了数据库创建、表创建、数据添加、增删查改等基本功能。界面采用字符界面。
```java
import java.sql.*;
import java.util.Scanner;
public class ProductManager {
private static final String URL = "jdbc:mysql://localhost:3306/";
private static final String DB_NAME = "webstore";
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String USER_NAME = "root";
private static final String PASSWORD = "password";
private static final String CREATE_DB_SQL = "CREATE DATABASE IF NOT EXISTS " + DB_NAME + " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
private static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS products (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50), price DOUBLE)";
private static final String INSERT_SQL = "INSERT INTO products (name, price) VALUES (?, ?)";
private static final String SELECT_ALL_SQL = "SELECT * FROM products";
private static final String SELECT_ONE_SQL = "SELECT * FROM products WHERE id = ?";
private static final String UPDATE_SQL = "UPDATE products SET name = ?, price = ? WHERE id = ?";
private static final String DELETE_SQL = "DELETE FROM products WHERE id = ?";
private Scanner scanner;
private Connection connection;
private PreparedStatement insertStatement;
private PreparedStatement selectOneStatement;
private PreparedStatement updateStatement;
private PreparedStatement deleteStatement;
public ProductManager() {
scanner = new Scanner(System.in);
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL + DB_NAME, USER_NAME, PASSWORD);
createDatabase();
createTable();
insertStatement = connection.prepareStatement(INSERT_SQL);
selectOneStatement = connection.prepareStatement(SELECT_ONE_SQL);
updateStatement = connection.prepareStatement(UPDATE_SQL);
deleteStatement = connection.prepareStatement(DELETE_SQL);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
private void createDatabase() throws SQLException {
Statement statement = connection.createStatement();
statement.execute(CREATE_DB_SQL);
statement.close();
}
private void createTable() throws SQLException {
Statement statement = connection.createStatement();
statement.execute(CREATE_TABLE_SQL);
statement.close();
}
private void printMenu() {
System.out.println("********请选择菜单*********");
System.out.println("* 1.添加商品信息 *");
System.out.println("* 2.查询所有商品信息 *");
System.out.println("* 3.查询单个商品信息 *");
System.out.println("* 4.修改商品信息 *");
System.out.println("* 5.删除商品信息 *");
System.out.println("* 0.退出系统 *");
System.out.println("*****************************");
}
private void addProduct() throws SQLException {
System.out.println("请输入商品名称:");
String name = scanner.nextLine();
System.out.println("请输入商品价格:");
double price = Double.parseDouble(scanner.nextLine());
insertStatement.setString(1, name);
insertStatement.setDouble(2, price);
int result = insertStatement.executeUpdate();
if (result > 0) {
System.out.println("添加商品信息成功!");
} else {
System.out.println("添加商品信息失败!");
}
}
private void listAllProducts() throws SQLException {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SELECT_ALL_SQL);
System.out.println("所有商品信息如下:");
System.out.println("编号\t名称\t价格");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
double price = resultSet.getDouble("price");
System.out.println(id + "\t" + name + "\t" + price);
}
statement.close();
}
private void queryProduct() throws SQLException {
System.out.println("请输入要查询的商品编号:");
int id = Integer.parseInt(scanner.nextLine());
selectOneStatement.setInt(1, id);
ResultSet resultSet = selectOneStatement.executeQuery();
if (resultSet.next()) {
String name = resultSet.getString("name");
double price = resultSet.getDouble("price");
System.out.println("编号\t名称\t价格");
System.out.println(id + "\t" + name + "\t" + price);
} else {
System.out.println("没有找到该商品信息!");
}
}
private void updateProduct() throws SQLException {
System.out.println("请输入要修改的商品编号:");
int id = Integer.parseInt(scanner.nextLine());
selectOneStatement.setInt(1, id);
ResultSet resultSet = selectOneStatement.executeQuery();
if (resultSet.next()) {
String name = resultSet.getString("name");
double price = resultSet.getDouble("price");
System.out.println("请输入新的商品名称(原来的名称为:" + name + "):");
String newName = scanner.nextLine();
System.out.println("请输入新的商品价格(原来的价格为:" + price + "):");
double newPrice = Double.parseDouble(scanner.nextLine());
updateStatement.setString(1, newName);
updateStatement.setDouble(2, newPrice);
updateStatement.setInt(3, id);
int result = updateStatement.executeUpdate();
if (result > 0) {
System.out.println("修改商品信息成功!");
} else {
System.out.println("修改商品信息失败!");
}
} else {
System.out.println("没有找到该商品信息!");
}
}
private void deleteProduct() throws SQLException {
System.out.println("请输入要删除的商品编号:");
int id = Integer.parseInt(scanner.nextLine());
deleteStatement.setInt(1, id);
int result = deleteStatement.executeUpdate();
if (result > 0) {
System.out.println("删除商品信息成功!");
} else {
System.out.println("删除商品信息失败!");
}
}
public void run() throws SQLException {
while (true) {
printMenu();
System.out.println("请输入菜单编号:");
int choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1:
addProduct();
break;
case 2:
listAllProducts();
break;
case 3:
queryProduct();
break;
case 4:
updateProduct();
break;
case 5:
deleteProduct();
break;
case 0:
System.out.println("谢谢使用,再见!");
return;
default:
System.out.println("输入的菜单编号不正确,请重新输入!");
break;
}
}
}
public static void main(String[] args) throws SQLException {
ProductManager manager = new ProductManager();
manager.run();
}
}
```
注意,这里的代码仅仅是一个示例,实际操作中可能还需要进行更多的异常处理和边界条件判断。同时,这里只是采用了字符界面来实现,如果需要图形界面可以使用Swing或JavaFX等GUI框架。
阅读全文