基于数据库的商品分类系统
实体类
Good类
public class Good {
private Integer id;
private String name;
private double price;
@Override
public String toString() {
return
"商品编号:" + id +
"\t商品名:" + name +
"\t价格:" + price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Good(Integer id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
}
Gods类
public class Goods {
private Integer GoodsId;
private String GoodsType;
public Goods(Integer goodsId, String goodsType) {
GoodsId = goodsId;
GoodsType = goodsType;
}
@Override
public String toString() {
return
"分类编号:" + GoodsId +
"\t分类:" + GoodsType;
}
public Integer getGoodsId() {
return GoodsId;
}
public void setGoodsId(Integer goodsId) {
GoodsId = goodsId;
}
public String getGoodsType() {
return GoodsType;
}
public void setGoodsType(String goodsType) {
GoodsType = goodsType;
}
}
数据库文件
在对应的数据库下直接执行即可
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for good
-- ----------------------------
DROP TABLE IF EXISTS `good`;
CREATE TABLE `good` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`price` double NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of good
-- ----------------------------
INSERT INTO `good` VALUES (1, '生蚝', 33);
INSERT INTO `good` VALUES (2, '小青菜', 8);
INSERT INTO `good` VALUES (3, '火龙果', 15);
INSERT INTO `good` VALUES (4, '牛肉', 100);
INSERT INTO `good` VALUES (5, '速冻水饺', 10);
SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for goods
-- ----------------------------
DROP TABLE IF EXISTS `goods`;
CREATE TABLE `goods` (
`GoodsId` int(0) NOT NULL AUTO_INCREMENT COMMENT '商品编号',
`GoodsType` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品类别',
PRIMARY KEY (`GoodsId`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of goods
-- ----------------------------
INSERT INTO `goods` VALUES (1, '海鲜');
INSERT INTO `goods` VALUES (2, '蔬菜');
INSERT INTO `goods` VALUES (3, '水果');
INSERT INTO `goods` VALUES (4, '肉禽蛋');
INSERT INTO `goods` VALUES (5, '速冻');
SET FOREIGN_KEY_CHECKS = 1;
service类
public class GoodService {
List<Good> goodList = new ArrayList<>();
List<Goods> goodsList = new ArrayList<>();
PreparedStatement preparedStatement;
Connection connection;
ResultSet resultSet;
Good good;
Goods goods;
//全查询信息
public List<Good> selectGoodAll() throws Exception {
goodList.clear();
Connection connection = DBUtil.getConnection();
try {
String sql = "select * from good";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
double price = resultSet.getDouble("price");
Good good = new Good(id, name, price);
goodList.add(good);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
DBUtil.closeAll(preparedStatement, connection, resultSet);
return goodList;
}
//根据id查看
public Good findGoodById(int id1) throws Exception {
Connection connection = DBUtil.getConnection();
try {
String sql = "select * from good where id=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id1);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
double price = resultSet.getDouble("price");
Good good1 = new Good(id, name, price);
DBUtil.closeAll(preparedStatement, connection, resultSet);
return good1;
}
} catch (
Exception e) {
throw new RuntimeException(e);
}
return null;
}
//修改信息
public void updateGood(Good good1) throws Exception {
Connection connection = DBUtil.getConnection();
String sql = "update good set name=?,price=? where id=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(3, good1.getId());
preparedStatement.setString(1, good1.getName());
preparedStatement.setDouble(2, good1.getPrice());
int re = preparedStatement.executeUpdate();
DBUtil.closeAll(preparedStatement, connection, resultSet);
}
//添加信息
public void addGood(Good good1) throws Exception {
Connection connection = DBUtil.getConnection();
String sql = "insert into good values(null,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, good1.getName());
preparedStatement.setDouble(2, good1.getPrice());
int re = preparedStatement.executeUpdate();
DBUtil.closeAll(preparedStatement, connection, resultSet);
}
//删除信息
public void deleteGood(int id) throws Exception {
Connection connection = DBUtil.getConnection();
String sql = "delete from good where id=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
DBUtil.closeAll(preparedStatement, connection, resultSet);
}
//全查询商品分类
public List<Goods> findGoodsAll() throws Exception {
goodsList.clear();
Connection connection = DBUtil.getConnection();
try {
String sql = "select * from goods";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("GoodsId");
String name = resultSet.getString("GoodsType");
Goods goods1 = new Goods(id, name);
goodsList.add(goods1);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
DBUtil.closeAll(preparedStatement, connection, resultSet);
return goodsList;
}
//根据id查看商品分类
public Goods findGoodsById(int id) throws Exception {
Connection connection = DBUtil.getConnection();
try {
String sql = "select * from goods where GoodsId=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id1 = resultSet.getInt("GoodsId");
String name = resultSet.getString("GoodsType");
DBUtil.closeAll(preparedStatement, connection, resultSet);
Goods goods1 = new Goods(id1, name);
return goods1;
}
} catch (
Exception e) {
throw new RuntimeException(e);
}
return null;
}
//修改分类
public void updateGoods(Goods goods1) throws Exception {
Connection connection = DBUtil.getConnection();
String sql = "update goods set GoodsType=?where GoodsId=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(2, goods1.getGoodsId());
preparedStatement.setString(1, goods1.getGoodsType());
int re = preparedStatement.executeUpdate();
DBUtil.closeAll(preparedStatement, connection, resultSet);
}
//添加分类信息
public void addGoods(Goods goods1) throws Exception {
Connection connection = DBUtil.getConnection();
String sql = "insert into goods values(null,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, goods1.getGoodsType());
int re = preparedStatement.executeUpdate();
DBUtil.closeAll(preparedStatement, connection, resultSet);
}
//删除分类信息
public void deleteGoods(int id) throws Exception {
Connection connection = DBUtil.getConnection();
String sql = "delete from goods where GoodsId=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
DBUtil.closeAll(preparedStatement, connection, resultSet);
}
}
主菜单视图类
public class GoodDao {
boolean loop = true;
GoodService service = new GoodService();
//全查询超市信息
public void selectAll() throws Exception {
List<Good> list = service.selectGoodAll();
list.forEach(System.out::println);
}
//添加信息
public void addGood() throws Exception {
System.out.println("请输入要添加的商品名");
String name = Utility.readString(10);
System.out.println("请输入要添加的商品价格");
double price = Utility.readInt();
Good good1 = new Good(null, name, price);
service.addGood(good1);
System.out.println("添加商品成功");
}
//修改信息
public void updateGood() throws Exception {
System.out.println("请输入要修改的商品编号");
//请输入你要修改的商品id
int id = Utility.readInt();
Good carts = service.findGoodById(id);
if (carts == null) {
System.out.println("不存在改商品");
return;
}
System.out.println(carts);
System.out.println("请输入要修改的商品名(" + carts.getName() + ")");
String name = Utility.readString(10);
System.out.println("请输入要修改的商品价格(" + carts.getPrice() + ")");
int price = Utility.readInt();
Good carts1 = new Good(id, name, price);
service.updateGood(carts1);
System.out.println("修改信息成功");
}
//删除信息
public void deleteGood() throws Exception {
System.out.println("请输入要删除的商品的编号");
int id = Utility.readInt();
service.deleteGood(id);
System.out.println("删除成功");
}
//全查询分类信息
public void findGoodsAll() throws Exception {
List<Goods> list = service.findGoodsAll();
list.forEach(System.out::println);
}
//修改分类信息
public void updateGoods() throws Exception {
System.out.println("请输入要修改的分类id");
//请输入你要修改的商品id
int id = Utility.readInt();
Goods cart = service.findGoodsById(id);
if (cart == null) {
System.out.println("分类不存在");
return;
}
System.out.println(cart);
System.out.println("请输入要添加的类型");
String name = Utility.readString(10);
Goods cart1 = new Goods(id, name);
service.updateGoods(cart1);
System.out.println("修改成功");
}
//添加分类信息
public void addGoods() throws Exception {
System.out.println("请输入要添加的分类");
String name = Utility.readString(10);
Goods cart = new Goods(0, name);
service.addGoods(cart);
System.out.println("添加成功");
}
//删除分类信息
public void deleteGoods() throws Exception {
System.out.println("请输入要删除的分类的编号");
int id = Utility.readInt();
service.deleteGoods(id);
System.out.println("删除成功");
}
public void mainEu() throws Exception {
do {
System.out.println("--------------商品分类管理系统--------------");
System.out.println("\t\t\t1、商品信息管理");
System.out.println("\t\t\t2、分类信息管理");
System.out.println("\t\t\t3、退出系统");
System.out.println("请输入你的选择");
System.out.println("-------------------------------------------");
int key = Utility.readInt();
switch (key) {
case 1:
good();
break;
case 2:
goods();
break;
case 3:
loop = false;
break;
default:
System.out.println("输入有误,请重新输入");
break;
}
} while (loop);
System.out.println("您退出了整个系统");
}
public void good() throws Exception {
boolean loop1 = true;
do {
System.out.println("--------------商品分类管理系统--------------");
System.out.println("\t\t\t1、商品添加");
System.out.println("\t\t\t2、商品修改");
System.out.println("\t\t\t3、商品删除");
System.out.println("\t\t\t4、商品查看");
System.out.println("\t\t\t5、退出商品管理");
System.out.println("请输入命令来执行对应的操作");
System.out.println("-------------------------------------------");
int key = Utility.readInt();
switch (key) {
case 1:
addGood();
break;
case 2:
updateGood();
break;
case 3:
deleteGood();
break;
case 4:
selectAll();
break;
case 5:
loop1 = false;
break;
default:
System.out.println("输入有误");
break;
}
} while (loop1);
System.out.println("您退出了商品菜单");
}
public void goods() throws Exception {
boolean loop1 = true;
do {
System.out.println("--------------商品分类管理系统--------------");
System.out.println("\t\t\t1、分类添加");
System.out.println("\t\t\t2、分类修改");
System.out.println("\t\t\t3、分类删除");
System.out.println("\t\t\t4、分类查看");
System.out.println("\t\t\t5、退出分类管理");
System.out.println("请输入命令来执行对应的操作");
System.out.println("-------------------------------------------");
int key = Utility.readInt();
switch (key) {
case 1:
addGoods();
break;
case 2:
updateGoods();
break;
case 3:
deleteGoods();
break;
case 4:
findGoodsAll();
break;
case 5:
loop1 = false;
break;
default:
System.out.println("输入有误");
break;
}
} while (loop1);
System.out.println("您退出了商品菜单");
}
}
测试类
public class Test {
public static void main(String[] args) throws Exception {
GoodDao goodDao = new GoodDao();
goodDao.mainEu();
}
}
####运行截图示例