华清远见-重庆中心-数据库阶段技术总结:

数据库DataBase

简称为DB,是结构化管理数据的仓库,称为电子化的文件柜。

数据库管理系统DataBase Manager System

简称DMBS,安装在操作系统上,管理数据库的软件。

数据库的分类

关系型数据库

SQLServer

MySQL

Oracle

非关系型数据库nosql

Redis

SQL语句

在关系型数据库中,用于操作数据库的结构化查询语言。

查看信息

查看所有数据库:show databases;

查看所有表:show tables;

查看表结构:desc 表名;

查看建表语言:show create table 表名;

数据库操作

创建数据库:create database 数据库名:

使用数据库:use 数据库名:

删除数据库:drop database 数据库名;

数据表操作

数据类型

常用数据类型
int整型
double浮点型
char(长度)定义长字符串
varchar(长度)可变字符串
text文本
date日期
time时间
datetime日期时间
timestamp时间毫秒

数据完整性

数据完整性指数据精确可靠。

完整性约束关键字作用
非空约束not null保证字段不为空
主键约束primary key保证字段不重复,用于唯一区分每条记录
唯一约束(索引)unique保证字段重复
默认值约束default保证字段在没有填充数据时,自动使用默认值填充
外键约束foreign key references保证从表中的记录只能来自于主表
  • 创建数据表
create table 表名(
字段名 数据类型 [是否为空|唯一约束|主键约束|默认值约束|自动递增],
字段名 数据类型 [是否为空|唯一约束|默认值约束],
...
字段名 数据类型 [是否为空|唯一约束|默认值约束]
)
  • 修改数据表

    • 表重命名:alter table 表名 rename to 新表名;

    • 添加新字段:alter table 表名 add column 字段名 数据类型 字段特征;

    • 修改字段:alter table 表名 change 旧字段 新字段 数据类型 字段特征;

    • 删除字段:alter table 表名 drop 字段名;

  • 删除数据表:drop table 表名

    • 删除时如果有外键约束,先删除从表,再删除主表
  • 添加约束

    • 添加主键约束:alter table 表名 add primary key(字段名)

    • 添加唯一约束:alter table 表名 add unique(字段名)

    • 添加默认值约束:alter table 表名 alter 字段名 set default '值'

    • 添加外键约束:alter table 从表 add foreign key(字段名) references 主表(主键字段)

  • 删除约束:alter table 表名 drop [primary key|index/foreign key 约束名]

数据操作

  • 增加

    • insert into 表名 values(值1,值2...);保证按表中字段顺序添加数据,不能缺少任何一个字段

      • 自增字段用null

      • 默认值字段用default

      • 允许为空字段用null

    • insert into 表名(字段1,字段2....) values(值1,值2...);保证必须写上非空字段,值的顺序和字段顺序保持一致

  • 修改

    • update 表名 set 字段=值,字段=值 [where 条件]
  • 删除

    • delete from 表名 [where 条件];会保留自增列的值

    • truncate table 表名;会重置自增列的值

  • 查询

    • select [字段|*] from 表名 [where 条件] [order by 字段] [having 条件]

    • limit N:查询前N条记录

    • limit N,M:从索引为N的开始查询M条记录

    • distinct:去重复

    • order by 字段1,字段2:多字段排序

    • group by 字段名:根据字段分组

    • having 统计函数条件

条件符号
指定值=、!=、<>
指定范围>、<、>=、=<、between值and值
指定集合[not] in (元素1,元素2...)
模糊查询(%表示未知长度字符串,_表示一个字符)like '%值'
空置is [not] null
多条件使用 and or & \

常用函数

  • 统计函数

    • sum():求和

    • avg():平均

    • count():统计数量

    • max():最大值

    • min():最小值

  • 字符串函数concat('值','值',字段,):拼接值或字段

    • trim():去除首尾空格

    • left(length)/right(length):从左/右开始截取指定长度字符串

  • 数学函数

    • round()/ceil()/floor():取整

    • abs():绝对值

  • 时间函数

    • now()/curdate()curtime():得到当前日期时间

    • year()/month()/day():得到日期中的指定部分

    • datediff()/timediff()/timestampdiff():计算时间间隔

  • 补充

    • if(条件,表达式1,表达式2):条件为真,结果为表达式1,条件为假,结果为表达式2

    • group_concat():拼接分组后的其他字段

嵌套查询

select * from 表 where 字段=(select * from 表 where 条件);

select * from 表,(select * from 表)t where 表.字段=t.字段;

多表查询

  • 交叉连接/笛卡尔积

    select * from 表1,表2...;
    
    select * from 表1 cross/inner join 表2;
  • 内连接

    select * from 表1,表2 where 表1.字段=表2.字段;
    
    select * from 表1 cross/inner join 表2 on 表1.字段=表2.字段;
  • 外连接

    • 左外连接

      select * from 表1 left join 表2 on 表1.字段=表2.字段;
      -- 保证表1中的数据完整
    • 右外连接

      select * from 表1 right join 表2 on 表1.字段=表2.字段;
      -- 保证表2中的数据完整

数据库设计

实体关系模型

ER图

  • 矩形:实体

  • 椭圆形:实体的属性

  • 菱形:实体之间的关系

实体关系分类

  • 一对一:一个国家有一个领导人

    • 1.根据两个实体创建两张表,都加上主键

    • 2.在其中一张表中添加一个字段,保存另一张表的主键并设置唯一

  • 一对多/多对一:一个班级有多个学生,一个学生不能有多个班级

    • 1.先根据两个实体创建两张表,都加上主键

    • 2.在从表中添加外键字段管理主表中的主键字段

  • 多对多:一个学生学习多门课程,一门课程可以被多个学生学习

    • 1.先根据两个实体创建两张表,都加上主键

    • 2.创建第三张"关系"表,在该表中添加两个字段分别保存两个实体表中的主键

范式

第一范式1NF

字段不可再分

第二范式2NF

在满足1NF的基础上,消除部分依赖。

对于联合主键而言,所有非主属性字段必须完全依赖于主属性。

第三范式3NF

在满足2NF的基础上,消除传递依赖。

JDBC

Java提供了一套规范用于连接各种数据库。不同的数据库厂商根据该规范设计连接驱动。

java.sql包下一组接口用于操作数据库。

  • Connection接口 用于获取连接对象

  • PreparedStatement接口 用于发送sql语句,处理sql语句

  • ResultSet接口 用于保存查询后的结果集

加载MySQL驱动

Class.forName("com.mysql.cj.jdbc.Driver");

连接MySQL数据库字符串

String url="jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai";

String username="root";

String password="root";

查询的步骤

1.获取连接对象Connection

2.构造sql语句String

3.预处理sql语句pst=conn.prepareStatement(sql)

4.给sql语句中的?赋值pst.setXXX(?顺序,值)

5.调用executeQuery()得到ResultSet结果集

6.遍历结果集rs.next()后rs.getXXX(字段顺序/字段名)

7.释放资源

增删改的步骤

1.获取连接对象Connection

2.构造sql语句String

3.预处理sql语句pst=conn.prepareStatement(sql)

4.给sql语句中的?赋值pst.setXXX(?顺序,值)

5.调用executeUpdate()得到受影响的行数

6.释放资源

事务transaction

一组sql执行单元,要么全部执行,要么全部不执行

事务的特性ACID

原子性Atomicity

事务是最小的执行单元

一致性Consistency

事务执行前后,数据整体保持一致。

隔离性Isolation

各个事务之间应当互不干涉

持久性Durability

事务一旦提交,改变是永久的

事务并发出现的问题

脏读

事务A读取到了事务B未提交的数据。

不可重复读

事务A中前后两次读取到的数据不一致。

事务A在读取过程中,事务B对数据进行了修改。

幻读

事务A在读取过程中,事务B向其中添加了数据,导致事务A读到了事务B中添加的"幻影"数据。

以上问题可以通过设置事务隔离级别解决。

事务隔离级别

隔离级别能否出现脏读能否出现不可重复读能否出现幻读
Read Uncommitted未提交读
Read Committed已提交读不会
Repeatable Read可重复读(MySQL默认)不会不会
Serializable序列化不会不会不会

事务相关指令

  • 查看事务自动提交状态

    select @@autocommit

  • 修改事务自动提交状态

    set @@autocommit=0/1 0表示不开启动,1表示开启

  • 手动开启事务

    start transaction;

  • 提交

    commit;

  • 回滚

    rollback;

  • 查询当前事务隔离级别

    select @@transaction_isolation

  • 设置事务隔离级别

    set [session|global] transaction isolation level [Read Uncommitted | Read Committed |

    Repeatable Read |Serializable]

视图

定义

create view 视图名 as

sql语句;

使用

select * from 视图名;

触发器

定义

create trigger 触发器名

触发时机 触发条件 on 表 for each row

begin

    触发器触发时执行的sql语句;

end

存储过程

创建

create procedure 存储过程名(in/out/inout 参数名 数据类型)

begin

    sql语句;

end

调用

call 存储过程名(参数);

数据库的应用

Util类

package com.hqyj.util;

import java.sql.*;

public class DBUtil {
    /*
     * 数据库工具类
     * 连接方法
     * 释放资源方法
     * */
    private static String url = "jdbc:mysql://localhost:3306/shopdb?serverTimezone=Asia/Shanghai";
    private static String username = "root";
    private static String password = "root";

    /*
     * 静态代码块
     * 加载驱动
     * */
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动异常" + e);
        }
    }

    /*
     * 静态方法
     * 获取连接
     * */
    public static Connection getConn() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            System.out.println("连接数据库异常" + e);
        }
        return connection;
    }

    /*
     * 静态方法
     * 释放资源
     * */
    public static void release(Connection conn, PreparedStatement pst, ResultSet rs) {
        try {
            if (conn != null) {
                conn.close();
            }
            if (pst != null) {
                pst.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            System.out.println("释放资源异常" + e);
        }
    }
}

entity类

管理员属性

package com.hqyj.entity;

import com.mysql.cj.x.protobuf.MysqlxDatatypes;

public class Admin {
    private int adminId;
    private String pwd;

    public Admin(int adminId, String pwd) {
        this.adminId = adminId;
        this.pwd = pwd;
    }


    @Override
    public String toString() {
        return "Admin{" +
                "adminId=" + adminId +
                ", pwd='" + pwd + '\'' +
                '}';
    }

    public int getAdminId() {
        return adminId;
    }

    public void setAdminId(int adminId) {
        this.adminId = adminId;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

客户属性

package com.hqyj.entity;

public class Customer {
    private String phone;
    private String password;
    private double balance;
    private String sex;

    public Customer(String phone, String password, double balance, String sex) {
        this.phone = phone;
        this.password = password;
        this.balance = balance;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "CustomerDao{" +
                "phone='" + phone + '\'' +
                ", password='" + password + '\'' +
                ", balance=" + balance +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

商品属性

package com.hqyj.entity;

public class Goods {
    private int shopId;
    private String shopName;
    private double price;
    private int shopNum;
    private String date;

    public Goods(int shopId, String shopName, double price, int shopNum, String date) {
        this.shopId = shopId;
        this.shopName = shopName;
        this.price = price;
        this.shopNum = shopNum;
        this.date = date;
    }

    public Goods(String shopName, double price, int shopNum) {
        this.shopName = shopName;
        this.price = price;
        this.shopNum = shopNum;
    }

    @Override
    public String toString() {
        return "商品" +
                "商品号:" + shopId +"\t"+
                "商品名称:" + shopName +"\t"+
                "商品价格:" + price +"\t"+"\t"+
                "商品库存:" + shopNum +"\t"+
                "生产日期:" + date
                ;
    }

    public int getShopId() {
        return shopId;
    }

    public void setShopId(int shopId) {
        this.shopId = shopId;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getShopNum() {
        return shopNum;
    }

    public void setShopNum(int shopNum) {
        this.shopNum = shopNum;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

dao类

管理员

package com.hqyj.dao;

import com.hqyj.entity.Admin;
import com.hqyj.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDao {
    Connection conn;
    PreparedStatement pst;
    ResultSet rs;

    public Admin login(int adminId, String pwd) {
        conn = DBUtil.getConn();
        String sql = "select * from admin where admin_id=? and pwd=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, adminId);
            pst.setString(2, pwd);
            rs = pst.executeQuery();
            if (rs.next()) {
                Admin admin = new Admin(adminId, pwd);
                return admin;
            }
        } catch (SQLException e) {
            System.out.println("登录异常" + e);
        } finally {
            DBUtil.release(conn, pst, rs);
        }
        return null;
    }

}

客户

package com.hqyj.dao;

import com.hqyj.entity.Customer;
import com.hqyj.util.DBUtil;
import sun.security.util.Password;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CustomerDao {
    Connection conn;
    PreparedStatement pst;
    ResultSet rs;

    public Customer userLogin(String phone,String password){
        conn= DBUtil.getConn();
        String sql="select * from customer where phone=? and password=?";
        try {
            pst=conn.prepareStatement(sql);
            pst.setString(1,phone);
            pst.setString(2,password);
            rs=pst.executeQuery();
            if (rs.next()){
                double balance =rs.getDouble(3);
                String sex =rs.getString(4);
                Customer customer = new Customer(phone,password,balance,sex);
                return customer;
            }
        }catch (SQLException e){
            System.out.println("登录异常"+e);
        }finally {
            DBUtil.release(conn,pst,rs);
        }
        return null;
    }
    public boolean register(String phone,String password){
        conn=DBUtil.getConn();
        try {
            String sql1="select * from customer where phone=?";
            pst = conn.prepareStatement(sql1);
            pst.setString(1,phone);
            rs = pst.executeQuery();
            if (rs.next()){
                if (rs.getInt(1)==1){
                    return false;
                }
            }
            String sql2="insert into customer values(?,?,null,null)";
            pst = conn.prepareStatement(sql2);
            pst.setString(1,phone);
            pst.setString(2,password);
            if (pst.executeUpdate()>0){
                return true;
            }
        }catch (SQLException e){
            System.out.println("注册异常"+e);
        }finally {
            DBUtil.release(conn,pst,rs);
        }
        return false;
    }
    public void buy(int shopId,int buyNum,String phone,double cost){
        conn=DBUtil.getConn();
        try {
            String sql1="update goodsinfo set shop_num=shop_num-? where shop_id=?";
            pst = conn.prepareStatement(sql1);
            pst.setInt(1,buyNum);
            pst.setInt(2,shopId);
            pst.executeUpdate();
            String sql2="update customer set balance=balance-? where phone=?";
            pst=conn.prepareStatement(sql2);
            pst.setDouble(1,cost);
            pst.setString(2,phone);
            pst.executeUpdate();
        }catch (SQLException e){
            System.out.println("购买异常"+e);
        }finally {
            DBUtil.release(conn,pst,rs);
        }
    }
    public void userMod(Customer customer){
        conn=DBUtil.getConn();
        try {
            String sql = "update customer set  password=?,balance=?,sex = ? where phone=?";
            pst=conn.prepareStatement(sql);
            pst.setString(1,customer.getPassword());
            pst.setDouble(2,customer.getBalance());
            pst.setString(3,customer.getSex());
            pst.setString(4,customer.getPhone());
            pst.executeUpdate();
        }catch (SQLException e){
            System.out.println("修改异常"+e);
        }finally {
            DBUtil.release(conn,pst,rs);
        }
    }

}

商品

package com.hqyj.dao;

import com.hqyj.entity.Goods;
import com.hqyj.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 GoodsDao {
    Connection conn;
    PreparedStatement pst;
    ResultSet rs;

    public List<Goods> queryAll() {
        ArrayList<Goods> list = new ArrayList<>();
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement("select * from goodsinfo");
            rs = pst.executeQuery();
            while (rs.next()) {
                int shopId = rs.getInt(1);
                String shopName = rs.getString(2);
                double price = rs.getDouble(3);
                int shopNum = rs.getInt(4);
                String date = rs.getString(5);
                Goods goods = new Goods(shopId, shopName, price, shopNum, date);
                list.add(goods);
            }
        } catch (Exception e) {
            System.out.println("查询异常" + e);
        } finally {
            DBUtil.release(conn, pst, rs);
        }
        return list;
    }

    public Goods queryById(int shopId) {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement("select * from goodsinfo where shop_id=?");
            pst.setInt(1, shopId);
            rs = pst.executeQuery();
            if (rs.next()) {
                String shopName = rs.getString(2);
                double price = rs.getDouble(3);
                int shopNum = rs.getInt(4);
                String date = rs.getString(5);
                Goods goods = new Goods(shopId, shopName, price, shopNum, date);
                return goods;
            }
        } catch (Exception e) {
            System.out.println("根据id查询异常" + e);
        } finally {
            DBUtil.release(conn, pst, rs);
        }
        return null;
    }

    public boolean insert(Goods goods) {
        conn = DBUtil.getConn();
        try {
            pst = conn.prepareStatement("insert into goodsinfo values(null,?,?,?,curdate()) ");
            pst.setString(1, goods.getShopName());
            pst.setDouble(2, goods.getPrice());
            pst.setInt(3, goods.getShopNum());
            return pst.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("添加异常" + e);
        } finally {
            DBUtil.release(conn, pst, rs);
        }
        return false;
    }

    public boolean update(int shopId, double newPrice, int newNum) {
        conn = DBUtil.getConn();
        try {
            pst = conn.prepareStatement("update goodsinfo set price=?,shop_num=? where shop_id=?");
            pst.setDouble(1, newPrice);
            pst.setInt(2, newNum);
            pst.setInt(3, shopId);
            return pst.executeUpdate() > 0;
        } catch (Exception e) {
            System.out.println("修改异常" + e);
        }
        return false;
    }

    public boolean delete(int shopId) {
        conn = DBUtil.getConn();
        try {
            pst = conn.prepareStatement("delete from goodsinfo where shop_id=?");
            pst.setInt(1, shopId);
            return pst.executeUpdate() > 0;
        } catch (Exception e) {
            System.out.println("删除异常" + e);
        }
        return false;
    }

}

service

AdminService

package com.hqyj.Service;

import com.hqyj.dao.GoodsDao;
import com.hqyj.entity.Goods;

import java.util.List;
import java.util.Scanner;

public class AdminService {
    Scanner sc = new Scanner(System.in);
    GoodsDao gd = new GoodsDao();

    public void menu() {
        while (true) {
            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("5.删除商品");
            System.out.println("6.退出");
            System.out.println("===============");
            System.out.println("请输入选项:");
            switch (sc.nextInt()) {
                case 1:
                    List<Goods> goodsInfos = gd.queryAll();
                    for (Goods goodsInfo : goodsInfos) {
                        System.out.println(goodsInfos);
                    }
                    break;
                case 2:
                    System.out.println("请输入需要查询的商品编号:");
                    int i = sc.nextInt();
                    System.out.println(gd.queryById(i));
                    break;
                case 3:
                    System.out.println("请输入商品名称:");
                    String name = sc.next();
                    System.out.println("请输入商品价格:");
                    double price = sc.nextInt();
                    System.out.println("请输入商品库存:");
                    int num = sc.nextInt();
                    Goods goods = new Goods(name, price, num);
                    if (gd.insert(goods)) {
                        System.out.println("添加成功!");
                    } else {
                        System.out.println("添加失败!");
                    }
                    break;
                case 4:
                    gd.queryAll();
                    System.out.println("请输入需要修改的商品编号:");
                    int a = sc.nextInt();
                    System.out.println("请输入新的价格:");
                    int c = sc.nextInt();
                    System.out.println("请输入新的库存:");
                    int d = sc.nextInt();
                    if (gd.update(a, c, d)) {
                        System.out.println("修改成功!");
                    } else {
                        System.out.println("修改失败!");
                    }
                    break;
                case 5:
                    gd.queryAll();
                    System.out.println("请输入需要删除的商品编号:");
                    int id = sc.nextInt();
                    if (gd.delete(id)) {
                        System.out.println("删除成功!");
                    } else {
                        System.out.println("删除失败!");
                    }
                    break;
                case 6:
                    System.out.println("谢谢使用!");
                    System.exit(0);

            }
        }
    }
}

CustomerService

package com.hqyj.Service;

import com.hqyj.dao.CustomerDao;
import com.hqyj.dao.GoodsDao;
import com.hqyj.entity.Customer;
import com.hqyj.entity.Goods;

import java.util.List;
import java.util.Scanner;

public class CustomerService {
    Scanner sc=new Scanner(System.in);
    CustomerDao customerDao = new CustomerDao();
    GoodsDao goodsDao = new GoodsDao();

    public void useMod(){
        System.out.println("请选择功能");
        System.out.println("1.注册");
        System.out.println("2.登录");
        switch (sc.nextInt()) {
            case 1:
                System.out.println("请输入电话");
                String phone = sc.next();
                System.out.println("请输入密码");
                String password = sc.next();
                //调用注册
                boolean register = customerDao.register(phone, password);
                if (register) {
                    System.out.println("注册成功,请登录");

                } else {
                    System.out.println("该用户已注册");
                }

                break;
            case 2:
                System.out.println("请输入电话");
                String loginPhone = sc.next();
                System.out.println("请输入密码");
                String loginPwd = sc.next();
                //调用登录
                Customer customer = customerDao.userLogin(loginPhone, loginPwd);
                if (customer != null) {
                    //进入客户菜单
                    Menu(customer);
                } else {
                    System.out.println("用户名或密码错误");
                }
                break;
        }
    }
    public void Menu(Customer customer){
        System.out.println("请选择功能:");
        System.out.println("1.修改个人信息");
        System.out.println("2.查看所有商品");
        System.out.println("3.购买商品");
        System.out.println("4.充值");
        System.out.println("5.修改密码");
        System.out.println("6.退出");
        switch (sc.nextInt()){
            case 1:
                System.out.println("请输入你的性别");
                String sex = sc.next();
                customer.setSex(sex);
                customerDao.userMod(customer);
                System.out.println("修改成功!");
                Menu(customer);
                break;
            case 2:
                List<Goods> goodsInfos = goodsDao.queryAll();
                for (Goods goodsInfo : goodsInfos) {
                    System.out.println(goodsInfo);
                }
                break;
            case 3:
                System.out.println("请输入商品编号");
                int shopId = sc.nextInt();
                Goods goods  = goodsDao.queryById(shopId);
                System.out.println("请输入购买数量");
                int buyNum = sc.nextInt();
                if (buyNum > goods.getShopNum()) {
                    System.out.println("库存不足");
                    Menu(customer);
                }
                if (customer.getBalance() < buyNum * goods.getPrice()) {
                    System.out.println("余额不足");
                    Menu(customer);

                }
                customerDao.buy(shopId, buyNum, customer.getPhone(), buyNum * goods.getPrice());
                System.out.println("购买成功");
                System.out.println("商品名:+" + goods.getShopName() );
                System.out.println("数量:" + buyNum);
                System.out.println("花费:" + buyNum * goods.getPrice());
                System.out.println("收货人电话:" + customer.getPhone());
                Menu(customer);
                break;
            case 4:
                System.out.println("请输入充值金额");
                double money = sc.nextDouble();
                customer.setBalance(customer.getBalance() + money);
                customerDao.userMod(customer);
                Menu(customer);
                System.out.println("充值成功!");
                break;
            case 5:
                System.out.println("请输入修改的密码");
                String pwd = sc.next();
                customer.setPassword(pwd);
                customerDao.userMod(customer);
                System.out.println("修改成功!");
                Menu(customer);
            case 6:
                System.out.println("谢谢使用!");
                System.exit(0);
        }
    }
}

Main

package com.hqyj;

import com.hqyj.Service.AdminService;
import com.hqyj.Service.CustomerService;
import com.hqyj.dao.AdminDao;
import com.hqyj.entity.Admin;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        AdminService adminService = new AdminService();
        CustomerService customerService = new CustomerService();

        AdminDao adminDao = new AdminDao();
        Scanner sc = new Scanner(System.in);
        System.out.println("1:管理员界面");
        System.out.println("2.用户界面");
        while (true) {
            switch (sc.nextInt()) {
                case 1:
                    System.out.println("请输入管理员账号:");
                    int q = sc.nextInt();
                    System.out.println("请输入管理员密码:");
                    String p = sc.next();
                    Admin admin = adminDao.login(q, p);
                    if (admin != null) {
                        adminService.menu();
                    } else {
                        System.out.println("登录失败");
                    }
                    break;
                case 2:
                    customerService.useMod();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值