使用JDBC作为数据库管理工具,做一个控制台模拟前端的简单外卖系统。 ## 一、设计数据库 #### 1、数据库结构 | No | 表名称 | 中文名 | 说明 | | -- | -------- | -------- | -------------------------------- | | 1 | business | 商家表 | 存储所有商家信息 | | 2 | food | 食品表 | 存储每个商家所拥有的所有食品信息 | | 3 | admin | 管理员表 | 存储管理员信息 | #### 2、表结构 约束类型标识: - PK:primary key 主键 - FK:foreign key 外键 - NN:not null 非空 - UQ:unique 唯一索引 - AI:auto increment 自增 ##### business(商家表) | No | 字段名 | 数据类型 | size | 默认値 | 约束 | 说明 | | -- | ------------- | -------- | ----- | ------ | ---------- | -------- | | 1 | bid | int | | | PK、AI、NN | 商家编号 | | 2 | password | varchar | 20 | | NN | 密码 | | 3 | bname | varchar | 40 | | NN | 商家名称 | | 4 | address | varchar | 50 | | | 商家地址 | | 5 | explain | varchar | 100 | | | 商家介绍 | | 6 | starPrice | decimal | (5,2) | 0.00 | | 起送费 | | 7 | deliveryPrice | decimal | (5,2) | 0.00 | | 配送费 | ##### food(食品表) | No | 字段名 | 数据类型 | size | 默认値 | 约束 | 说明 | | -- | ------- | -------- | ----- | ------ | ---------- | ------------ | | 1 | fid | int | | | PK、AI、NN | 食品编号 | | 2 | fname | varchar | 30 | | NN | 食品名称 | | 3 | explain | varchar | 30 | | | 食品介绍 | | 4 | price | decimal | (5,2) | | NN | 食品价格 | | 5 | bid | int | | | FK、NN | 所属商家编号 | ##### admin(管理员表) | No | 字段名 | 数据类型 | size | 默认値 | 约束 | 说明 | | -- | -------- | -------- | ---- | ------ | ---------- | ---------- | | 1 | aid | int | | | PK、AI、NN | 管理员编号 | | 2 | aname | varchar | 20 | | NN、UQ | 管理员名称 | | 3 | password | varchar | 20 | | NN | 密码 | #### 3、SQL语句 ```SQL CREATE DATABASE IF NOT EXISTS elm CHARSET utf8; USE elm; CREATE TABLE IF NOT EXISTS business ( bid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '商家编号', password VARCHAR(20) NOT NULL COMMENT '密码', bname VARCHAR(40) NOT NULL COMMENT '商家名称', address VARCHAR(50) COMMENT '商家地址', `explain` VARCHAR(100) COMMENT '商家介绍', startPrice DECIMAL(5, 2) DEFAULT 0.00 COMMENT '起送费', deliveryPrice DECIMAL(5, 2) DEFAULT 0.00 COMMENT '配送费' ); CREATE TABLE IF NOT EXISTS food ( fid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '食品编号', fname VARCHAR(30) NOT NULL COMMENT '食品名称', `explain` VARCHAR(30) COMMENT '食品介绍', price DECIMAL(5, 2) NOT NULL COMMENT '食品价格', bid INT UNSIGNED NOT NULL COMMENT '所属商家编号', FOREIGN KEY (bid) REFERENCES business (bid) ); CREATE TABLE IF NOT EXISTS admin ( aid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '管理员编号', aname VARCHAR(20) NOT NULL UNIQUE COMMENT '管理员名称', password VARCHAR(20) NOT NULL COMMENT '密码' ); INSERT INTO admin(aname, `password`) VALUES ('admin', 'admin'); ``` ## 二、添加JDBC库 在根目录下新建`lib`文件夹,将`mysqlconnectorjava5.1.10bin.jar`存放进去,导入仓库。 [香肠狗网盘](https://pan.soki.me/s/vrUj) ## 三、创建项目各层 #### 1、`po`层 新建`com.doooog.elm.po`包,创建`Admin.java`类 ```java package com.doooog.elm.po; public class Admin { private Integer aid; private String aname; private String password; public Admin() { } public Admin(Integer aid, String aname, String password) { this.aid = aid; this.aname = aname; this.password = password; } @Override public String toString() { return "Admin{" + "aid=" + aid + ", aname='" + aname + '\'' + ", password='" + password + '\'' + '}'; } public boolean isEmpty() { return aid == null && (aname == null || aname.isEmpty()) && (password == null || password.isEmpty()); } public boolean isUnsafe(boolean withPK) { if (!withPK) { return isEmpty() || aname == null || aname.isEmpty() || password == null || password.isEmpty(); } else { return isEmpty() || aid == null || aid <= 0 || aname == null || aname.isEmpty() || password == null || password.isEmpty(); } } public Integer getAid() { return aid; } public void setAid(Integer aid) { this.aid = aid; } public String getAname() { return aname; } public void setAname(String aname) { this.aname = aname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ``` 创建`Business.java`类 ```java package com.doooog.elm.po; import java.util.List; public class Business { private Integer bid; private String password; private String bname; private String address; private String explain; private Double startPrice; private Double deliveryPrice; private List foodList; public Business() { } public Business(Integer bid, String password, String bname, String address, String explain, Double startPrice, Double deliveryPrice) { this.bid = bid; this.password = password; this.bname = bname; this.address = address; this.explain = explain; this.startPrice = startPrice; this.deliveryPrice = deliveryPrice; } public Business(Integer bid, String password, String bname, String address, String explain, Double startPrice, Double deliveryPrice, List foodList) { this.bid = bid; this.password = password; this.bname = bname; this.address = address; this.explain = explain; this.startPrice = startPrice; this.deliveryPrice = deliveryPrice; this.foodList = foodList; } @Override public String toString() { return "Business{" + "bid=" + bid + ", password='" + password + '\'' + ", bname='" + bname + '\'' + ", address='" + address + '\'' + ", explain='" + explain + '\'' + ", startPrice=" + startPrice + ", deliveryPrice=" + deliveryPrice + '}'; } public boolean isEmpty() { return bid == null && (bname == null || bname.isEmpty()) && (address == null || address.isEmpty()) && (explain == null || explain.isEmpty()) && startPrice == null && deliveryPrice == null && (foodList == null || foodList.isEmpty()); } public boolean isUnsafe(boolean withPK) { if (!withPK) { return isEmpty() || password == null || password.isEmpty() || bname == null || bname.isEmpty(); } else { return isEmpty() || bid == null || bid <= 0 || password == null || password.isEmpty() || bname == null || bname.isEmpty(); } } public List getFoodList() { return foodList; } public void setFoodList(List foodList) { this.foodList = foodList; } public Integer getBid() { return bid; } public void setBid(Integer bid) { this.bid = bid; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getExplain() { return explain; } public void setExplain(String explain) { this.explain = explain; } public Double getStartPrice() { return startPrice; } public void setStartPrice(Double startPrice) { this.startPrice = startPrice; } public Double getDeliveryPrice() { return deliveryPrice; } public void setDeliveryPrice(Double deliveryPrice) { this.deliveryPrice = deliveryPrice; } } ``` 创建`Food.java`类 ```java package com.doooog.elm.po; public class Food { private Integer fid; private String fname; private String explain; private Double price; private Integer bid; private Business business; public Food() { } public Food(Integer fid, String fname, String explain, Double price, Integer bid) { this.fid = fid; this.fname = fname; this.explain = explain; this.price = price; this.bid = bid; } public Food(Integer fid, String fname, String explain, Double price, Integer bid, Business business) { this.fid = fid; this.fname = fname; this.explain = explain; this.price = price; this.bid = bid; this.business = business; } @Override public String toString() { return "Food{" + "fid=" + fid + ", fname='" + fname + '\'' + ", explain='" + explain + '\'' + ", price=" + price + ", bid=" + bid + '}'; } public boolean isEmpty() { return fid == null && (fname == null || fname.isEmpty()) && (explain == null || explain.isEmpty()) && price == null && bid == null && (business == null || business.isEmpty()); } public boolean isUnsafe(boolean withPK) { if (!withPK) { return this.isEmpty() || bid == null || bid <= 0 || fname == null || fname.isEmpty() || price == null || price <= 0; } else { return this.isEmpty() || fid == null || fid <= 0 || bid == null || bid <= 0 || fname == null || fname.isEmpty() || price == null || price <= 0; } } public Integer getFid() { return fid; } public void setFid(Integer fid) { this.fid = fid; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getExplain() { return explain; } public void setExplain(String explain) { this.explain = explain; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Integer getBid() { return bid; } public void setBid(Integer bid) { this.bid = bid; } public Business getBusiness() { return business; } public void setBusiness(Business business) { this.business = business; } } ``` #### 2、`util`层 在`rescources`资源包根目录下创建配置文件`db.properties`用来存放数据库连接信息 ```properties com.doooog.elm.mysql.driverClassName=com.mysql.jdbc.Driver com.doooog.elm.mysql.url=jdbc:mysql://localhost:3306/elm?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8 com.doooog.elm.mysql.username=username com.doooog.elm.mysql.password=password ``` 新建`com.doooog.elm.util`包,创建`DBUtil.java`类 ```java package com.doooog.elm.util; import java.io.IOException; import java.sql.*; import java.util.Properties; public class DBUtil { public static Connection getConnection() { Connection con = null; try { // 操作Properties的类 Properties properties = new Properties(); properties.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties")); Class.forName(properties.getProperty("com.doooog.elm.mysql.driverClassName")); con = DriverManager.getConnection( properties.getProperty("com.doooog.elm.mysql.url"), properties.getProperty("com.doooog.elm.mysql.username"), properties.getProperty("com.doooog.elm.mysql.password")); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } catch (IOException e) { System.out.println("未找到db.properties"); throw new RuntimeException(e); } return con; } public static void closeConnection(Connection con, PreparedStatement pst, ResultSet rs) { try { if (rs != null) rs.close(); if (pst != null) pst.close(); if (con != null) con.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } ``` 创建`CommonUtil.java`类 ```java package com.doooog.elm.util; public class CommonUtil { public static String copyChar(char c, int num) { if (num <= 0) return ""; return String.valueOf(c).repeat(num); } } ``` 创建`InputUtil.java`类 ```java package com.doooog.elm.util; import java.util.InputMismatchException; import java.util.Scanner; public class InputUtil { private static final Scanner INPUT = new Scanner(System.in); public static int menuInputInteger(String msg) { int data; try { data = INPUT.nextInt(); if (data < -2) throw new InputMismatchException(); } catch (InputMismatchException e) { INPUT.nextLine(); if (!msg.isEmpty()) { System.out.print("输入的类型错误!请重新输入" + msg + ":"); } else { System.out.print("输入的类型错误!请重新输入:"); } data = menuInputInteger(msg); } return data; } public static double menuInputDouble(String msg) { double data; try { data = INPUT.nextDouble(); if (data < 0 && data != -1) throw new InputMismatchException(); } catch (InputMismatchException e) { INPUT.nextLine(); if (!msg.isEmpty()) { System.out.print("输入的类型错误!请重新输入" + msg + ":"); } else { System.out.print("输入的类型错误!请重新输入:"); } data = menuInputDouble(msg); } return data; } public static String menuInputString(String msg) { String data; try { data = INPUT.next(); } catch (InputMismatchException e) { INPUT.nextLine(); if (!msg.isEmpty()) { System.out.print("输入的类型错误!请重新输入" + msg + ":"); } else { System.out.print("输入的类型错误!请重新输入:"); } data = menuInputString(msg); } return data; } } ``` #### 3、`dao`层 新建`com.doooog.elm.dao`包,创建`AdminDao.java`接口 ```java package com.doooog.elm.dao; import com.doooog.elm.po.Admin; import java.util.List; public interface AdminDao { Admin getAdminByLogin(String username, String password); Admin getAdminById(int aid); boolean isExistById(int aid); List listAdminAll(); int addAdmin(Admin admin); int addAdminWithId(Admin admin); int deleteAdminById(int aid); int updateAdminPasswordById(int aid, String password); } ``` 创建`BusinessDao.java`接口 ```java package com.doooog.elm.dao; import com.doooog.elm.po.Business; import java.util.List; public interface BusinessDao { Business getBusinessById(int bid); Business getBusinessByName(String bname); List listBusinessByName(String bname); List listBusinessAll(); Business getLastBusiness(); int addBusiness(Business business); int addBusinessWithId(Business business); int updateBusinessById(Business business); int deleteBusinessById(int bid); } ``` 创建`FoodDao.java`接口 ```java package com.doooog.elm.dao; import com.doooog.elm.po.Food; import java.util.List; public interface FoodDao { Food getFoodById(int fid); List getFoodByName(String fname); List listFoodByName(String fname); List listFoodAll(); List listFoodByBusinessId(int bid); int updateFoodById(Food food); int updateFoodPriceById(int fid, double price); int addFood(Food food); int addFoodWithId(Food food); int deleteFoodById(int fid); } ``` 新建`com.doooog.elm.dao.impl`包,创建`AdminDaoImpl.java`类 ```java package com.doooog.elm.dao.impl; import com.doooog.elm.dao.AdminDao; import com.doooog.elm.po.Admin; import com.doooog.elm.util.DBUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class AdminDaoImpl implements AdminDao { @Override public Admin getAdminByLogin(String username, String password) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Admin admin = null; try { String sql = "select * from admin where aname=? and password=?"; pst = con.prepareStatement(sql); pst.setString(1, username); pst.setString(2, password); rs = pst.executeQuery(); if (rs.next()) { admin = fillAdmin(rs); } if (admin != null && !admin.getPassword().equals(password)) { admin = null; } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return admin; } @Override public Admin getAdminById(int aid) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Admin admin = null; try { String sql = "select * from admin where aid=?"; pst = con.prepareStatement(sql); pst.setInt(1, aid); rs = pst.executeQuery(); if (rs.next()) { admin = fillAdmin(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return admin; } @Override public boolean isExistById(int aid) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; boolean result = false; try { String sql = "select count(aid) 'num' from admin where aid=?"; pst = con.prepareStatement(sql); pst.setInt(1, aid); rs = pst.executeQuery(); if (rs.next()) { int i = rs.getInt("num"); if (i > 1) result = true; } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public List listAdminAll() { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; List list = null; try { String sql = "select * from admin"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillAdmin(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public int addAdmin(Admin admin) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "insert into admin(aname, password) VALUES (?,?)"; pst = con.prepareStatement(sql); pst.setString(1, admin.getAname()); pst.setString(2, admin.getPassword()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int addAdminWithId(Admin admin) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "insert into admin VALUES (?,?,?)"; pst = con.prepareStatement(sql); pst.setInt(1, admin.getAid()); pst.setString(2, admin.getAname()); pst.setString(3, admin.getPassword()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int deleteAdminById(int aid) { if (aid <= 0) return 0; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "delete from admin where aid=?"; pst = con.prepareStatement(sql); pst.setInt(1, aid); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int updateAdminPasswordById(int aid, String password) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "update admin set password=? where aid=?"; pst = con.prepareStatement(sql); pst.setInt(2, aid); pst.setString(1, password); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } private Admin fillAdmin(ResultSet rs) throws SQLException { return new Admin( rs.getInt("aid"), rs.getString("aname"), rs.getString("password") ); } } ``` 创建`BusinessDaoImpl.java`类 ```java package com.doooog.elm.dao.impl; import com.doooog.elm.dao.BusinessDao; import com.doooog.elm.po.Business; import com.doooog.elm.util.DBUtil; import java.sql.*; import java.util.ArrayList; import java.util.List; public class BusinessDaoImpl implements BusinessDao { @Override public Business getBusinessById(int bid) { if (bid <= 0) return null; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Business business = null; try { String sql = "select * from business where bid=?"; pst = con.prepareStatement(sql); pst.setInt(1, bid); rs = pst.executeQuery(); if (rs.next()) { business = fillBusiness(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return business; } @Override public Business getBusinessByName(String bname) { if (bname == null || bname.isEmpty()) return null; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Business business = null; try { String sql = "select * from business where bname=?"; pst = con.prepareStatement(sql); pst.setString(1, bname); rs = pst.executeQuery(); if (rs.next()) { business = fillBusiness(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return business; } @Override public List listBusinessByName(String bname) { if (bname == null || bname.isEmpty()) return null; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; List list = null; try { String sql = "select * from business where bname=concat('%',?,'%')"; pst = con.prepareStatement(sql); pst.setString(1, bname); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillBusiness(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public List listBusinessAll() { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; List list = null; try { String sql = "select * from business"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillBusiness(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public Business getLastBusiness() { //SELECT * FROM your_table ORDER BY created_at DESC LIMIT 1; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Business business = null; try { String sql = "select * from business order by bid desc limit 1"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); if (rs.next()) { business = fillBusiness(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return business; } @Override public int addBusiness(Business business) { if (business == null || business.isEmpty()) return 0; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; int result = 0; try { String sql = "insert into business(password, bname, address, `explain`, startPrice, deliveryPrice) values(?,?,?,?,?,?)"; pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); if (business.getPassword() != null && !business.getPassword().isEmpty()) { pst.setString(1, business.getPassword()); } else { pst.setNull(1, Types.VARCHAR); } if (business.getBname() != null && !business.getBname().isEmpty()) { pst.setString(2, business.getBname()); } else { pst.setNull(2, Types.VARCHAR); } if (business.getAddress() != null && !business.getAddress().isEmpty()) { pst.setString(3, business.getAddress()); } else { pst.setNull(3, Types.VARCHAR); } if (business.getExplain() != null && !business.getExplain().isEmpty()) { pst.setString(4, business.getExplain()); } else { pst.setNull(4, Types.VARCHAR); } if (business.getStartPrice() != null && business.getStartPrice() >= 0) { pst.setDouble(5, business.getStartPrice()); } else { pst.setNull(5, Types.DECIMAL); } if (business.getDeliveryPrice() != null && business.getDeliveryPrice() >= 0) { pst.setDouble(6, business.getDeliveryPrice()); } else { pst.setNull(6, Types.DECIMAL); } result = pst.executeUpdate(); rs = pst.getGeneratedKeys(); if (rs.next()) result = rs.getInt(1); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return result; } @Override public int addBusinessWithId(Business business) { if (business == null || business.isEmpty() || business.getBid() <= 0) return 0; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "insert into business values(?,?,?,?,?,?,?)"; pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); if (business.getBid() != null && business.getBid() >= 0) { pst.setInt(1, business.getBid()); } else { pst.setNull(1, Types.INTEGER); } if (business.getPassword() != null && !business.getPassword().isEmpty()) { pst.setString(2, business.getPassword()); } else { pst.setNull(2, Types.VARCHAR); } if (business.getBname() != null && !business.getBname().isEmpty()) { pst.setString(3, business.getBname()); } else { pst.setNull(3, Types.VARCHAR); } if (business.getAddress() != null && !business.getAddress().isEmpty()) { pst.setString(4, business.getAddress()); } else { pst.setNull(4, Types.VARCHAR); } if (business.getExplain() != null && !business.getExplain().isEmpty()) { pst.setString(5, business.getExplain()); } else { pst.setNull(5, Types.VARCHAR); } if (business.getStartPrice() != null && business.getStartPrice() >= 0) { pst.setDouble(6, business.getStartPrice()); } else { pst.setNull(6, Types.DECIMAL); } if (business.getDeliveryPrice() != null && business.getDeliveryPrice() >= 0) { pst.setDouble(7, business.getDeliveryPrice()); } else { pst.setNull(7, Types.DECIMAL); } result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int updateBusinessById(Business business) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "update business set password=?, bname=?, address=?, `explain`=?, startPrice=?, deliveryPrice=? where bid=?"; pst = con.prepareStatement(sql); pst.setString(1, business.getPassword()); pst.setString(2, business.getBname()); if (business.getAddress() != null && !business.getAddress().isEmpty()) { pst.setString(3, business.getAddress()); } else { pst.setNull(3, Types.VARCHAR); } if (business.getExplain() != null && !business.getExplain().isEmpty()) { pst.setString(4, business.getExplain()); } else { pst.setNull(4, Types.VARCHAR); } if (business.getStartPrice() != null && business.getStartPrice() >= 0) { pst.setDouble(5, business.getStartPrice()); } else { pst.setNull(5, Types.DECIMAL); } if (business.getDeliveryPrice() != null && business.getDeliveryPrice() >= 0) { pst.setDouble(6, business.getDeliveryPrice()); } else { pst.setNull(6, Types.DECIMAL); } pst.setInt(7, business.getBid()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int deleteBusinessById(int bid) { if (bid <= 0) return 0; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "delete from business where bid=?"; pst = con.prepareStatement(sql); pst.setInt(1, bid); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } private Business fillBusiness(ResultSet rs) throws SQLException { return new Business( rs.getInt("bid"), rs.getString("password"), rs.getString("bname"), rs.getString("address"), rs.getString("explain"), rs.getDouble("startPrice"), rs.getDouble("deliveryPrice") ); } } ``` 创建`FoodDaoImpl.java`类 ```java package com.doooog.elm.dao.impl; import com.doooog.elm.dao.FoodDao; import com.doooog.elm.po.Food; import com.doooog.elm.util.DBUtil; import java.sql.*; import java.util.ArrayList; import java.util.List; public class FoodDaoImpl implements FoodDao { @Override public Food getFoodById(int fid) { if (fid <= 0) return null; PreparedStatement pst = null; ResultSet rs = null; Food food = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food where fid=?"; pst = con.prepareStatement(sql); pst.setInt(1, fid); rs = pst.executeQuery(); if (rs.next()) { food = fillFood(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return food; } @Override public List getFoodByName(String fname) { if (fname == null || fname.isEmpty()) return null; PreparedStatement pst = null; ResultSet rs = null; List list = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food where fname=?"; pst = con.prepareStatement(sql); pst.setString(1, fname); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillFood(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public List listFoodByName(String fname) { if (fname == null || fname.isEmpty()) return null; PreparedStatement pst = null; ResultSet rs = null; List list = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food where fname like concat('%',?,'%')"; pst = con.prepareStatement(sql); pst.setString(1, fname); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillFood(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public List listFoodAll() { PreparedStatement pst = null; ResultSet rs = null; List list = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillFood(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public List listFoodByBusinessId(int bid) { if (bid <= 0) return null; PreparedStatement pst = null; ResultSet rs = null; List list = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food where bid=?"; pst = con.prepareStatement(sql); pst.setInt(1, bid); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillFood(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public int updateFoodById(Food food) { if (food == null || food.isUnsafe(true)) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "update food set fname=?,`explain`=?,price=? where fid=?"; pst = con.prepareStatement(sql); pst.setString(1, food.getFname()); if (food.getExplain() != null && !food.getExplain().isEmpty()) { pst.setString(2, food.getExplain()); } else { pst.setNull(2, Types.VARCHAR); } pst.setDouble(3, food.getPrice()); pst.setInt(4, food.getFid()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int updateFoodPriceById(int fid, double price) { if (fid <= 0 || price <= 0) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "update food set price=? where fid=?"; pst = con.prepareStatement(sql); pst.setDouble(1, price); pst.setInt(2, fid); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int addFood(Food food) { if (food == null || food.isUnsafe(false)) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "insert into food(fname, `explain`, price, bid) VALUES (?,?,?,?)"; pst = con.prepareStatement(sql); pst.setString(1, food.getFname()); if (food.getExplain() != null && !food.getExplain().isEmpty()) { pst.setString(2, food.getExplain()); } else { pst.setNull(2, Types.VARCHAR); } pst.setDouble(3, food.getPrice()); pst.setInt(4, food.getBid()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int addFoodWithId(Food food) { if (food == null || food.isUnsafe(true)) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "insert into food VALUES (?,?,?,?,?)"; pst = con.prepareStatement(sql); pst.setInt(1, food.getFid()); pst.setString(2, food.getFname()); if (food.getExplain() != null && !food.getExplain().isEmpty()) { pst.setString(3, food.getExplain()); } else { pst.setNull(3, Types.VARCHAR); } pst.setDouble(4, food.getPrice()); pst.setInt(5, food.getBid()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int deleteFoodById(int fid) { if (fid <= 0) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "delete from food where fid=?"; pst = con.prepareStatement(sql); pst.setInt(1, fid); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } private Food fillFood(ResultSet rs) throws SQLException { return new Food( rs.getInt("fid"), rs.getString("fname"), rs.getString("explain"), rs.getDouble("price"), rs.getInt("bid") ); } } ``` #### 4、`controller`层 新建`com.doooog.elm.controller`包,创建`AdminCtrl.java`接口 ```java package com.doooog.elm.controller; import com.doooog.elm.po.Admin; import com.doooog.elm.po.Business; public interface AdminCtrl { // 管理员部分 boolean login(String username, String password); boolean addAdmin(Admin admin); boolean deleteAdminById(int aid); void listAdminAll(); boolean updateAdminPasswordById(int aid, String oldPW, String newPW); boolean getAdminById(int aid); // 商家部分 boolean addBusiness(Business business); boolean deleteBusinessById(int bid); boolean getBusinessById(int bid); void listBusinessAll(); boolean updateBusinessPasswordById(int bid, String oldPW, String newPW); boolean updateBusinessNameById(int bid, String name); boolean updateBusinessAddressById(int bid, String address); boolean updateBusinessExplainById(int bid, String explain); boolean updateBusinessPriceById(int bid,double startPrice, double deliveryPrice); } ``` 新建`com.doooog.elm.controller.impl`包,创建`AdminCtrlImpl.java`类 ```java package com.doooog.elm.controller.impl; import com.doooog.elm.controller.AdminCtrl; import com.doooog.elm.dao.AdminDao; import com.doooog.elm.dao.BusinessDao; import com.doooog.elm.dao.impl.AdminDaoImpl; import com.doooog.elm.dao.impl.BusinessDaoImpl; import com.doooog.elm.po.Admin; import com.doooog.elm.po.Business; import java.util.List; public class AdminCtrlImpl implements AdminCtrl { public boolean login(String username, String password) { if (username == null || password == null || username.isEmpty() || password.isEmpty()) return false; AdminDao adminDao = new AdminDaoImpl(); Admin admin = adminDao.getAdminByLogin(username, password); return admin != null && !admin.isEmpty(); } @Override public boolean addAdmin(Admin admin) { if (admin == null) return false; if ((admin.getAid() == null || admin.getAid() <= 0) && !admin.isUnsafe(false)) { AdminDao adminDao = new AdminDaoImpl(); return adminDao.addAdmin(admin) == 1; } else if (!admin.isUnsafe(true)) { AdminDao adminDao = new AdminDaoImpl(); return adminDao.addAdminWithId(admin) == 1; } return false; } @Override public boolean deleteAdminById(int aid) { if (aid <= 0) return false; AdminDao adminDao = new AdminDaoImpl(); return adminDao.deleteAdminById(aid) == 1; } @Override public void listAdminAll() { AdminDao adminDao = new AdminDaoImpl(); List list = adminDao.listAdminAll(); if (list.isEmpty()) { System.out.println("未查询到信息"); return; } for (Admin admin : list) { System.out.println(admin); } } @Override public boolean updateAdminPasswordById(int aid, String oldPW, String newPW) { if (aid <= 0 || oldPW == null || oldPW.isEmpty() || newPW == null || newPW.isEmpty()) return false; AdminDao adminDao = new AdminDaoImpl(); Admin admin = adminDao.getAdminById(aid); if (admin == null || !admin.getPassword().equals(oldPW)) return false; return adminDao.updateAdminPasswordById(aid, newPW) == 1; } @Override public boolean getAdminById(int aid) { if (aid <= 0) { System.out.println("输入ID有误>0"); return false; } AdminDao adminDao = new AdminDaoImpl(); Admin admin = adminDao.getAdminById(aid); if (admin == null) { System.out.println("未查询到信息"); return false; } System.out.println(admin); return true; } @Override public boolean addBusiness(Business business) { if (business == null) return false; if ((business.getBid() == null || business.getBid() <= 0) && !business.isUnsafe(false)) { BusinessDao businessDao = new BusinessDaoImpl(); int rs = businessDao.addBusiness(business); business.setBid(rs); return rs != 0; } else if (!business.isUnsafe(true)) { BusinessDao businessDao = new BusinessDaoImpl(); int rs = businessDao.addBusinessWithId(business); business.setBid(rs); return rs != 0; } return false; } @Override public boolean deleteBusinessById(int bid) { if (bid <= 0) return false; BusinessDao businessDao = new BusinessDaoImpl(); return businessDao.deleteBusinessById(bid) == 1; } @Override public boolean getBusinessById(int bid) { if (bid <= 0) { System.out.println("输入ID有误>0"); return false; } BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) { System.out.println("未查询到信息"); return false; } System.out.println(b); return true; } @Override public void listBusinessAll() { BusinessDao businessDao = new BusinessDaoImpl(); List list = businessDao.listBusinessAll(); if (list.isEmpty()) { System.out.println("未查询到信息"); return; } for (Business b : list) { System.out.println(b); } } @Override public boolean updateBusinessPasswordById(int bid, String oldPW, String newPW) { if (bid <= 0 || oldPW == null || oldPW.isEmpty() || newPW == null || newPW.isEmpty()) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null || !b.getPassword().equals(oldPW)) return false; b.setPassword(newPW); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } @Override public boolean updateBusinessNameById(int bid, String name) { if (bid <= 0 || name == null || name.isEmpty()) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) return false; b.setBname(name); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } @Override public boolean updateBusinessAddressById(int bid, String address) { if (bid <= 0 || address == null || address.isEmpty()) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) return false; b.setAddress(address); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } @Override public boolean updateBusinessExplainById(int bid, String explain) { if (bid <= 0 || explain == null || explain.isEmpty()) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) return false; b.setExplain(explain); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } @Override public boolean updateBusinessPriceById(int bid, double startPrice, double deliveryPrice) { if (bid <= 0 || startPrice <= 0 || deliveryPrice <= 0) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) return false; b.setStartPrice(startPrice); b.setDeliveryPrice(deliveryPrice); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } } ``` #### 5、`view`层 新建`com.doooog.elm.view`包,创建`MainEntry.java`类作为程序入口。 ```java package com.doooog.elm.view; import com.doooog.elm.util.CommonUtil; import com.doooog.elm.util.InputUtil; public class MainEntry { public static void main(String[] args) { System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 欢迎进入饿了么后台管理系统 --|"); MainEntry main = new MainEntry(); main.menuMain(); } public void menuMain() { System.out.println("|-- 1.系统管理员 2.商家"); System.out.print("|-- 请输入编号(-1退出):"); int choice = InputUtil.menuInputInteger("(整数)"); switch (choice) { case 1 -> new AdminView().menuLogin(); case 2 -> new BusinessView().menuLogin(); default -> { } } } } ``` 创建`AdminView.java`类 ```java package com.doooog.elm.view; import com.doooog.elm.controller.impl.AdminCtrlImpl; import com.doooog.elm.po.Admin; import com.doooog.elm.po.Business; import com.doooog.elm.util.CommonUtil; import com.doooog.elm.util.InputUtil; public class AdminView { private AdminCtrlImpl ctrl = new AdminCtrlImpl(); public void menuLogin() { System.out.println("|-- 请使用管理员身份进行登录 --|"); System.out.print("|-- 请输入用户名:"); String username = InputUtil.menuInputString(""); System.out.print("|-- 请输入密码:"); String password = InputUtil.menuInputString(""); if (ctrl.login(username, password)) { System.out.println("登录成功!"); menuMain(); } else { System.out.println("登录失败,请检查账号或密码是否有误!"); } } public void menuMain() { boolean exit = false; while (!exit) { System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 管理员菜单 --|"); System.out.println("|-- 管理员操作-----------------"); System.out.println("|-- 1.查看所有管理员"); System.out.println("|-- 2.新增管理员"); System.out.println("|-- 3.更改管理员密码"); System.out.println("|-- 4.删除管理员"); System.out.println("|-- 商家操作------------------"); System.out.println("|-- 5.查看所有商家"); System.out.println("|-- 6.新增商家"); System.out.println("|-- 7.更改商家密码"); System.out.println("|-- 8.更改商家名称"); System.out.println("|-- 9.更改商家地址"); System.out.println("|-- 10.更改商家介绍"); System.out.println("|-- 11.更改商家配送价格"); System.out.println("|-- 12.删除商家"); System.out.println("|-- 0.退出系统"); System.out.println(CommonUtil.copyChar('-', 30)); System.out.print("|-- 请输入编号(0~12整数):"); int choice = InputUtil.menuInputInteger("(0~12整数)"); switch (choice) { case 0 -> exit = true; case 1 -> ctrl.listAdminAll(); case 2 -> menuAddAdmin(); case 3 -> menuRePasswordAdmin(); case 4 -> menuDeleteAdmin(); case 5 -> ctrl.listBusinessAll(); case 6 -> menuAddBusiness(); case 7 -> menuRePasswordBusiness(); case 8 -> menuRename(); case 9 -> menuReAddress(); case 10 -> menuReExplain(); case 11 -> menuRePrice(); case 12 -> menuDeleteBusiness(); } } } public void menuAddAdmin() { Admin admin = new Admin(); int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- ID(整数):"); tmp = InputUtil.menuInputInteger("(请输入整数,或0跳过)"); if (tmp == -1) return; if (tmp != 0) admin.setAid(tmp); System.out.print("|-- 账号(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; admin.setAname(tmp2); System.out.print("|-- 密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; admin.setPassword(tmp2); if (ctrl.addAdmin(admin)) { System.out.println("添加成功!"); return; } System.out.println("添加失败,请检查输入是否有误,ID可以为空"); } public void menuRePasswordAdmin() { Admin admin = new Admin(); int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)提取方法…"); if (tmp == -1 || tmp == 0) return; admin.setAid(tmp); System.out.print("|-- 旧密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; admin.setAname(tmp2); System.out.print("|-- 新密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; admin.setPassword(tmp2); if (ctrl.updateAdminPasswordById(admin.getAid(), admin.getAname(), admin.getPassword())) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuDeleteAdmin() { int tmp; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要删除的ID(0以上整数)(必填):"); tmp = InputUtil.menuInputInteger("(0以上整数)"); if (tmp == -1) return; if (!ctrl.getAdminById(tmp)) return; if (!doChoice()) return; if (ctrl.deleteAdminById(tmp)) { System.out.println("删除成功!"); return; } System.out.println("删除失败,未知错误"); } public void menuAddBusiness() { Business business = new Business(); double tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 名称(必填):"); tmp2 = InputUtil.menuInputString("(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; business.setBname(tmp2); System.out.print("|-- 密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; business.setPassword(tmp2); System.out.print("|-- 地址:"); tmp2 = InputUtil.menuInputString(""); if (tmp2.equals("-1")) return; if (!tmp2.equals("0")) business.setAddress(tmp2); System.out.print("|-- 介绍:"); tmp2 = InputUtil.menuInputString(""); if (tmp2.equals("-1")) return; if (!tmp2.equals("0")) business.setExplain(tmp2); System.out.print("|-- 起送价格:"); tmp = InputUtil.menuInputDouble(""); if (tmp == -1) return; if (tmp != 0) business.setStartPrice(tmp); System.out.print("|-- 配送价格:"); tmp = InputUtil.menuInputDouble(""); if (tmp == -1) return; if (tmp != 0) business.setDeliveryPrice(tmp); System.out.print("|-- 确认信息:"); System.out.println(business); if (!doChoice()) return; if (ctrl.addBusiness(business)) { System.out.println("添加成功!"); System.out.println("您用于登录的编号为:" + business.getBid()); return; } System.out.println("添加失败,请检查输入是否有误"); } public void menuRePasswordBusiness() { Business business = new Business(); int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; business.setBid(tmp); System.out.print("|-- 旧密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; business.setBname(tmp2); System.out.print("|-- 新密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; business.setPassword(tmp2); if (ctrl.updateBusinessPasswordById(business.getBid(), business.getBname(), business.getPassword())) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuRename() { int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; System.out.print("|-- 新名称(必填):"); tmp2 = InputUtil.menuInputString("(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; if (ctrl.updateBusinessNameById(tmp, tmp2)) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuReAddress() { int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; System.out.print("|-- 新地址(必填):"); tmp2 = InputUtil.menuInputString("(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; if (ctrl.updateBusinessAddressById(tmp, tmp2)) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuReExplain() { int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; System.out.print("|-- 新介绍(必填):"); tmp2 = InputUtil.menuInputString("(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; if (ctrl.updateBusinessExplainById(tmp, tmp2)) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuRePrice() { int tmp; double tmp2, tmp3; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; System.out.print("|-- 新起送价(大于0的小数)(必填):"); tmp2 = InputUtil.menuInputDouble("(大于0的小数)(必填)"); if (tmp2 == -1 || tmp2 == 0) return; System.out.print("|-- 新配送价(大于0的小数)(必填):"); tmp3 = InputUtil.menuInputDouble("(大于0的小数)(必填)"); if (tmp3 == -1 || tmp3 == 0) return; if (ctrl.updateBusinessPriceById(tmp, tmp2, tmp3)) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuDeleteBusiness() { int tmp; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要删除的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; if (!ctrl.getBusinessById(tmp)) return; if (!doChoice()) return; if (ctrl.deleteBusinessById(tmp)) { System.out.println("删除成功!"); return; } System.out.println("删除失败,请检查输入是否有误"); } public boolean doChoice() { System.out.print("|-- 确认无误(1.是,2.否):"); int tmp = InputUtil.menuInputInteger("(1.是,2.否)"); return tmp == 1; } } ``` Loading... 使用JDBC作为数据库管理工具,做一个控制台模拟前端的简单外卖系统。 ## 一、设计数据库 #### 1、数据库结构 | No | 表名称 | 中文名 | 说明 | | -- | -------- | -------- | -------------------------------- | | 1 | business | 商家表 | 存储所有商家信息 | | 2 | food | 食品表 | 存储每个商家所拥有的所有食品信息 | | 3 | admin | 管理员表 | 存储管理员信息 | #### 2、表结构 约束类型标识: - PK:primary key 主键 - FK:foreign key 外键 - NN:not null 非空 - UQ:unique 唯一索引 - AI:auto increment 自增 ##### business(商家表) | No | 字段名 | 数据类型 | size | 默认値 | 约束 | 说明 | | -- | ------------- | -------- | ----- | ------ | ---------- | -------- | | 1 | bid | int | | | PK、AI、NN | 商家编号 | | 2 | password | varchar | 20 | | NN | 密码 | | 3 | bname | varchar | 40 | | NN | 商家名称 | | 4 | address | varchar | 50 | | | 商家地址 | | 5 | explain | varchar | 100 | | | 商家介绍 | | 6 | starPrice | decimal | (5,2) | 0.00 | | 起送费 | | 7 | deliveryPrice | decimal | (5,2) | 0.00 | | 配送费 | ##### food(食品表) | No | 字段名 | 数据类型 | size | 默认値 | 约束 | 说明 | | -- | ------- | -------- | ----- | ------ | ---------- | ------------ | | 1 | fid | int | | | PK、AI、NN | 食品编号 | | 2 | fname | varchar | 30 | | NN | 食品名称 | | 3 | explain | varchar | 30 | | | 食品介绍 | | 4 | price | decimal | (5,2) | | NN | 食品价格 | | 5 | bid | int | | | FK、NN | 所属商家编号 | ##### admin(管理员表) | No | 字段名 | 数据类型 | size | 默认値 | 约束 | 说明 | | -- | -------- | -------- | ---- | ------ | ---------- | ---------- | | 1 | aid | int | | | PK、AI、NN | 管理员编号 | | 2 | aname | varchar | 20 | | NN、UQ | 管理员名称 | | 3 | password | varchar | 20 | | NN | 密码 | #### 3、SQL语句 ```SQL CREATE DATABASE IF NOT EXISTS elm CHARSET utf8; USE elm; CREATE TABLE IF NOT EXISTS business ( bid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '商家编号', password VARCHAR(20) NOT NULL COMMENT '密码', bname VARCHAR(40) NOT NULL COMMENT '商家名称', address VARCHAR(50) COMMENT '商家地址', `explain` VARCHAR(100) COMMENT '商家介绍', startPrice DECIMAL(5, 2) DEFAULT 0.00 COMMENT '起送费', deliveryPrice DECIMAL(5, 2) DEFAULT 0.00 COMMENT '配送费' ); CREATE TABLE IF NOT EXISTS food ( fid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '食品编号', fname VARCHAR(30) NOT NULL COMMENT '食品名称', `explain` VARCHAR(30) COMMENT '食品介绍', price DECIMAL(5, 2) NOT NULL COMMENT '食品价格', bid INT UNSIGNED NOT NULL COMMENT '所属商家编号', FOREIGN KEY (bid) REFERENCES business (bid) ); CREATE TABLE IF NOT EXISTS admin ( aid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '管理员编号', aname VARCHAR(20) NOT NULL UNIQUE COMMENT '管理员名称', password VARCHAR(20) NOT NULL COMMENT '密码' ); INSERT INTO admin(aname, `password`) VALUES ('admin', 'admin'); ``` ## 二、添加JDBC库 在根目录下新建`lib`文件夹,将`mysqlconnectorjava5.1.10bin.jar`存放进去,导入仓库。 [香肠狗网盘](https://pan.soki.me/s/vrUj) ## 三、创建项目各层 #### 1、`po`层 新建`com.doooog.elm.po`包,创建`Admin.java`类 ```java package com.doooog.elm.po; public class Admin { private Integer aid; private String aname; private String password; public Admin() { } public Admin(Integer aid, String aname, String password) { this.aid = aid; this.aname = aname; this.password = password; } @Override public String toString() { return "Admin{" + "aid=" + aid + ", aname='" + aname + '\'' + ", password='" + password + '\'' + '}'; } public boolean isEmpty() { return aid == null && (aname == null || aname.isEmpty()) && (password == null || password.isEmpty()); } public boolean isUnsafe(boolean withPK) { if (!withPK) { return isEmpty() || aname == null || aname.isEmpty() || password == null || password.isEmpty(); } else { return isEmpty() || aid == null || aid <= 0 || aname == null || aname.isEmpty() || password == null || password.isEmpty(); } } public Integer getAid() { return aid; } public void setAid(Integer aid) { this.aid = aid; } public String getAname() { return aname; } public void setAname(String aname) { this.aname = aname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ``` 创建`Business.java`类 ```java package com.doooog.elm.po; import java.util.List; public class Business { private Integer bid; private String password; private String bname; private String address; private String explain; private Double startPrice; private Double deliveryPrice; private List<Food> foodList; public Business() { } public Business(Integer bid, String password, String bname, String address, String explain, Double startPrice, Double deliveryPrice) { this.bid = bid; this.password = password; this.bname = bname; this.address = address; this.explain = explain; this.startPrice = startPrice; this.deliveryPrice = deliveryPrice; } public Business(Integer bid, String password, String bname, String address, String explain, Double startPrice, Double deliveryPrice, List<Food> foodList) { this.bid = bid; this.password = password; this.bname = bname; this.address = address; this.explain = explain; this.startPrice = startPrice; this.deliveryPrice = deliveryPrice; this.foodList = foodList; } @Override public String toString() { return "Business{" + "bid=" + bid + ", password='" + password + '\'' + ", bname='" + bname + '\'' + ", address='" + address + '\'' + ", explain='" + explain + '\'' + ", startPrice=" + startPrice + ", deliveryPrice=" + deliveryPrice + '}'; } public boolean isEmpty() { return bid == null && (bname == null || bname.isEmpty()) && (address == null || address.isEmpty()) && (explain == null || explain.isEmpty()) && startPrice == null && deliveryPrice == null && (foodList == null || foodList.isEmpty()); } public boolean isUnsafe(boolean withPK) { if (!withPK) { return isEmpty() || password == null || password.isEmpty() || bname == null || bname.isEmpty(); } else { return isEmpty() || bid == null || bid <= 0 || password == null || password.isEmpty() || bname == null || bname.isEmpty(); } } public List<Food> getFoodList() { return foodList; } public void setFoodList(List<Food> foodList) { this.foodList = foodList; } public Integer getBid() { return bid; } public void setBid(Integer bid) { this.bid = bid; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getExplain() { return explain; } public void setExplain(String explain) { this.explain = explain; } public Double getStartPrice() { return startPrice; } public void setStartPrice(Double startPrice) { this.startPrice = startPrice; } public Double getDeliveryPrice() { return deliveryPrice; } public void setDeliveryPrice(Double deliveryPrice) { this.deliveryPrice = deliveryPrice; } } ``` 创建`Food.java`类 ```java package com.doooog.elm.po; public class Food { private Integer fid; private String fname; private String explain; private Double price; private Integer bid; private Business business; public Food() { } public Food(Integer fid, String fname, String explain, Double price, Integer bid) { this.fid = fid; this.fname = fname; this.explain = explain; this.price = price; this.bid = bid; } public Food(Integer fid, String fname, String explain, Double price, Integer bid, Business business) { this.fid = fid; this.fname = fname; this.explain = explain; this.price = price; this.bid = bid; this.business = business; } @Override public String toString() { return "Food{" + "fid=" + fid + ", fname='" + fname + '\'' + ", explain='" + explain + '\'' + ", price=" + price + ", bid=" + bid + '}'; } public boolean isEmpty() { return fid == null && (fname == null || fname.isEmpty()) && (explain == null || explain.isEmpty()) && price == null && bid == null && (business == null || business.isEmpty()); } public boolean isUnsafe(boolean withPK) { if (!withPK) { return this.isEmpty() || bid == null || bid <= 0 || fname == null || fname.isEmpty() || price == null || price <= 0; } else { return this.isEmpty() || fid == null || fid <= 0 || bid == null || bid <= 0 || fname == null || fname.isEmpty() || price == null || price <= 0; } } public Integer getFid() { return fid; } public void setFid(Integer fid) { this.fid = fid; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getExplain() { return explain; } public void setExplain(String explain) { this.explain = explain; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Integer getBid() { return bid; } public void setBid(Integer bid) { this.bid = bid; } public Business getBusiness() { return business; } public void setBusiness(Business business) { this.business = business; } } ``` #### 2、`util`层 在`rescources`资源包根目录下创建配置文件`db.properties`用来存放数据库连接信息 ```properties com.doooog.elm.mysql.driverClassName=com.mysql.jdbc.Driver com.doooog.elm.mysql.url=jdbc:mysql://localhost:3306/elm?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8 com.doooog.elm.mysql.username=username com.doooog.elm.mysql.password=password ``` 新建`com.doooog.elm.util`包,创建`DBUtil.java`类 ```java package com.doooog.elm.util; import java.io.IOException; import java.sql.*; import java.util.Properties; public class DBUtil { public static Connection getConnection() { Connection con = null; try { // 操作Properties的类 Properties properties = new Properties(); properties.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties")); Class.forName(properties.getProperty("com.doooog.elm.mysql.driverClassName")); con = DriverManager.getConnection( properties.getProperty("com.doooog.elm.mysql.url"), properties.getProperty("com.doooog.elm.mysql.username"), properties.getProperty("com.doooog.elm.mysql.password")); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } catch (IOException e) { System.out.println("未找到db.properties"); throw new RuntimeException(e); } return con; } public static void closeConnection(Connection con, PreparedStatement pst, ResultSet rs) { try { if (rs != null) rs.close(); if (pst != null) pst.close(); if (con != null) con.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } ``` 创建`CommonUtil.java`类 ```java package com.doooog.elm.util; public class CommonUtil { public static String copyChar(char c, int num) { if (num <= 0) return ""; return String.valueOf(c).repeat(num); } } ``` 创建`InputUtil.java`类 ```java package com.doooog.elm.util; import java.util.InputMismatchException; import java.util.Scanner; public class InputUtil { private static final Scanner INPUT = new Scanner(System.in); public static int menuInputInteger(String msg) { int data; try { data = INPUT.nextInt(); if (data < -2) throw new InputMismatchException(); } catch (InputMismatchException e) { INPUT.nextLine(); if (!msg.isEmpty()) { System.out.print("输入的类型错误!请重新输入" + msg + ":"); } else { System.out.print("输入的类型错误!请重新输入:"); } data = menuInputInteger(msg); } return data; } public static double menuInputDouble(String msg) { double data; try { data = INPUT.nextDouble(); if (data < 0 && data != -1) throw new InputMismatchException(); } catch (InputMismatchException e) { INPUT.nextLine(); if (!msg.isEmpty()) { System.out.print("输入的类型错误!请重新输入" + msg + ":"); } else { System.out.print("输入的类型错误!请重新输入:"); } data = menuInputDouble(msg); } return data; } public static String menuInputString(String msg) { String data; try { data = INPUT.next(); } catch (InputMismatchException e) { INPUT.nextLine(); if (!msg.isEmpty()) { System.out.print("输入的类型错误!请重新输入" + msg + ":"); } else { System.out.print("输入的类型错误!请重新输入:"); } data = menuInputString(msg); } return data; } } ``` #### 3、`dao`层 新建`com.doooog.elm.dao`包,创建`AdminDao.java`接口 ```java package com.doooog.elm.dao; import com.doooog.elm.po.Admin; import java.util.List; public interface AdminDao { Admin getAdminByLogin(String username, String password); Admin getAdminById(int aid); boolean isExistById(int aid); List<Admin> listAdminAll(); int addAdmin(Admin admin); int addAdminWithId(Admin admin); int deleteAdminById(int aid); int updateAdminPasswordById(int aid, String password); } ``` 创建`BusinessDao.java`接口 ```java package com.doooog.elm.dao; import com.doooog.elm.po.Business; import java.util.List; public interface BusinessDao { Business getBusinessById(int bid); Business getBusinessByName(String bname); List<Business> listBusinessByName(String bname); List<Business> listBusinessAll(); Business getLastBusiness(); int addBusiness(Business business); int addBusinessWithId(Business business); int updateBusinessById(Business business); int deleteBusinessById(int bid); } ``` 创建`FoodDao.java`接口 ```java package com.doooog.elm.dao; import com.doooog.elm.po.Food; import java.util.List; public interface FoodDao { Food getFoodById(int fid); List<Food> getFoodByName(String fname); List<Food> listFoodByName(String fname); List<Food> listFoodAll(); List<Food> listFoodByBusinessId(int bid); int updateFoodById(Food food); int updateFoodPriceById(int fid, double price); int addFood(Food food); int addFoodWithId(Food food); int deleteFoodById(int fid); } ``` 新建`com.doooog.elm.dao.impl`包,创建`AdminDaoImpl.java`类 ```java package com.doooog.elm.dao.impl; import com.doooog.elm.dao.AdminDao; import com.doooog.elm.po.Admin; import com.doooog.elm.util.DBUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class AdminDaoImpl implements AdminDao { @Override public Admin getAdminByLogin(String username, String password) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Admin admin = null; try { String sql = "select * from admin where aname=? and password=?"; pst = con.prepareStatement(sql); pst.setString(1, username); pst.setString(2, password); rs = pst.executeQuery(); if (rs.next()) { admin = fillAdmin(rs); } if (admin != null && !admin.getPassword().equals(password)) { admin = null; } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return admin; } @Override public Admin getAdminById(int aid) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Admin admin = null; try { String sql = "select * from admin where aid=?"; pst = con.prepareStatement(sql); pst.setInt(1, aid); rs = pst.executeQuery(); if (rs.next()) { admin = fillAdmin(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return admin; } @Override public boolean isExistById(int aid) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; boolean result = false; try { String sql = "select count(aid) 'num' from admin where aid=?"; pst = con.prepareStatement(sql); pst.setInt(1, aid); rs = pst.executeQuery(); if (rs.next()) { int i = rs.getInt("num"); if (i > 1) result = true; } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public List<Admin> listAdminAll() { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; List<Admin> list = null; try { String sql = "select * from admin"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillAdmin(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public int addAdmin(Admin admin) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "insert into admin(aname, password) VALUES (?,?)"; pst = con.prepareStatement(sql); pst.setString(1, admin.getAname()); pst.setString(2, admin.getPassword()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int addAdminWithId(Admin admin) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "insert into admin VALUES (?,?,?)"; pst = con.prepareStatement(sql); pst.setInt(1, admin.getAid()); pst.setString(2, admin.getAname()); pst.setString(3, admin.getPassword()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int deleteAdminById(int aid) { if (aid <= 0) return 0; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "delete from admin where aid=?"; pst = con.prepareStatement(sql); pst.setInt(1, aid); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int updateAdminPasswordById(int aid, String password) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "update admin set password=? where aid=?"; pst = con.prepareStatement(sql); pst.setInt(2, aid); pst.setString(1, password); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } private Admin fillAdmin(ResultSet rs) throws SQLException { return new Admin( rs.getInt("aid"), rs.getString("aname"), rs.getString("password") ); } } ``` 创建`BusinessDaoImpl.java`类 ```java package com.doooog.elm.dao.impl; import com.doooog.elm.dao.BusinessDao; import com.doooog.elm.po.Business; import com.doooog.elm.util.DBUtil; import java.sql.*; import java.util.ArrayList; import java.util.List; public class BusinessDaoImpl implements BusinessDao { @Override public Business getBusinessById(int bid) { if (bid <= 0) return null; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Business business = null; try { String sql = "select * from business where bid=?"; pst = con.prepareStatement(sql); pst.setInt(1, bid); rs = pst.executeQuery(); if (rs.next()) { business = fillBusiness(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return business; } @Override public Business getBusinessByName(String bname) { if (bname == null || bname.isEmpty()) return null; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Business business = null; try { String sql = "select * from business where bname=?"; pst = con.prepareStatement(sql); pst.setString(1, bname); rs = pst.executeQuery(); if (rs.next()) { business = fillBusiness(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return business; } @Override public List<Business> listBusinessByName(String bname) { if (bname == null || bname.isEmpty()) return null; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; List<Business> list = null; try { String sql = "select * from business where bname=concat('%',?,'%')"; pst = con.prepareStatement(sql); pst.setString(1, bname); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillBusiness(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public List<Business> listBusinessAll() { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; List<Business> list = null; try { String sql = "select * from business"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillBusiness(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public Business getLastBusiness() { //SELECT * FROM your_table ORDER BY created_at DESC LIMIT 1; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; Business business = null; try { String sql = "select * from business order by bid desc limit 1"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); if (rs.next()) { business = fillBusiness(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return business; } @Override public int addBusiness(Business business) { if (business == null || business.isEmpty()) return 0; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; ResultSet rs = null; int result = 0; try { String sql = "insert into business(password, bname, address, `explain`, startPrice, deliveryPrice) values(?,?,?,?,?,?)"; pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); if (business.getPassword() != null && !business.getPassword().isEmpty()) { pst.setString(1, business.getPassword()); } else { pst.setNull(1, Types.VARCHAR); } if (business.getBname() != null && !business.getBname().isEmpty()) { pst.setString(2, business.getBname()); } else { pst.setNull(2, Types.VARCHAR); } if (business.getAddress() != null && !business.getAddress().isEmpty()) { pst.setString(3, business.getAddress()); } else { pst.setNull(3, Types.VARCHAR); } if (business.getExplain() != null && !business.getExplain().isEmpty()) { pst.setString(4, business.getExplain()); } else { pst.setNull(4, Types.VARCHAR); } if (business.getStartPrice() != null && business.getStartPrice() >= 0) { pst.setDouble(5, business.getStartPrice()); } else { pst.setNull(5, Types.DECIMAL); } if (business.getDeliveryPrice() != null && business.getDeliveryPrice() >= 0) { pst.setDouble(6, business.getDeliveryPrice()); } else { pst.setNull(6, Types.DECIMAL); } result = pst.executeUpdate(); rs = pst.getGeneratedKeys(); if (rs.next()) result = rs.getInt(1); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return result; } @Override public int addBusinessWithId(Business business) { if (business == null || business.isEmpty() || business.getBid() <= 0) return 0; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "insert into business values(?,?,?,?,?,?,?)"; pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); if (business.getBid() != null && business.getBid() >= 0) { pst.setInt(1, business.getBid()); } else { pst.setNull(1, Types.INTEGER); } if (business.getPassword() != null && !business.getPassword().isEmpty()) { pst.setString(2, business.getPassword()); } else { pst.setNull(2, Types.VARCHAR); } if (business.getBname() != null && !business.getBname().isEmpty()) { pst.setString(3, business.getBname()); } else { pst.setNull(3, Types.VARCHAR); } if (business.getAddress() != null && !business.getAddress().isEmpty()) { pst.setString(4, business.getAddress()); } else { pst.setNull(4, Types.VARCHAR); } if (business.getExplain() != null && !business.getExplain().isEmpty()) { pst.setString(5, business.getExplain()); } else { pst.setNull(5, Types.VARCHAR); } if (business.getStartPrice() != null && business.getStartPrice() >= 0) { pst.setDouble(6, business.getStartPrice()); } else { pst.setNull(6, Types.DECIMAL); } if (business.getDeliveryPrice() != null && business.getDeliveryPrice() >= 0) { pst.setDouble(7, business.getDeliveryPrice()); } else { pst.setNull(7, Types.DECIMAL); } result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int updateBusinessById(Business business) { Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "update business set password=?, bname=?, address=?, `explain`=?, startPrice=?, deliveryPrice=? where bid=?"; pst = con.prepareStatement(sql); pst.setString(1, business.getPassword()); pst.setString(2, business.getBname()); if (business.getAddress() != null && !business.getAddress().isEmpty()) { pst.setString(3, business.getAddress()); } else { pst.setNull(3, Types.VARCHAR); } if (business.getExplain() != null && !business.getExplain().isEmpty()) { pst.setString(4, business.getExplain()); } else { pst.setNull(4, Types.VARCHAR); } if (business.getStartPrice() != null && business.getStartPrice() >= 0) { pst.setDouble(5, business.getStartPrice()); } else { pst.setNull(5, Types.DECIMAL); } if (business.getDeliveryPrice() != null && business.getDeliveryPrice() >= 0) { pst.setDouble(6, business.getDeliveryPrice()); } else { pst.setNull(6, Types.DECIMAL); } pst.setInt(7, business.getBid()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int deleteBusinessById(int bid) { if (bid <= 0) return 0; Connection con = DBUtil.getConnection(); PreparedStatement pst = null; int result = 0; try { String sql = "delete from business where bid=?"; pst = con.prepareStatement(sql); pst.setInt(1, bid); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } private Business fillBusiness(ResultSet rs) throws SQLException { return new Business( rs.getInt("bid"), rs.getString("password"), rs.getString("bname"), rs.getString("address"), rs.getString("explain"), rs.getDouble("startPrice"), rs.getDouble("deliveryPrice") ); } } ``` 创建`FoodDaoImpl.java`类 ```java package com.doooog.elm.dao.impl; import com.doooog.elm.dao.FoodDao; import com.doooog.elm.po.Food; import com.doooog.elm.util.DBUtil; import java.sql.*; import java.util.ArrayList; import java.util.List; public class FoodDaoImpl implements FoodDao { @Override public Food getFoodById(int fid) { if (fid <= 0) return null; PreparedStatement pst = null; ResultSet rs = null; Food food = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food where fid=?"; pst = con.prepareStatement(sql); pst.setInt(1, fid); rs = pst.executeQuery(); if (rs.next()) { food = fillFood(rs); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return food; } @Override public List<Food> getFoodByName(String fname) { if (fname == null || fname.isEmpty()) return null; PreparedStatement pst = null; ResultSet rs = null; List<Food> list = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food where fname=?"; pst = con.prepareStatement(sql); pst.setString(1, fname); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillFood(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public List<Food> listFoodByName(String fname) { if (fname == null || fname.isEmpty()) return null; PreparedStatement pst = null; ResultSet rs = null; List<Food> list = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food where fname like concat('%',?,'%')"; pst = con.prepareStatement(sql); pst.setString(1, fname); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillFood(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public List<Food> listFoodAll() { PreparedStatement pst = null; ResultSet rs = null; List<Food> list = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillFood(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public List<Food> listFoodByBusinessId(int bid) { if (bid <= 0) return null; PreparedStatement pst = null; ResultSet rs = null; List<Food> list = null; Connection con = DBUtil.getConnection(); try { String sql = "select * from food where bid=?"; pst = con.prepareStatement(sql); pst.setInt(1, bid); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { list.add(fillFood(rs)); } } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, rs); } return list; } @Override public int updateFoodById(Food food) { if (food == null || food.isUnsafe(true)) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "update food set fname=?,`explain`=?,price=? where fid=?"; pst = con.prepareStatement(sql); pst.setString(1, food.getFname()); if (food.getExplain() != null && !food.getExplain().isEmpty()) { pst.setString(2, food.getExplain()); } else { pst.setNull(2, Types.VARCHAR); } pst.setDouble(3, food.getPrice()); pst.setInt(4, food.getFid()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int updateFoodPriceById(int fid, double price) { if (fid <= 0 || price <= 0) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "update food set price=? where fid=?"; pst = con.prepareStatement(sql); pst.setDouble(1, price); pst.setInt(2, fid); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int addFood(Food food) { if (food == null || food.isUnsafe(false)) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "insert into food(fname, `explain`, price, bid) VALUES (?,?,?,?)"; pst = con.prepareStatement(sql); pst.setString(1, food.getFname()); if (food.getExplain() != null && !food.getExplain().isEmpty()) { pst.setString(2, food.getExplain()); } else { pst.setNull(2, Types.VARCHAR); } pst.setDouble(3, food.getPrice()); pst.setInt(4, food.getBid()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int addFoodWithId(Food food) { if (food == null || food.isUnsafe(true)) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "insert into food VALUES (?,?,?,?,?)"; pst = con.prepareStatement(sql); pst.setInt(1, food.getFid()); pst.setString(2, food.getFname()); if (food.getExplain() != null && !food.getExplain().isEmpty()) { pst.setString(3, food.getExplain()); } else { pst.setNull(3, Types.VARCHAR); } pst.setDouble(4, food.getPrice()); pst.setInt(5, food.getBid()); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } @Override public int deleteFoodById(int fid) { if (fid <= 0) return 0; PreparedStatement pst = null; int result = 0; Connection con = DBUtil.getConnection(); try { String sql = "delete from food where fid=?"; pst = con.prepareStatement(sql); pst.setInt(1, fid); result = pst.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { DBUtil.closeConnection(con, pst, null); } return result; } private Food fillFood(ResultSet rs) throws SQLException { return new Food( rs.getInt("fid"), rs.getString("fname"), rs.getString("explain"), rs.getDouble("price"), rs.getInt("bid") ); } } ``` #### 4、`controller`层 新建`com.doooog.elm.controller`包,创建`AdminCtrl.java`接口 ```java package com.doooog.elm.controller; import com.doooog.elm.po.Admin; import com.doooog.elm.po.Business; public interface AdminCtrl { // 管理员部分 boolean login(String username, String password); boolean addAdmin(Admin admin); boolean deleteAdminById(int aid); void listAdminAll(); boolean updateAdminPasswordById(int aid, String oldPW, String newPW); boolean getAdminById(int aid); // 商家部分 boolean addBusiness(Business business); boolean deleteBusinessById(int bid); boolean getBusinessById(int bid); void listBusinessAll(); boolean updateBusinessPasswordById(int bid, String oldPW, String newPW); boolean updateBusinessNameById(int bid, String name); boolean updateBusinessAddressById(int bid, String address); boolean updateBusinessExplainById(int bid, String explain); boolean updateBusinessPriceById(int bid,double startPrice, double deliveryPrice); } ``` 新建`com.doooog.elm.controller.impl`包,创建`AdminCtrlImpl.java`类 ```java package com.doooog.elm.controller.impl; import com.doooog.elm.controller.AdminCtrl; import com.doooog.elm.dao.AdminDao; import com.doooog.elm.dao.BusinessDao; import com.doooog.elm.dao.impl.AdminDaoImpl; import com.doooog.elm.dao.impl.BusinessDaoImpl; import com.doooog.elm.po.Admin; import com.doooog.elm.po.Business; import java.util.List; public class AdminCtrlImpl implements AdminCtrl { public boolean login(String username, String password) { if (username == null || password == null || username.isEmpty() || password.isEmpty()) return false; AdminDao adminDao = new AdminDaoImpl(); Admin admin = adminDao.getAdminByLogin(username, password); return admin != null && !admin.isEmpty(); } @Override public boolean addAdmin(Admin admin) { if (admin == null) return false; if ((admin.getAid() == null || admin.getAid() <= 0) && !admin.isUnsafe(false)) { AdminDao adminDao = new AdminDaoImpl(); return adminDao.addAdmin(admin) == 1; } else if (!admin.isUnsafe(true)) { AdminDao adminDao = new AdminDaoImpl(); return adminDao.addAdminWithId(admin) == 1; } return false; } @Override public boolean deleteAdminById(int aid) { if (aid <= 0) return false; AdminDao adminDao = new AdminDaoImpl(); return adminDao.deleteAdminById(aid) == 1; } @Override public void listAdminAll() { AdminDao adminDao = new AdminDaoImpl(); List<Admin> list = adminDao.listAdminAll(); if (list.isEmpty()) { System.out.println("未查询到信息"); return; } for (Admin admin : list) { System.out.println(admin); } } @Override public boolean updateAdminPasswordById(int aid, String oldPW, String newPW) { if (aid <= 0 || oldPW == null || oldPW.isEmpty() || newPW == null || newPW.isEmpty()) return false; AdminDao adminDao = new AdminDaoImpl(); Admin admin = adminDao.getAdminById(aid); if (admin == null || !admin.getPassword().equals(oldPW)) return false; return adminDao.updateAdminPasswordById(aid, newPW) == 1; } @Override public boolean getAdminById(int aid) { if (aid <= 0) { System.out.println("输入ID有误>0"); return false; } AdminDao adminDao = new AdminDaoImpl(); Admin admin = adminDao.getAdminById(aid); if (admin == null) { System.out.println("未查询到信息"); return false; } System.out.println(admin); return true; } @Override public boolean addBusiness(Business business) { if (business == null) return false; if ((business.getBid() == null || business.getBid() <= 0) && !business.isUnsafe(false)) { BusinessDao businessDao = new BusinessDaoImpl(); int rs = businessDao.addBusiness(business); business.setBid(rs); return rs != 0; } else if (!business.isUnsafe(true)) { BusinessDao businessDao = new BusinessDaoImpl(); int rs = businessDao.addBusinessWithId(business); business.setBid(rs); return rs != 0; } return false; } @Override public boolean deleteBusinessById(int bid) { if (bid <= 0) return false; BusinessDao businessDao = new BusinessDaoImpl(); return businessDao.deleteBusinessById(bid) == 1; } @Override public boolean getBusinessById(int bid) { if (bid <= 0) { System.out.println("输入ID有误>0"); return false; } BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) { System.out.println("未查询到信息"); return false; } System.out.println(b); return true; } @Override public void listBusinessAll() { BusinessDao businessDao = new BusinessDaoImpl(); List<Business> list = businessDao.listBusinessAll(); if (list.isEmpty()) { System.out.println("未查询到信息"); return; } for (Business b : list) { System.out.println(b); } } @Override public boolean updateBusinessPasswordById(int bid, String oldPW, String newPW) { if (bid <= 0 || oldPW == null || oldPW.isEmpty() || newPW == null || newPW.isEmpty()) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null || !b.getPassword().equals(oldPW)) return false; b.setPassword(newPW); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } @Override public boolean updateBusinessNameById(int bid, String name) { if (bid <= 0 || name == null || name.isEmpty()) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) return false; b.setBname(name); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } @Override public boolean updateBusinessAddressById(int bid, String address) { if (bid <= 0 || address == null || address.isEmpty()) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) return false; b.setAddress(address); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } @Override public boolean updateBusinessExplainById(int bid, String explain) { if (bid <= 0 || explain == null || explain.isEmpty()) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) return false; b.setExplain(explain); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } @Override public boolean updateBusinessPriceById(int bid, double startPrice, double deliveryPrice) { if (bid <= 0 || startPrice <= 0 || deliveryPrice <= 0) return false; BusinessDao businessDao = new BusinessDaoImpl(); Business b = businessDao.getBusinessById(bid); if (b == null) return false; b.setStartPrice(startPrice); b.setDeliveryPrice(deliveryPrice); if (!b.isUnsafe(true)) return businessDao.updateBusinessById(b) == 1; return false; } } ``` #### 5、`view`层 新建`com.doooog.elm.view`包,创建`MainEntry.java`类作为程序入口。 ```java package com.doooog.elm.view; import com.doooog.elm.util.CommonUtil; import com.doooog.elm.util.InputUtil; public class MainEntry { public static void main(String[] args) { System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 欢迎进入饿了么后台管理系统 --|"); MainEntry main = new MainEntry(); main.menuMain(); } public void menuMain() { System.out.println("|-- 1.系统管理员 2.商家"); System.out.print("|-- 请输入编号(-1退出):"); int choice = InputUtil.menuInputInteger("(整数)"); switch (choice) { case 1 -> new AdminView().menuLogin(); case 2 -> new BusinessView().menuLogin(); default -> { } } } } ``` 创建`AdminView.java`类 ```java package com.doooog.elm.view; import com.doooog.elm.controller.impl.AdminCtrlImpl; import com.doooog.elm.po.Admin; import com.doooog.elm.po.Business; import com.doooog.elm.util.CommonUtil; import com.doooog.elm.util.InputUtil; public class AdminView { private AdminCtrlImpl ctrl = new AdminCtrlImpl(); public void menuLogin() { System.out.println("|-- 请使用管理员身份进行登录 --|"); System.out.print("|-- 请输入用户名:"); String username = InputUtil.menuInputString(""); System.out.print("|-- 请输入密码:"); String password = InputUtil.menuInputString(""); if (ctrl.login(username, password)) { System.out.println("登录成功!"); menuMain(); } else { System.out.println("登录失败,请检查账号或密码是否有误!"); } } public void menuMain() { boolean exit = false; while (!exit) { System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 管理员菜单 --|"); System.out.println("|-- 管理员操作-----------------"); System.out.println("|-- 1.查看所有管理员"); System.out.println("|-- 2.新增管理员"); System.out.println("|-- 3.更改管理员密码"); System.out.println("|-- 4.删除管理员"); System.out.println("|-- 商家操作------------------"); System.out.println("|-- 5.查看所有商家"); System.out.println("|-- 6.新增商家"); System.out.println("|-- 7.更改商家密码"); System.out.println("|-- 8.更改商家名称"); System.out.println("|-- 9.更改商家地址"); System.out.println("|-- 10.更改商家介绍"); System.out.println("|-- 11.更改商家配送价格"); System.out.println("|-- 12.删除商家"); System.out.println("|-- 0.退出系统"); System.out.println(CommonUtil.copyChar('-', 30)); System.out.print("|-- 请输入编号(0~12整数):"); int choice = InputUtil.menuInputInteger("(0~12整数)"); switch (choice) { case 0 -> exit = true; case 1 -> ctrl.listAdminAll(); case 2 -> menuAddAdmin(); case 3 -> menuRePasswordAdmin(); case 4 -> menuDeleteAdmin(); case 5 -> ctrl.listBusinessAll(); case 6 -> menuAddBusiness(); case 7 -> menuRePasswordBusiness(); case 8 -> menuRename(); case 9 -> menuReAddress(); case 10 -> menuReExplain(); case 11 -> menuRePrice(); case 12 -> menuDeleteBusiness(); } } } public void menuAddAdmin() { Admin admin = new Admin(); int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- ID(整数):"); tmp = InputUtil.menuInputInteger("(请输入整数,或0跳过)"); if (tmp == -1) return; if (tmp != 0) admin.setAid(tmp); System.out.print("|-- 账号(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; admin.setAname(tmp2); System.out.print("|-- 密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; admin.setPassword(tmp2); if (ctrl.addAdmin(admin)) { System.out.println("添加成功!"); return; } System.out.println("添加失败,请检查输入是否有误,ID可以为空"); } public void menuRePasswordAdmin() { Admin admin = new Admin(); int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)提取方法…"); if (tmp == -1 || tmp == 0) return; admin.setAid(tmp); System.out.print("|-- 旧密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; admin.setAname(tmp2); System.out.print("|-- 新密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; admin.setPassword(tmp2); if (ctrl.updateAdminPasswordById(admin.getAid(), admin.getAname(), admin.getPassword())) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuDeleteAdmin() { int tmp; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要删除的ID(0以上整数)(必填):"); tmp = InputUtil.menuInputInteger("(0以上整数)"); if (tmp == -1) return; if (!ctrl.getAdminById(tmp)) return; if (!doChoice()) return; if (ctrl.deleteAdminById(tmp)) { System.out.println("删除成功!"); return; } System.out.println("删除失败,未知错误"); } public void menuAddBusiness() { Business business = new Business(); double tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 名称(必填):"); tmp2 = InputUtil.menuInputString("(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; business.setBname(tmp2); System.out.print("|-- 密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; business.setPassword(tmp2); System.out.print("|-- 地址:"); tmp2 = InputUtil.menuInputString(""); if (tmp2.equals("-1")) return; if (!tmp2.equals("0")) business.setAddress(tmp2); System.out.print("|-- 介绍:"); tmp2 = InputUtil.menuInputString(""); if (tmp2.equals("-1")) return; if (!tmp2.equals("0")) business.setExplain(tmp2); System.out.print("|-- 起送价格:"); tmp = InputUtil.menuInputDouble(""); if (tmp == -1) return; if (tmp != 0) business.setStartPrice(tmp); System.out.print("|-- 配送价格:"); tmp = InputUtil.menuInputDouble(""); if (tmp == -1) return; if (tmp != 0) business.setDeliveryPrice(tmp); System.out.print("|-- 确认信息:"); System.out.println(business); if (!doChoice()) return; if (ctrl.addBusiness(business)) { System.out.println("添加成功!"); System.out.println("您用于登录的编号为:" + business.getBid()); return; } System.out.println("添加失败,请检查输入是否有误"); } public void menuRePasswordBusiness() { Business business = new Business(); int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; business.setBid(tmp); System.out.print("|-- 旧密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; business.setBname(tmp2); System.out.print("|-- 新密码(字母+数字)(必填):"); tmp2 = InputUtil.menuInputString("(字母+数字)(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; business.setPassword(tmp2); if (ctrl.updateBusinessPasswordById(business.getBid(), business.getBname(), business.getPassword())) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuRename() { int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; System.out.print("|-- 新名称(必填):"); tmp2 = InputUtil.menuInputString("(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; if (ctrl.updateBusinessNameById(tmp, tmp2)) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuReAddress() { int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; System.out.print("|-- 新地址(必填):"); tmp2 = InputUtil.menuInputString("(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; if (ctrl.updateBusinessAddressById(tmp, tmp2)) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuReExplain() { int tmp; String tmp2; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; System.out.print("|-- 新介绍(必填):"); tmp2 = InputUtil.menuInputString("(必填)"); if (tmp2.equals("-1") || tmp2.equals("0")) return; if (ctrl.updateBusinessExplainById(tmp, tmp2)) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuRePrice() { int tmp; double tmp2, tmp3; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要更改的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; System.out.print("|-- 新起送价(大于0的小数)(必填):"); tmp2 = InputUtil.menuInputDouble("(大于0的小数)(必填)"); if (tmp2 == -1 || tmp2 == 0) return; System.out.print("|-- 新配送价(大于0的小数)(必填):"); tmp3 = InputUtil.menuInputDouble("(大于0的小数)(必填)"); if (tmp3 == -1 || tmp3 == 0) return; if (ctrl.updateBusinessPriceById(tmp, tmp2, tmp3)) { System.out.println("修改成功!"); return; } System.out.println("修改失败,请检查输入是否有误"); } public void menuDeleteBusiness() { int tmp; System.out.println(CommonUtil.copyChar('=', 30)); System.out.println("|-- 没有(必填)项输入0可跳过,输入-1返回上级菜单"); System.out.print("|-- 需要删除的ID(整数)(必填):"); tmp = InputUtil.menuInputInteger("(整数)(必填)"); if (tmp == -1 || tmp == 0) return; if (!ctrl.getBusinessById(tmp)) return; if (!doChoice()) return; if (ctrl.deleteBusinessById(tmp)) { System.out.println("删除成功!"); return; } System.out.println("删除失败,请检查输入是否有误"); } public boolean doChoice() { System.out.print("|-- 确认无误(1.是,2.否):"); int tmp = InputUtil.menuInputInteger("(1.是,2.否)"); return tmp == 1; } } ``` 最后修改:2025 年 10 月 22 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 👆请简单粗暴地爱我。