实际开发过程中,当存储量较大时,表的数量较多时,表的关系就会比较复杂。
此时就要有限考虑数据库的设计,并通 数据库三大范式 检验设计的合理性
1、失败的数据库设计造成的后果
2、优秀的数据库设计带来的好处
数据库需求分析阶段,了解系统需要实现的业务功能。
此时需要进一步的进入概要设计阶段,为了更加形象直观的体现数据库设计意图
数据库设计也有类似的图形化展示工具 E-R图 实体-练习图
通过一些具有特定含义的图形符号,提供图形化展示实体、属性和联系的方法
1、实体
2、属性
3、联系
示例
4、实体间的映射关系
绘制 E-R 图时,需要表现出实体之间的映射关系
即一个实体关联其他实体的个数
映射关系也成为了映射基数
三种映射关系
在绘制 E-R 图时,通过在连接线上分别添加数字表示映射关系 (一用1表示,多用 N 表示)
绘制完 E-R 图后,应当对数据局库进行详细的设计,数据库的详细设计,采用数据库模型图来实现
将 E-R 图转为数据库党总的多张表,并标识各表的主外键关系
通过图形化的方式,显示数据库存储的信息,信息的数据类型,以及各表之间的关系
Navicat 在设计好数据库后,可以一键生成数据库模型图
在设计数据库的过程中,如果开发者缺乏在设计阶段对数据库评价的手段,导致数据库设计的不合理
则会导致系统性能低下甚至不稳定
数据库设计问题大部分是数据冗余引起的
下方是一张存在大量数据冗余的数据表
这种数据冗余的数据表,对于查询来说很方便,只需要一次简单的查询即可,但是当对于数据库的更新操作 (增、删、改) 时,就会导致数据的更新异常
官方 https://downloads.mysql.com/archives/community/
度盘 https://pan.baidu.com/s/1fuGdLIDXx2-2b5mEdqEh3g?pwd=xzsr
注意选择版本号为5.7.40
下载完后我们会得到一个以 MySQL 开头命名的压缩包,我们需要把这个压缩包解压到本地
这里可以把 MySQL 文件名后面的-winx64
删除掉(度盘下载的压缩包我已经删除过了)
推荐将文件解压到 C 盘的 Program Files 文件下,即解压路径为C:\Program Files\mysql-5.7.40
在C:\Program Files\mysql-5.7.40
文件夹内新建一个文本文档,【强制】命名为:my.ini
注意开启显示文件拓展名
设置
如果在度盘下载的压缩包,我在压缩包中已经写入过
my.ini
文件,只需要注意自己的安装路径是否和配置文件内路径一致即可
使用记事本方法打开my.ini
文件,将下面的配置文件粘贴进去
[mysqld]
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=C:\Program Files\mysql-5.7.40
# 设置mysql数据库的数据的存放目录
datadir=C:\Program Files\mysql-5.7.40\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#跳过密码
skip-grant-tables
basedir:就是mysql的安装路径
datadir:只需要把
\data
前面的路径替换即可文本里的代码前面是没有数字的
C:\Program Files\mysql-5.7.40\bin
Win + R
然后通过管理员身份打开cmd
mysql --version
查看环境是否配置成功Win + R
打开运行输入cmd
以管理员身份运行cd /d C:\Program Files\mysql-5.7.40\bin
mysqld -install
命令安装 MySQL 服务mysqld --initialize-insecure
初始化 data 目录net start mysql
启动 MySQL 服务mysql -uroot
登录 MySQLupdate mysql.user set authentication_string=password('root') where user='root' and Host = 'localhost';
root
就是登录密码,账户名也是root
或者你可以把密码改成自己喜欢的flush privileges;
刷新一下权限exit
退出 MySQL 即可退出后回到my.ini
配置文件中,用#
注释掉或者直接删除掉最后一行代码skip-grant-tables
然后保存退出
到这里就已经成功下载并配置好了 MySQL
命令行先输入net stop mysql
关闭 MySQL 服务
在输入net start mysql
开启 MySQL 服务
这里报错的请自己到服务中手动打开 MySQL 服务后重新尝试
登录数据库
mysql -uroot -proot
mysql -u账户名 -p密码
注意密码前后不能有空格
最后可以输入一个查询版本信息的SQl语句玩一下:
select version(),user();
代码后面有分号
官方 https://downloads.mysql.com/archives/installer/
度盘 https://pan.baidu.com/s/1fuGdLIDXx2-2b5mEdqEh3g?pwd=xzsr
注意选择版本号为5.7.40
下载完后我们会得到一个以 MySQL 开头命名的msi
安装器,直接双击运行安装
NO
即可Custom
自定义安装MySQL Server 5.7.40 - X64
点击箭头添加到右边Execute
执行安装即可MySQL
登录密码(默认设置为 root
)MySQL57
就是服务器名称,点击Next
下一步C:\Program Files\MySQL\MySQL Server 5.7\bin
Win + R
然后通过管理员身份打开cmd
mysql --version
查看环境是否配置成功最后可以输入一个查询版本信息的SQl语句玩一下:
select version(),user();
代码后面有分号
压缩包安装方式安装的MySQL,卸载就直接删除掉解压后的MySQL文件夹即可,然后删除配置的 MySQL 环境变量
到这里只是卸载了MySQL软件,MySQL还有许多文件并没有删掉,如果不删除干净,会影响后续安装导致安装失败。
C:\Program Files (x86)\MySQL
内所有文件C:\ProgramData\MySQL
内所有文件,该文件是默认隐藏的,设置显示后即可见C:\Documents and Settings\All Users\Application Data\MySQL
内所有文件到这里也只是删掉了MySQL所有残余文件,还需要删除MySQL的注册表信息
Win + R
打开cmd
窗口输入regedit
打开注册表HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001/Services/Eventlog/Application/MySQL
HKEY_LOCAL_MACHINE/SYSTEM/ControlSet002/Services/Eventlog/Application/MySQL
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog/Application/MySQL
Navicate官网 https://www.navicat.com/en/products
SqlYog官网 https://sqlyog.en.softonic.com/
度盘 https://pan.baidu.com/s/1fuGdLIDXx2-2b5mEdqEh3g?pwd=xzsr
Navicat Premium 是一款功能强大的、可支持多连接的数据库管理工具
Navicate / SqlYog 涉及版权无法分享,加封面联系方式获取软件
通过操作向导创建数据库
utf8
点击 SQL 预览可以查看建库语句通过SQL语句创建数据库
SqlYog 与 Navicate 操作同理
MySQL 是一款流行的开源数据库,也是一个关系型数据库管理系统
在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一
由瑞典 MySQL AB 公司开发
2008年, MySQL AB公司被SUN公司收购
2009年,SUN公司被Oracle公司收购
运行速度快
使用成本低
容易使用
可移植性强
适用用户广
官网 https://downloads.mysql.com/archives/community/
安装教程 https://blog.csdn.net/qq_62283694/article/details/128853617
推荐使用 MySQL5.7
首先检查是否启动服务
电脑按下Win + R 输入 cmd
打开 DOS 窗口
net start mysql
net stop mysql
mysql –h服务器主机地址 –u用户名 –p密码
–h服务器主机地址
本机登录可以省略
Navicat Premium是一款功能强大的、可支持多连接的数据库管理工具
允许在单一程序中同时连接多达7种数据库
MySQL
MariaDB
MongoDB
SQL Server
SQLite
Oracle
PostgreSQL
官网 https://www.navicat.com/en/products
推荐使用:Navicat Premium 16
SQL语言 | 作用 |
---|---|
DDL(数据定义语言) | 用于创建或删除数据库对象 |
DML(数据操作语言) | 用于插入,修改和删除表中的数据 |
DQL(数据查询语言) | 用于数据查询,指 SELECT 语句 |
DCL(数据控制语言) | 用于控制数据库组件的存取许可、存取权限等 |
执行 MySql 的任何操作,都需要保证 MySql的服务处于启动状态
MySql 服务启动步骤
1、使用 Win 键 + R键调出"运行"对话框
2、输入 services .msc 打开 “服务” 窗口
3、找到 MySql 服务,右键,点击启动
1、在命令行窗口中,可使用以下语法格式连接数据库
mysql -h 服务器主机地址 -u 用户名 -p 密码
2、如果连接本机的 mysql,可以不输入服务器主机地址,如下
mysql -u 用户名 -p
3、然后再次输入密码,即可连接
4、使用命令 \p 即可断开连接 mysql
5、除了使用 命令行窗口链接 MySql,也可以数据库管理工具来连接
登陆数据库后,可以通过输入命令操作 MySql
SQL 语句不区分大小写
SELECT VERSION(),USER();
运行效果
CREATE DATABASE 数据库名
SHOW DATABASES;
在操作数据之前,需要先选定表所在的数据库,选择对某个数据库的表进行操作。
USE 数据库名
DROP DATABASE 数据库名;
在创建表之前,需要确定数据表的字段及每个字段的数据类型
MySQL 当中常用的的有数值类型、字符串类型和日期类型
1、数值类型
数据类型 | 字节数 | 取值范围 |
---|---|---|
TINYINT[(M)] | 1 字节 | 有符号值:-27~-27 无符号值:0~28-1 |
SMALLINT[(M)] | 2 字节 | 有符号值:-215~215-1 无符号值:0~216-1 |
MEDIUMINT[(M)] | 3 字节 | 有符号值:-223~223-1 无符号值:0~224-1 |
INT[(M)] | 4 字节 | 有符号值:-231~231-1 无符号值:0~2432-1 |
FLOAT[(M,D)] | 4 字节 | 有符号值:-3.402823466E+38~-1.175494351E-38 无符号值:1.175494351E-3.402823466E+38 |
DOUBLE[(M,D)] | 8 字节 | 有符号值:-1.7976931348623157E+308~-2.2250738585072014E-308 无符号值:2.225073858072014E-308~1.7976931348623157E+308 |
DECIMAL[(M[,D])] | M+2字节 | M:最大精度位数即总位数,M 的取值范围是1~16,默认值为 10 D:最小位精度位数,D 的取值范围为 0~30 该类型可能的取值范围与 DOUBLE 相同,但有效取值范围有 M、D决定 例如,类型为 DECIMAL(5,2) ,能够存储具有五位数字和两位小数的任何值的字段 取值范围是 -999.99~999.99 |
M 代表需要显示的数字的位数
若数据位数大于数据本身的显示宽度,只要不超过该类型取值范围,则以是实际位数显示
TINYINT、SMALLINT、MEDIUMINT、INT 都为整数类型
FLOAT、DOUBLE 为浮点数类型
DECIMAL 为定数类型
当对精度要求较高时,一般使用 DECIMAL 类型
表中的值都包含有符号值和无符号值,其中,有符号数可以为负数
2、字符串类型
数据类型 | 字节数 | 说明 |
---|---|---|
CHAR[(M)] | M 字节 | 固定长度字符串 M 为 0~255 的整数 |
VARCHAR[(M)] | 可变长度 | 可变长度字符串 M 为 0~65535 的整数 |
TINYTEXT | 0~255 | 微型文本 |
TEXT | 0~65535 | 文本 |
3、日期类型
数据类型 | 格式 | 取值范围 |
---|---|---|
DATE | YYYY-MM-DD | 1000-01-04~9999-12-31 |
DATETIME | YYYY-MM-DD hh:mm:ss | 1000-01-01 00:00:00~9999-12-31 23:59:59 |
TIME | hh:mm:ss | -838:59:59~838:59:59 |
TIMESTAMP | YYYYMMDDHHMMSS | 1970 年某时刻~2038 年某时刻,精度为 1 秒 |
YEAR | YYYY 格式的年份 | 1901~2155 |
在选择数据库之后,可以使用 SQL 语句创建数据表
1、创建数据表 语法
CREATE TABLE [IF NOT EXISTS] 表名(
字段1 数据类型 [字段属性 | 约束] [索引] [字段备注],
字段2 数据类型 [字段属性 | 约束] [索引] [字段备注],
......
字段n 数据类型 [字段属性 | 约束] [索引] [字段备注]
)[表类型] [表字符集] [表备注];
2、字段的约束和属性
字段约束和属性 | 关键字 | 说明 |
---|---|---|
主键约束 | PRIMARY KEY(PK) | 设置该字段为表的主键,同时保证字段的唯一性和非空 |
外键约束 | FOREIGN KEY(FK) | 用于在两表之间建立关系,需要指定引用主表的哪一字段,作为外键的字段要求在主表中为主键 |
自动增长 | AUTO_INCREMENT | (1)使用该列为自增字段,默认每条自增1 (2)通常用于设置主键,且为整数类型 (3)可设置1初始值和步长 |
非空约束 | NOT NULL | 保证字段不能为空 |
默认约束 | DEFAULT | 保证字段总会有值,即没有插入值,也会有默认值 |
唯一约束 | UNIQUE KEY(UK) | 设置字段的值是唯一的。不可重复 |
3、查看数据表结构 语法
describe 数据表名
4、 删除表
删除当前数据库中的数据表
删除表 语法
DROP TABLE [IF EXISTS] 表名;
ALTER TABLE 旧表名 RENAME [TO] 新表名;
ALTER TABLE 表名 CHANGE 原字段名 新字段名 数据类型 [属性];
ALTER TABLE 表名 ADD 字段名 数据类型 [属性];
ALTER TABLE 表名 DROP 字段名
ALTER TABLE 表名 ADD CONSTRAINT 主键名 PRIMARY KEY 表名(主键字段)
ALTER TABLE 表名 ADD CONSTRAINT 外键名 FOREIGN KEY (外键字段) REFERENCES 关联表名(关联字段)
功能 | InnoDB | MyISAM |
---|---|---|
事务处理 | 支持 | 不支持 |
外键约束 | 支持 | 不支持 |
表空间大小 | 较大 | 较小 |
数据行锁定 | 支持 | 不支持 |
DML 语句:执行 增、删、改、语句
使用 INSERT 语句向数据表中插入数据
INSERT INTO 表名 [(字段名列表)] VALUES (值列表);
INSERT INTO 表名 [(字段名列表)] VALUES(值列表1),(值列表2),...,(值列表n);
INSERT INTO 表名(字段1,字段2,...) SELECT 字段1,字段2,... FROM 原表;
SELECT 字段1,字段2,... INTO 新表 FROM 原表;
CREATE TABLE 新表(SELECT 字段1,字段2,... FROM 原表)
使用 UPDATE 语句更新数据表中的数据
UPDATE 表名 SET 字段1=值1,字段2=值2,...,字段n=值n [WHERE 条件]
从数据表中删除数据可以使用 DELETE 语句 或 TRUNCATE 语句
DELETE FROM 表名 [WHERE 条件]
TRUNCATE TABLE 表名
SELECT * FROM 表
SELECT 列名1,列名2 FROM 表
SELECT 列名1 AS '别名' FROM 表
在基础查询的基础上,对查询添加一些属性,让查询数据更为精准
回顾数据查询语句 (SELECT 语句) 语法如下
SELECT *或<字段名列表> FROM <表名或视图> [WHERE <查询条件>] [GROUP BY <分组的字段名>] [HAVING <条件>] [ORDER BY <排序的字段名>] [ASC 或 DESC] [LIMIT [位置偏移量,]行数];
MySQL 的运算符主要有:算术运算符、比较运算符、逻辑运算符、位运算符
运算符 | 作用 |
---|---|
+ | 加法 |
- | 减法 |
* | 乘法 |
/ 或 DIV | 除法 |
% 或 MOD | 取余 |
在除法运算和模(取余)运算中,如果除数为0,将是非法除数,返回结果为NULL
符号 | 描述 | 备注 |
---|---|---|
= | 等于 | |
<>, != | 不等于 | |
> | 大于 | |
< | 小于 | |
<= | 小于等于 | |
>= | 大于等于 | |
BETWEEN | 在两值之间 | >=min&&<=max |
NOT BETWEEN | 不在两值之间 | |
IN | 在集合中 | |
NOT IN | 不在集合中 | |
<=> | 严格比较两个NULL值是否相等 | 两个操作码均为NULL时,其所得值为1;而当一个操作码为NULL时,其所得值为0 |
LIKE | 模糊匹配 | |
REGEXP 或 RLIKE | 正则式匹配 | |
IS NULL | 为空 | |
IS NOT NULL | 不为空 |
运算符号 | 作用 | 示例 |
---|---|---|
NOT 或 ! | 逻辑非 | |
AND | 逻辑与 | |
OR | 逻辑或 | 当A、B中只要有一个或者两个都为1时,结果为1,否则为0 |
XOR | 逻辑异或 | 当A、B两个不同时结果为1,否则为0 |
运算符号 | 作用 | 说明 |
---|---|---|
& | 按位与 | 参与& 运算的两个二进制位都为 1 时,结果就为 1,否则为 0 |
| | 按位或 | 参与` |
^ | 按位异或 | 参与^ 运算的两个二进制位不同时,结果为 1,相同时,结果为 0 |
~ | 取反 | 按位取反(~)运算符将给定的值的二进制数逐位进行取反操作,即将1变为0,将0变为1 |
<< | 左移 | 位左移是按指定值的补码形式进行左移,左移指定位数之后,左边高位的数值被移出并丢弃,右边低位空出的位置用 0 补齐 |
>> | 右移 | 位右移是按指定值的补码形式进行右移,右移指定位数之后,右边低位的数值被移出并丢弃,左边高位空出的位置用 0 补齐 |
优先级顺序 | 运算符 |
---|---|
1 | := |
2 | ||,OR,XOR |
3 | &&,AND |
4 | NOT |
5 | BETWEEN,CASE,WHEN,THEN,ELSE |
6 | =,<=>,>,>=,<,<=,<>,!=,IS,LIKE,REGEXP,IN |
7 | | |
8 | & |
9 | <<,>> |
10 | -,+ |
11 | *,/,DIV,%,MOD |
12 | ^ |
13 | -(一元减号),~(一元比特反转) |
14 | ! |
使用 WHERE 关键字来对查询的结果进行筛选
WHERE 列名 FROM 表名 WHERE 列名 运算符 值
WHERE 列名 FROM 表名 WHERE 列名 运算符 值 AND 列名 运算符 值
WHERE 列名 FROM 表名 WHERE 列名 运算符 值 OR 列名 运算符 值
WHERE 列名 NOT BETWEEN 起始值 AND 最终值
WHERE 列名 IS NOT NULL
模糊查询一般使用关键字 LIKE 主要用于匹配列中的数据
通配符 | 描述 |
---|---|
% | 替代0或多个字符 |
_ | 替代一个字符,字符长度不能为0 |
[…] | 字符列中任何单一字符 |
[^…] | 不在字符列中任何单一字符 |
where NOT LIKE 字符串
NOT:可选项,表示取反
字符串:用来匹配的字符串,可以是完整的字符串或者通配符
语法格式如下
WHERE 列1 LIKE '%123' #表示 列1的值结尾为123即可匹配
WHERE 列1 LIKE '123%' #表示 列1的值开头为123即可匹配
WHERE 列1 LIKE '%123%' #表示 列1的值包含为123即可匹配
WHERE 列1 LIKE '_123' #表示列1的值为 X123,X 表示任意字符
WHERE 列1 LIKE '[张李王]三' #将找出“张三”、“李三”、“王三”(而不是“张李王三”)
WHERE 列1 LIKE '[^张李王]三' #将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等;
- 匹配的字符串必须加单引号或者双引号。
- 默认情况下,LIKE关键字匹配字符串时候不区分大小写,可以在LIKE关键字后添加BINARY关键字来区分大小写。
- 如果查询内容中有通配符字符,就需要加转义字符 “\”.
基于已有数据进行数据的统计分析计算等需求
函数名 | 函数说明 | 函数示例 |
---|---|---|
CONCAT(str1,str2,…,strn) | 将字符串 str1、str2、…、strn连接为一个完整的字符串 | SELECT CONCAT(‘L’,‘Q’,‘E’); 返回:LQE |
LOWER(str) | 将字符穿 str 中所有字符变为小写 | SELECT LOWER(‘LqE’) 返回:lqe |
UPPER(str) | 将字符串 str 中所有的字符变为大写 | SELECT UPPER(‘lQe’) 返回:LQE |
SUBSTRING(str,num,len) | 返回字符串 str 的第 num个位置开始,长度为 len 的子字符串 | SELECT SUBSTRING(‘ABCDEFG’,3,4) 返回:CDEF |
INSERT(str,pos,len,newstr) | 将字符串 str 从 pos 位置开始,将len 个字符长度的子串替换为字符串 newstr | SELECT INSERT(‘1234’,3,0,‘ABCDE’) 返回:12ABCDE34 |
函数名 | 函数说明 | 示例 |
---|---|---|
CURDATE() | 获取当前日期 | SELECT CURDATE(); 返回:2023-03-11 |
CURTIME() | 获取当前时间 | SELECT CURTIME(); 返回:01:54:30 |
NOW() | 获取当前日期和时间 | SELECT NOW(); 返回:2023-03-11 01:55:12 |
WEEK(date) | 返回参数 date 为一年中的第几周 | SELECT WEEK(NOW()); 返回: 10 |
YEAR(date) | 返回参数 date 的年份 | SELECT YEAR(NOW()); 返回:2023 |
HOUR(time) | 返回参数 time 的小时值 | SELECT HOUR(NOW()); 返回:1 |
MINUTE(time) | 返回参数 time 的分钟值 | SELECT HOUR(NOW()); 返回:57 |
DATEDIFF(date1,date2) | 返回参数 date1 和参数 date2 之间相隔的天数 | SELECT DATEDIFF(NOW(),‘2019-8-8’) 返回:1311 |
ADDDATE(date,n) | 计算参数 date 在 n 天后的日期 | SELECT ADDDATE(NOW(),4); 返回:2023-3-15 01:58:36 |
函数名 | 函数说明 |
---|---|
COUNT() | 返回某字段的行数 |
MAX() | 返回某字段的最大值 |
MIN() | 返回某字段的最小值 |
SUM() | 返回某字段的和 |
AVG | 返回某字段的平均值 |
函数名 | 函数说明 | 示例 |
---|---|---|
CELL(x) | 返回大于或等于数值 x 的最小整数,向上取整 | SELECT CELL(2.1) 返回:3 |
FLOOR(x) | 返回小于或等于数值 x 的最大整数,向下取整 | SELECT FLOOR(2.9) 返回:2 |
RAND() | 返回 0~1 的随机数 | SELECT RANG(); 返回:0.15013303621684485 |
SELECT COUNT(*) FROM 表名 GROUP BY 分组的字段
HAVING 语句对分组查询的结果进行条件限定,用来限制分组后的显示,符合条件表达式的结果将被显示
HAVING 语法格式 如下
SELECT COUNT(*) FROM 表名 GROUP BY 分组的字段 HAVING 条件表达式
ASC
降序DESC
SELECT * FROM 表名 ORDER BY 进行排序的列 ASC|DESC
ORDER BY 后面可以跟多个字段进行排序
多字段排序 语法格式如下
SELECT * FROM 表名 order by A1 asc , A2 desc
当查询的数据有很多条时,通过真分页的方式,每次取出所需数据。对于不同的数据,实现分页有不同的方式
分页查询使用关键字 LIMIT
LIMIT出现在查询语句的最后,可以使用一个参数或两个参数来限制取出的数据。其中第一个参数代表偏移量:offset(可选参数),第二个参数代表取出的数据条数:rows
LIMIT 单参数用法
/* 查询前5条数据 */
SELECT * FROM 表名 LIMIT 5;
LIMIT 双参数用法
/* 查询第1-10条数据 */
SELECT * FROM 表名 LIMIT 0,10;
/* 查询第11-20条数据 */
SELECT * FROM 表名 LIMIT 10 OFFSET 10;
/* 查询第21-30条数据 */
SELECT * FROM 表名 LIMIT 20 OFFSET 10;
/* 查询第N页的十条数据 */
SELECT * FROM 表名 LIMIT (N-1)*10,10;
SELECT * FROM 表1 WHERE 列1=(Where 列2 FROM 表2)
SELECT patientName FROM patient WHERE checkItemID=(SELECT checkItemID FROM prescription WHERE checkItemName='尿常规');
如果子查询的结果为多个值,就会导致代码报错
解决方案就是使用 IN 关键字,将 = 替换成 IN,如下
SELECT patientName FROM patient WHERE checkItemID IN (SELECT checkItemID FROM prescription WHERE checkItemName='尿常规');
EXISTS 是一个布尔运算符返回true或false。 EXISTS 经常使用的在一个子查询,以测试一个“存在”状态
EXISTS 在 WHERE 子句中作为子查询时使用,若子查询中有数据,则 EXISTS 子查询为 true,其外层查询语句会被执行,反之,不会执行
EXISTS 语法
SELECT .... FROM 表名 WHERE EXISTS(子查询)
在查询数据时,需要查询数据关联的另一个表的那一列,需要从多个表中选择数据项的情况,就是多表查询
Select 表1.值2,表2.值2 From 表1 INNER JOIN 表2 ON 表1.值1=表2.值1
Select 表1.值2,表2.值2 From 表1 LEFT JOIN 表2 ON 表1.值1=表2.值1
Select 表1.值2,表2.值2 From 表1 RIGHT JOIN 表2 ON 表1.值1=表2.值1
优先级顺序 | 关键字 |
---|---|
1 | LIMIT |
2 | ORDER BY |
3 | HAVING |
4 | GROUP BY |
5 | WHERE |
6 | INNER JOIN |
7 | ON |
存储过程 是一组为了完成特定功能的 SQL 语句集合
经编译后保存在数据库中
通过指定存储过程的名字并给出参数的值
可带参数,也可返回结果
可包含数据操纵语句、变量、逻辑控制语句等
存储过程的优点
存储过程的缺点
创建存储过程
CREATE PROCEDURE 过程名([过程参数[,...]])
[特性] #可选项,用于设置存储过程的行为
存储过程体
特性 | 说明 |
---|---|
LANGUAGE SQL | 存储过程语音,默认为 SQL |
CONTAINS SQL NO SQL READS SQL DATA MODIFIES SQL DATA |
存储过程要做哪类工作 默认为CONTAINS SQL |
SQL SECURITY{DEFINER | INVOKER} | 用来指定存储过程的执行权限 默认值为 DEFINER DEFINER 使用创建者的权限执行 INVOKER 使用执行者的权限执行 |
COMMENT ‘string’ | 存储过程的注释信息 |
示例 编写存储过程,输出病人总人数
DROP PROCEDURE IF EXISTS proc_patient_countPatient;
DELIMITER // #声明分隔符
CREATE PROCEDURE proc_patient_countPatient()
BEGIN #过程体开始
SELECT COUNT(*) FROM patient;
END // #过程体结束
DELIMITER ; #恢复默认分隔符
1、删除存储过程
DROP PROCEDURE 过程名
2、声明语句分隔符
DELIMITER $$
DELIMITER //
DELIMITER ;
3、定义存储过程的参数
[IN|OUT|INOUT] 参数名 类型
DELIMITER // #声明分隔符
CREATE PROCEDURE proc_patient_countPatient2(IN patientNum INT)
#省略......
DELIMITER ; #恢复默认分隔符
DELIMITER // #声明分隔符
CREATE PROCEDURE proc_patient_countPatient3(OUT patientNum INT)
SELECT COUNT(*) INTO patientNum FROM patient;
DELIMITER ; #恢复默认分隔符
#调用
ALL proc_patient_countPatient3(@patientNum);
4、过程体的标识
BEGIN ...... END 分隔符
调用存储过程
CALL 存储过程名 (参数列表);
创建和调用存储过程 示例
#如果存储过程存在,则删除
DROP PROCEDURE IF EXISTS proc_patient_countPatient;
DELIMITER // #声明分隔符
CREATE PROCEDURE proc_patient_countPatient()
BEGIN #过程体开始
SELECT COUNT(*) FROM patient;
END // #过程体结束
DELIMITER ; #恢复默认分隔符
#调用
CALL roc_patient_countPatient();
类似 Java 与其他编程语言,定义存储过程时可以使用变量
声明变量 语法
DECLARE 变量名[,变量名...] 数据类型 [DEFAULT 值];
DECLARE trade_time DATE DEFAULT '2020-07-10';
所有局部变量的声明一定要放在存储过程体的开始,必须在BEGIN后,否则会提示语法错误
声明变量后,可以给变量赋值
变量赋值 语法
SET 变量名=表达式值[,变量名=表达式...];
set total=100;
DELIMITER //
CREATE [DEFINER={user|CURRENT_USER}]
PROCEDURE 存储过程名
[SQL SECURITY {DEFINER|INVOKER|....}]
BEGIN
....
END //
DELIMITER ;
对创建后的存储过程进行查看
1、查看存储过程的状态
SHOW PROCEDURE STATUS
SHOW PROCEDURE STATUS WHERE DB='数据库名'
SHOW PROCEDURE STATUS LIKE '%patient%';
2、查看存储过程的创建代码
SHOW CREATE PROCEDURE 存储过程名
ALTER PROCEDURE 存储过程名 [特性......];
DROP PROCEDURE 存储过程名
MySQL 提供的控制语句包括条件语句、循环语句、迭代语句
MySQL 提供了两种控制语句,分别是 IF-ELSEIF-ELSE 条件语句和 CASE 条件语句
1、IF-ELSEIF-ELSE 条件语句
IF 条件 THEN 语句列表
[ELSEIF 条件 THEN 语句列表]
[ELSE 语句列表]
END IF;
2、CASE 条件语句
CASE
WHEN 条件 THEN 语句列表
[WHEN 条件 THEN 语句列表]
[ELSE 语句列表]
END CASE;
CASE 列名
WHEN 条件 THEN 语句列表
[WHEN 条件 THEN 语句列表]
[ELSE 语句列表]
END CASE;
MySQL 语句提供多种循环语句,包括 WHILE 循环语句、LOOP 循环语句 和 REPEAT 循环语句
1、WHILE 循环语句
[lable:] WHILE 条件 DO
语句列表
END WHILE [lable]
2、LOOP 循环
[lable:] LOOP
语句列表
END LOOP [lable];
LEAVE lable;
3、REPEAT 循环语句
[lable:] REPEAT
语句列表
UNTLL 条件
END REPEAT [label]
ITERATE lable;
事务是最哦为单个逻辑工作单元执行的一系列操作,必须遵循四个特性
原子性、一致性、隔离性、持久性
默认设置下,每个 SQL 语句就是一个事务,即执行 SQL 语句后自动提交
实现事务主要有两种方式
两种实现事务的语法格式如下
1、执行事务的 SQL 语句语法格式
#开启事务
BEGIN ; 或 START TRANSACTION;
#一组作为事务运行的 SQL 语句
#提交事务
COMMIT;
BEGIN 或 START TRANSACTION 标志着事务的起止点,其后的 SQL 语句不会被提交生效
当运行到 COMMIT 语句后,标志着一个事务被提交成功
自事务开始至提交语句之间执行的所有数据更新将被永久的保存在数据库文件中,并释放连接时占用的资源
回滚(撤销)事务 语法
#开启事务
BEGIN;或 START TRANSACTION;
#一组作为事务运行的 SQL 语句
#回滚事务
ROLLBACK;
2、设置自动提交关闭或开启
SET autocommit=0|1
1、创建索引
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX 索引名称 ON 表名(列名)
2、查看索引
SHOW INDEX FORM 表名
3、删除索引
DROP INDEX 索引名 ON 表名
1、视图是一张虚拟表
2、视图中不存放数据
3、一个原始表,根据不同用户的不同需求,可以创建不同的视图
1、给开发人员带来好处
2、给数据库的最终用户带来的好处
1、创建视图
CREATE VIEW 视图名 AS
2、查询视图中的数据
SELECT * FROM 视图名
3、删除视图
DROP VIEW [IF EXISTS] 视图名;
4、使用视图的注意事项
1、使用 mysqldump 备份数据库
mysqldump [options] database [table1.table2,...] > [path]/filename.sql
2、使用 Navicat 备份数据库
1、使用 MySQL 命令导入数据
mysql -u username -p dbname < filename.sql
2、使用 source 命令恢复数据、
source filename;
3、使用 Navicat 的数据导入功能导入数据
JDBC (Java Database Connectivity,Java数据库连接)
1、JDBC API
2、JDBC Driver Manager
3、JDBC 驱动
类/接口 | 作用 |
---|---|
DriverManager 类 | 装载驱动程序,并为创建新的数据库连接提供支持 |
Connection 接口 | 负责连接数据库并担任传送数据的任务 |
Statement 接口 | 由 Connection 产生,负责执行 SQL 语句 |
PreparedStatement 接口 | Statement 的子接口,也由 Connection 产生,负责执行 Sql语句 与 Statement 接口的区别:PreparedStatement 借口具有高安全性、 高性能、高可读性和高可维护性等优点 |
ResultSet 接口 | 负责保存和处理 Statement 执行后产生的查询结果 |
1、加载 JDBC 驱动
try{
Class.forName("JDBC驱动类的名称")
}catch(ClassNotFoundException){
//异常输出代码
}
2、与数据库建立连接
Connection conn=DriverManager.getConnection(数据库连接地址字符串,数据库用户名,密码)
jdbc:数据库://ip:端口/数据库名称 [?连接参数=参数值]
方法名称 | 作用 |
---|---|
Statement createStatement() | 创建一个 Statement 对象并将 SQL 语句发送到数据库 |
PreparedStatement preparedStatement(String sql) | 创建一个 PreparedStatement 对象并将参数化的 SQL 语句发送到数据库 |
boolean isClosed() | 查询此 Connection 对象是否已经被关闭。若已关闭,返回true |
void close() | 立即释放此 Connection 对象的数据库和 JDBC 资源 |
获取 Connection 对象后,需要使用 Connection 对象的 createStatement() 方法创建 Statement 对象,在通过 Statement 对象 将 SQL 语句发送到 MySQL 服务器中执行操作
在 Statement 接口中执行 SQL 命令的 3 种常用方法
方法名称 | 作用 |
---|---|
ResultSet executeQuery(String sql) | 可以执行 SQL 查询并获取 ResultSet 对象 |
int executeUpdate(Sting sql) | 可以执行插入、删除、更新等操作,返回值时执行该操作所影响的行数 |
boolean execute(String sql) | 可以执行任意 SQL 语句。若结果为 Result 对象,则返回 true;若结果为 更新计数或不存在任何结果,则返回 false |
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
/**
* 准备工作:
* 1.从官网下载MySql驱动包
* 2.将驱动包引入到项目工程中
*/
public class LX_insert {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner input=new Scanner(System.in);
//设置连接数据库需要的参数
//获取驱动类
String driver="com.mysql.jdbc.Driver";
//数据库连接地址
String url="jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=UTF-8";
//数据库用户名
String user="root";
//数据库密码
String password="root";
//加载驱动
Class.forName(driver);
//建立数据库连接
Connection conn= DriverManager.getConnection(url,user,password);
//创建 Statement 对象
Statement statement=conn.createStatement();
//获取要添加的数据
System.out.print("请输入患者密码:");
String patientPassWord=input.next();
System.out.print("请输入患者性别:");
String patientGender=input.next();
System.out.print("请输入患者姓名:");
String patientName=input.next();
//拼接 SQL 语句
StringBuffer sql=new StringBuffer("INSERT INTO patient(password,gender,patientName) VALUES ('");
sql.append(patientPassWord);
sql.append("','");
sql.append(patientGender);
sql.append("','");
sql.append(patientName);
sql.append("');");
System.out.println(sql);
//发送 SQL 语句,并接收结果
int effectRowNum= statement.executeUpdate(sql.toString());
//判断是否添加成功
if(effectRowNum>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
//关闭资源
statement.close();
conn.close();
}
}
运行结果
请输入患者密码:111111
请输入患者性别:男
请输入患者姓名:无名氏
INSERT INTO patient(password,gender,patientName) VALUES ('111111','男','无名氏');
添加成功
使用 JDBC 查询数据库中的数据时,需要使用 ResultSet 对象来接收从数据库中返回的数据
ResultSet 常用方法
方法名 | 说明 |
---|---|
boolean next() | 将游标从当前位置向下移动一行 |
boolean previous() | 游标从当前位置向上移动一行 |
void close | 关闭 ResultSet 对象 |
int getInt(int colIndex) | 以 int 形式获取结果集当前行指定列号值 |
int getInt(String colLabel) | 以 int 形式获取结果集当前行指定列名值 |
float getFloat(int colIndex) | 以 float 形式获取结果集当前行指定列号值 |
float getFloat(String colLabel) | 以float形式获取结果集当前行指定列名值 |
String getString(int colIndex) | 以String形式获取结果集当前行指定列号值 |
String getString(String colLabel) | 以String形式获取结果集当前行指定列名值 |
import java.sql.*;
/**
* 准备工作:
* 1.从官网下载MySql驱动包
* 2.将驱动包引入到项目工程中
*/
public class LX_Select {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//设置连接数据库需要的参数
//获取驱动类
String driver="com.mysql.jdbc.Driver";
//数据库连接地址
String url="jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=UTF-8";
//数据库用户名
String user="root";
//数据库密码
String password="root";
//加载驱动
Class.forName(driver);
//建立数据库连接
Connection conn= DriverManager.getConnection(url, user, password);
//创建 Statement 对象
Statement statement=conn.createStatement();
//SQL 语句
String sql="SELECT * FROM patient";
//向 MySQL 发送 SQL 语句,并接收结果
ResultSet rs=statement.executeQuery(sql);
//输出结果
//判断结果是否还有下一个
while(rs.next()){
//如果有下一个 输出 patientID列、patientName列
System.out.println("病人编号:"+rs.getString("patientID")+"病人姓名:"+rs.getString("patientName"));
}
//关闭资源
rs.close();
statement.close();
conn.close();
}
}
运行结果
病人编号:1病人姓名:夏颖
病人编号:2病人姓名:李政
病人编号:3病人姓名:李沁
病人编号:4病人姓名:李思雨
病人编号:5病人姓名:夏天
病人编号:6病人姓名:刘占波
病人编号:7病人姓名:廖慧颖
病人编号:8病人姓名:李伟忠
病人编号:9病人姓名:姚维新
病人编号:10病人姓名:陈建
病人编号:11病人姓名:林永清
病人编号:12病人姓名:李亚
病人编号:13病人姓名:张菲
代码演示
import java.sql.*;
import java.util.Scanner;
import java.sql.*;
/**
* 准备工作:
* 1.从官网下载MySql驱动包
* 2.将驱动包引入到项目工程中
*/
public class LX_SQL {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner input=new Scanner(System.in);
//设置连接数据库需要的参数
//获取驱动类
String driver="com.mysql.jdbc.Driver";
//数据库连接地址
String url="jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=UTF-8";
//数据库用户名
String user="root";
//数据库密码
String password="root";
//接收用户输入的姓名和密码
System.out.print("请输入患者姓名:");
String patientName=input.next();
System.out.print("请输入患者密码:");
String patientPassword=input.next();
//加载驱动
Class.forName(driver);
//建立数据库连接
Connection conn= DriverManager.getConnection(url, user, password);
//创建 Statement 对象
Statement statement=conn.createStatement();
//SQL 语句
StringBuffer sql=new StringBuffer("SELECT * FROM patient where patientName='");
sql.append(patientName);
sql.append("'and password='");
sql.append(patientPassword);
sql.append("';");
//向 MySQL 发送 SQL 语句,并接收结果
ResultSet rs=statement.executeQuery(sql.toString());
//判断输入的是否正确
if(rs.next()){
System.out.println("登录成功");
System.out.println("病人姓名:"+rs.getString("patientName"));
}else{
System.out.println("登录失败");
}
//关闭资源
rs.close();
statement.close();
conn.close();
}
}
效果演示
请输入患者姓名:夏颖
请输入患者密码:123456
登录成功
病人姓名:夏颖
使用 SQL注入攻击 效果演示
请输入患者姓名:*-*
请输入患者密码:100'OR'1'='1
登录成功
病人姓名:夏颖
方法 | 作用 |
---|---|
boolean execute() | 执行 SQL 语句,该语句可以是任何 SQL 语句。若结果为 Result 对象, 则返回 true;若结果是更新计数或没有结果,则返回 false |
ResultSet executeQuery() | 执行 SQL 查询,并返回该查询生成的 ResultSet 对象 |
int executeUpdate() | 执行 SQL 语句,该语句必须是一个 DML 语句,如 INSERT、UPDATE 或 DELETE 语句;或者是无返回内容的 SQL 语句和 DDL 语句。返回值时执行该操作所影响的行数 |
void sexXxx(int index,xxx x) | 方法名 Xxx 和第二个参数的 xxx 均表示如 int、float、double 等基本数据类型, 并且两个类型必须一致,参数列表中的 x 表示方法的形式参数。把指定数据类型 (xxx) 的值 x 设置为 index 位置的参数。根据参数类型的不同,常见的方法有 setInt(int index,int x)、setFloat(int index,float x),setDouble(int index,double x) 等 |
void setObject(int index,Object x) | 除基本数据类型外,参数类型也可以使 Object,可以将 Object 对象 x 设置为 index 位置的参数 |
1、创建 PreparedStatement 对象
通过 Connection 接口的 prepareStatement (String sql) 方法创建 PreparedStatement 对象
在创建 PreparedStatement 对象时应设置号该对象要执行的 SQL 语句
SQL 语句可以具有一个或多个输入参数时,这些输入参数的值在 PreparedStatement 创建时违背指定,而
是为每个参数保留一个 ? 作为占位符
因为 SQL 语句的参数已经固定,所以不会出现 SQL 注入的漏洞
在 SQL 语句中使用 ? 作为占位符 演示
String sql="SELECT * FROM patient where patientName=? and password=?"
2、设置输入参数的值
String sql="SELECT * FROM patient where patientName=? and password=?";
PreparedStatement pps=conn.prepareStatement(sql);
pps.setString(1,"夏颖");
pss.setString(2,"123456")
3、使用 PreparedStatement 升级医院管理系统开发登录功能
import java.sql.*;
import java.util.Scanner;
import java.sql.*;
/**
* 准备工作:
* 1.从官网下载MySql驱动包
* 2.将驱动包引入到项目工程中
*/
public class LX_SQL {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner input=new Scanner(System.in);
//设置连接数据库需要的参数
//获取驱动类
String driver="com.mysql.jdbc.Driver";
//数据库连接地址
String url="jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=UTF-8";
//数据库用户名
String user="root";
//数据库密码
String password="root";
//接收用户输入的姓名和密码
System.out.print("请输入患者姓名:");
String patientName=input.next();
System.out.print("请输入患者密码:");
String patientPassword=input.next();
//加载驱动
Class.forName(driver);
//建立数据库连接
Connection conn= DriverManager.getConnection(url, user, password);
//SQL 语句
String sql="SELECT * FROM patient where patientName=? and password=?";
//创建 PreparedStatement 对象
PreparedStatement pps=conn.prepareStatement(sql);
//设置参数值
pps.setString(1,patientName);
pps.setString(2,patientPassword);
//输出 SQL 语句
System.out.println(pps.toString());
//接收查询数据
ResultSet rs=pps.executeQuery();
//判断输入的是否正确
if(rs.next()){
System.out.println("登录成功");
System.out.println("病人姓名:"+rs.getString("patientName"));
}else{
System.out.println("登录失败");
}
//关闭资源
rs.close();
pps.close();
conn.close();
}
}
运行效果展示
请输入患者姓名:夏颖
请输入患者密码:123456
com.mysql.jdbc.JDBC4PreparedStatement@20ad9418: SELECT * FROM patient where patientName='夏颖' and password='123456'
登录成功
病人姓名:夏颖
测试 SQL 注入攻击 运行效果展示
请输入患者姓名:夏颖
请输入患者密码:100'OR'1'='1
com.mysql.jdbc.JDBC4PreparedStatement@20ad9418: SELECT * FROM patient where patientName='夏颖' and password='100\'OR\'1\'=\'1'
登录失败
1. 添加 properties 文件
2. 编辑配置文件
在 database.properties 文件中添加配置信息
在连接数据库时,可能会修改的部分时数据库驱动 (driver)、数据库连接 URL (url)、
连接数据库的用户名 (user) 和密码 (password)
按照 Properties 文件格式填写配置文件,如下
dirver=com.mysql.jdbc.Driver;
url=jdbc:mysql://localhost:3306/hospital?useUnicode=ture&characterEncoding=UTF-8
user=root
password=root
3. 读取配置信息
方法 | 描述 |
---|---|
String getProperty(String key) | 用指定的键在此属性列表中搜索属性,通过参数 key 得到其所对应的值 |
Object serProperty(String key,String value) | 通过调用基类 Hashtable 的 put() 方法设置键-值对 |
void load(InputStream inStream) | 从输入流中读取属性列表 (键和元素对),通过对指定文件进行装载获取 该文件中所有的键-值对 |
void clear() | 清除所装载的键-值对,该方法由基类 Hashtable 提供 |
public class DBUtil {
public static String driver;
public static String url;
public static String user;
public static String password;
//静态方法,创建该类时自动调用
static{
init();
}
//配置文件
public static void init(){
String file="database.properties";
InputStream in=DBUtil.class.getClassLoader().getResourceAsStream(file);
Properties ties=new Properties();
try {
ties.load(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
driver= ties.getProperty("driver");
url=ties.getProperty("url");
user=ties.getProperty("user");
password=ties.getProperty("password");
}
Entity(实体类)是在 Java 项目中,与数据表相对应的类
实体类特征
private
修饰getter/setter
方法java.io.Serializable
接口,支持序列化机制,可以将该对象转换成字节序列而保存在磁盘上或在网络上传输操作数据库时,会将数据库中的实体及关系以实体类的方式组织起来
通常在 Java 工程中,创建一个名为 entity 的 Package 集合保存实体类
示例
以下表格为 patient 表中的字段,构造 patient 实体,实体类中的属性与 patient 表中的字段相对应
名 | 类型 | 长度 | 小数点 | 不是null | 虚拟 | 键 | 注释 |
---|---|---|---|---|---|---|---|
patientID | int | 4 | 0 | ☑ | □ | 主键 | 病人编号 |
password | varchar | 20 | 0 | ☑ | □ | 登录密码 | |
birthDate | date | 0 | 0 | □ | □ | 出生日期 | |
gender | varchar | 4 | 0 | ☑ | □ | 性别 | |
patirentName | varchar | 50 | 0 | ☑ | □ | 病人姓名 | |
phoneNum | varchar | 50 | 0 | □ | □ | 联系电话 | |
varchar | 70 | 0 | □ | □ | 邮箱 | ||
identityNum | varchar | 20 | 0 | □ | □ | 身份证号 | |
address | varchar | 255 | 0 | □ | □ | 地址 |
在 Java 项目中构造 patient 表的实体类
package cn.QiaoYun.entity;
/*
* 患者表
* */
public class Patient {
private int patientId;
private String password;
private String birthDate;
private String gender;
private String patientName;
private String phoneNum;
private String email;
private String identityNum;
private String address;
public int getPatientId() {
return patientId;
}
public void setPatientId(int patientId) {
this.patientId = patientId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPatientName() {
return patientName;
}
public void setPatientName(String patientName) {
this.patientName = patientName;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getIdentityNum() {
return identityNum;
}
public void setIdentityNum(String identityNum) {
this.identityNum = identityNum;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Patient() {
}
public Patient(int patientId, String password, String birthDate, String gender, String patientName, String phoneNum, String email, String identityNum, String address) {
this.patientId = patientId;
this.password = password;
this.birthDate = birthDate;
this.gender = gender;
this.patientName = patientName;
this.phoneNum = phoneNum;
this.email = email;
this.identityNum = identityNum;
this.address = address;
}
@Override
public String toString() {
return "Patient{" +
"patientId=" + patientId +
", password='" + password + '\'' +
", birthDate='" + birthDate + '\'' +
", gender='" + gender + '\'' +
", patientName='" + patientName + '\'' +
", phoneNum='" + phoneNum + '\'' +
", email='" + email + '\'' +
", identityNum='" + identityNum + '\'' +
", address='" + address + '\'' +
'}';
}
}
通过学习 JDBC 及 Dao 模式,现在通过项目联系,进行对 JDBC 及 Dao 模式的进一步训练
以下是我通过练习,写的一个 理发店项目,仅供参考
大概需求分析完毕后,需要根据自己所实现的业务需求,来设计数据库的 E-R图
以下是我个人绘制的 E-R图,仅供参考
根据自己所设计的 数据库模型图,进一步的设计自己项目所需的数据库
以下是我自己设计的数据库(仅结构),仅供参考
/*
Navicat Premium Data Transfer
Source Server : LanQiaoer
Source Server Type : MySQL
Source Server Version : 50740 (5.7.40)
Source Host : localhost:3306
Source Schema : block
Target Server Type : MySQL
Target Server Version : 50740 (5.7.40)
File Encoding : 65001
Date: 03/04/2023 15:50:26
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for blocktype
-- ----------------------------
DROP TABLE IF EXISTS `blocktype`;
CREATE TABLE `blocktype` (
`blockTypeID` int(11) NOT NULL AUTO_INCREMENT COMMENT '卡类型ID',
`blockTypeName` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '卡类型名称',
`blockdiscount` double NOT NULL COMMENT '卡折扣',
`maxRecharge` double NOT NULL COMMENT '最大充值金额',
`minRecharge` double NULL DEFAULT NULL COMMENT '最小充值金额',
PRIMARY KEY (`blockTypeID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Table structure for consumption
-- ----------------------------
DROP TABLE IF EXISTS `consumption`;
CREATE TABLE `consumption` (
`consumptionID` int(11) NOT NULL AUTO_INCREMENT COMMENT '消费记录ID',
`consumptionUserID` int(11) NOT NULL COMMENT '消费用户ID',
`consumptionProjectID` int(11) NOT NULL COMMENT '消费项目ID',
`staffID` int(11) NOT NULL COMMENT '消费时,使用哪位员工的ID',
`consumptionBalance` double NOT NULL COMMENT '金额流水',
`userBalance` double NOT NULL COMMENT '消费后用户余额余额',
`consumptionDatetime` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '消费时间',
PRIMARY KEY (`consumptionID`) USING BTREE,
INDEX `FK_consumption_user`(`consumptionUserID`) USING BTREE,
INDEX `FK_consumption_staff`(`staffID`) USING BTREE,
INDEX `FK`(`consumptionProjectID`) USING BTREE,
CONSTRAINT `FK` FOREIGN KEY (`consumptionProjectID`) REFERENCES `projecttype` (`projectTypeID`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FK_consumption_staff` FOREIGN KEY (`staffID`) REFERENCES `staff` (`staffID`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FK_consumption_user` FOREIGN KEY (`consumptionUserID`) REFERENCES `user` (`userID`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 47 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for manageruser
-- ----------------------------
DROP TABLE IF EXISTS `manageruser`;
CREATE TABLE `manageruser` (
`ManagerID` int(11) NOT NULL AUTO_INCREMENT,
`ManagerUserName` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`ManagerPassword` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`ManagerID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for projecttype
-- ----------------------------
DROP TABLE IF EXISTS `projecttype`;
CREATE TABLE `projecttype` (
`projectTypeID` int(11) NOT NULL AUTO_INCREMENT COMMENT '消费类型ID',
`projectTypeName` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '消费类型名称',
`projectTypePrice` double NOT NULL COMMENT '消费项目金额',
`staffTypeID` int(11) NULL DEFAULT NULL COMMENT '员工类型ID',
PRIMARY KEY (`projectTypeID`) USING BTREE,
INDEX `FK_projecttype_stafftype`(`staffTypeID`) USING BTREE,
CONSTRAINT `FK_projecttype_stafftype` FOREIGN KEY (`staffTypeID`) REFERENCES `stafftype` (`staffTypeID`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for staff
-- ----------------------------
DROP TABLE IF EXISTS `staff`;
CREATE TABLE `staff` (
`staffID` int(11) NOT NULL AUTO_INCREMENT COMMENT '员工ID',
`staffName` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '员工姓名',
`staffTypeID` int(11) NOT NULL COMMENT '员工类型ID',
PRIMARY KEY (`staffID`) USING BTREE,
INDEX `FK_staff_staffType`(`staffTypeID`) USING BTREE,
CONSTRAINT `FK_staff_staffType` FOREIGN KEY (`staffTypeID`) REFERENCES `stafftype` (`staffTypeID`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for stafftype
-- ----------------------------
DROP TABLE IF EXISTS `stafftype`;
CREATE TABLE `stafftype` (
`staffTypeID` int(11) NOT NULL AUTO_INCREMENT COMMENT '员工类型ID',
`staffTypeName` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '员工类型名称',
PRIMARY KEY (`staffTypeID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userID` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`userName` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '用户姓名',
`phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '用户电话',
`userbalance` double NOT NULL DEFAULT 0 COMMENT '用户余额',
`userAccount` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '用户账户',
`userPassword` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '用户密码',
PRIMARY KEY (`userID`) USING BTREE,
UNIQUE INDEX `IN_useraccount`(`userAccount`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for userblock
-- ----------------------------
DROP TABLE IF EXISTS `userblock`;
CREATE TABLE `userblock` (
`userBlockID` int(11) NOT NULL AUTO_INCREMENT COMMENT '开卡记录ID',
`userBlockStaffID` int(11) NULL DEFAULT NULL COMMENT '帮用户开卡的员工ID',
`rechargeable` double NOT NULL COMMENT '用户首次充值金额',
`openBlockTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '开卡时间',
`blockTypeID` int(11) NULL DEFAULT NULL COMMENT '卡类型ID',
`userID` int(11) NULL DEFAULT NULL COMMENT '开卡用户ID',
PRIMARY KEY (`userBlockID`) USING BTREE,
UNIQUE INDEX `FK_userblock_user`(`userID`) USING BTREE,
INDEX `FK_userblock_staff`(`userBlockStaffID`) USING BTREE,
INDEX `FK_userblock_blockType`(`blockTypeID`) USING BTREE,
CONSTRAINT `FK_userblock_blockType` FOREIGN KEY (`blockTypeID`) REFERENCES `blocktype` (`blockTypeID`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FK_userblock_staff` FOREIGN KEY (`userBlockStaffID`) REFERENCES `staff` (`staffID`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FK_userblock_user` FOREIGN KEY (`userID`) REFERENCES `user` (`userID`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 14 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Procedure structure for proc_selectBlockTypeIDByBlockName
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_selectBlockTypeIDByBlockName`;
delimiter ;;
CREATE PROCEDURE `proc_selectBlockTypeIDByBlockName`(IN money DOUBLE)
BEGIN
#Routine body goes here...
IF money<200 THEN
SELECT blockTypeID FROM blocktype WHERE blockTypeName='白银卡';
ELSEIF money>500 AND money<1000 THEN
SELECT blockTypeID FROM blocktype WHERE blockTypeName='白金卡';
ELSEIF money>1000 AND money<2000 THEN
SELECT blockTypeID FROM blocktype WHERE blockTypeName='黄金卡';
ELSEIF money>2000 THEN
SELECT blockTypeID FROM blocktype WHERE blockTypeName='钻石卡';
END IF;
END
;;
delimiter ;
SET FOREIGN_KEY_CHECKS = 1;
在 entity 包下,创建自己的实体类
可根据不同的业务功能,在实体类中增加额外的属性,方便自己编写
package cn.block.entity;
public class Blocktype {
private int blockTypeId;
private String blockTypeName;
private double blockdiscount;
private double maxRecharge;
private double minRecharge;
public Blocktype() {
}
@Override
public String toString() {
return "Blocktype{" +
"blockTypeId=" + blockTypeId +
", blockTypeName='" + blockTypeName + '\'' +
", blockdiscount=" + blockdiscount +
", maxRecharge=" + maxRecharge +
", minRecharge=" + minRecharge +
'}';
}
public Blocktype(int blockTypeId, String blockTypeName, double blockdiscount, double maxRecharge, double minRecharge) {
this.blockTypeId = blockTypeId;
this.blockTypeName = blockTypeName;
this.blockdiscount = blockdiscount;
this.maxRecharge = maxRecharge;
this.minRecharge = minRecharge;
}
public int getBlockTypeId() {
return blockTypeId;
}
public void setBlockTypeId(int blockTypeId) {
this.blockTypeId = blockTypeId;
}
public String getBlockTypeName() {
return blockTypeName;
}
public void setBlockTypeName(String blockTypeName) {
this.blockTypeName = blockTypeName;
}
public double getBlockdiscount() {
return blockdiscount;
}
public void setBlockdiscount(double blockdiscount) {
this.blockdiscount = blockdiscount;
}
public double getMaxRecharge() {
return maxRecharge;
}
public void setMaxRecharge(double maxRecharge) {
this.maxRecharge = maxRecharge;
}
public double getMinRecharge() {
return minRecharge;
}
public void setMinRecharge(double minRecharge) {
this.minRecharge = minRecharge;
}
}
package cn.block.entity;
public class Consumption {
private int consumptionId;
private int consumptionUserId;
private int consumptionProjectId;
private int staffId;
private double consumptionBalance;
private double userBalance;
private String consumptionDatetime;
private String userName;
private String staffName;
private String projectTypeName;
private String userAccount;
private String userPassword;
public Consumption() {
}
public Consumption(int consumptionId, int consumptionUserId, int consumptionProjectId, int staffId, double consumptionBalance, double userBalance, String consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword) {
this.consumptionId = consumptionId;
this.consumptionUserId = consumptionUserId;
this.consumptionProjectId = consumptionProjectId;
this.staffId = staffId;
this.consumptionBalance = consumptionBalance;
this.userBalance = userBalance;
this.consumptionDatetime = consumptionDatetime;
this.userName = userName;
this.staffName = staffName;
this.projectTypeName = projectTypeName;
this.userAccount = userAccount;
this.userPassword= userPassword;
}
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public int getConsumptionId() {
return consumptionId;
}
public void setConsumptionId(int consumptionId) {
this.consumptionId = consumptionId;
}
public int getConsumptionUserId() {
return consumptionUserId;
}
public void setConsumptionUserId(int consumptionUserId) {
this.consumptionUserId = consumptionUserId;
}
public int getConsumptionProjectId() {
return consumptionProjectId;
}
public void setConsumptionProjectId(int consumptionProjectId) {
this.consumptionProjectId = consumptionProjectId;
}
public int getStaffId() {
return staffId;
}
public void setStaffId(int staffId) {
this.staffId = staffId;
}
public double getConsumptionBalance() {
return consumptionBalance;
}
public void setConsumptionBalance(double consumptionBalance) {
this.consumptionBalance = consumptionBalance;
}
public double getUserBalance() {
return userBalance;
}
public void setUserBalance(double userBalance) {
this.userBalance = userBalance;
}
public String getConsumptionDatetime() {
return consumptionDatetime;
}
public void setConsumptionDatetime(String consumptionDatetime) {
this.consumptionDatetime = consumptionDatetime;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getStaffName() {
return staffName;
}
public void setStaffName(String staffName) {
this.staffName = staffName;
}
public String getProjectTypeName() {
return projectTypeName;
}
public void setProjectTypeName(String projectTypeName) {
this.projectTypeName = projectTypeName;
}
@Override
public String toString() {
return "Consumption{" +
"consumptionId=" + consumptionId +
", consumptionUserId=" + consumptionUserId +
", consumptionProjectId=" + consumptionProjectId +
", staffId=" + staffId +
", consumptionBalance=" + consumptionBalance +
", userBalance=" + userBalance +
", consumptionDatetime='" + consumptionDatetime + '\'' +
", userName='" + userName + '\'' +
", staffName='" + staffName + '\'' +
", projectTypeName='" + projectTypeName + '\'' +
", userAccount='" + userAccount + '\'' +
", userPassword='" + userPassword + '\'' +
'}';
}
}
package cn.block.entity;
public class Manageruser {
private int managerId;
private String managerUserName;
private String managerPassword;
@Override
public String toString() {
return "Manageruser{" +
"managerId=" + managerId +
", managerUserName='" + managerUserName + '\'' +
", managerPassword='" + managerPassword + '\'' +
'}';
}
public Manageruser(int managerId, String managerUserName, String managerPassword) {
this.managerId = managerId;
this.managerUserName = managerUserName;
this.managerPassword = managerPassword;
}
public Manageruser() {
}
public int getManagerId() {
return managerId;
}
public void setManagerId(int managerId) {
this.managerId = managerId;
}
public String getManagerUserName() {
return managerUserName;
}
public void setManagerUserName(String managerUserName) {
this.managerUserName = managerUserName;
}
public String getManagerPassword() {
return managerPassword;
}
public void setManagerPassword(String managerPassword) {
this.managerPassword = managerPassword;
}
}
package cn.block.entity;
public class Projecttype {
private int projectTypeId;
private String projectTypeName;
private double projectTypePrice;
private int staffTypeId;
private String staffTypeName;
@Override
public String toString() {
return "Projecttype{" +
"projectTypeId=" + projectTypeId +
", projectTypeName='" + projectTypeName + '\'' +
", projectTypePrice=" + projectTypePrice +
", staffTypeId=" + staffTypeId +
", staffTypeName='" + staffTypeName + '\'' +
'}';
}
public Projecttype(int projectTypeId, String projectTypeName, double projectTypePrice, int staffTypeId, String staffTypeName) {
this.projectTypeId = projectTypeId;
this.projectTypeName = projectTypeName;
this.projectTypePrice = projectTypePrice;
this.staffTypeId = staffTypeId;
this.staffTypeName = staffTypeName;
}
public Projecttype() {
}
public int getProjectTypeId() {
return projectTypeId;
}
public void setProjectTypeId(int projectTypeId) {
this.projectTypeId = projectTypeId;
}
public String getProjectTypeName() {
return projectTypeName;
}
public void setProjectTypeName(String projectTypeName) {
this.projectTypeName = projectTypeName;
}
public double getProjectTypePrice() {
return projectTypePrice;
}
public void setProjectTypePrice(double projectTypePrice) {
this.projectTypePrice = projectTypePrice;
}
public int getStaffTypeId() {
return staffTypeId;
}
public void setStaffTypeId(int staffTypeId) {
this.staffTypeId = staffTypeId;
}
public String getStaffTypeName() {
return staffTypeName;
}
public void setStaffTypeName(String staffTypeName) {
this.staffTypeName = staffTypeName;
}
}
package cn.block.entity;
public class Staff {
private int staffId;
private String staffName;
private int staffTypeId;
private String staffTypeName;
public String getStaffTypeName() {
return staffTypeName;
}
public void setStaffTypeName(String staffTypeName) {
this.staffTypeName = staffTypeName;
}
public int getStaffId() {
return staffId;
}
public void setStaffId(int staffId) {
this.staffId = staffId;
}
public String getStaffName() {
return staffName;
}
public void setStaffName(String staffName) {
this.staffName = staffName;
}
public int getStaffTypeId() {
return staffTypeId;
}
public void setStaffTypeId(int staffTypeId) {
this.staffTypeId = staffTypeId;
}
public Staff() {
}
public Staff(int staffId, String staffName, int staffTypeId, String staffTypeName) {
this.staffId = staffId;
this.staffName = staffName;
this.staffTypeId = staffTypeId;
this.staffTypeName = staffTypeName;
}
@Override
public String toString() {
return "Staff{" +
"staffId=" + staffId +
", staffName='" + staffName + '\'' +
", staffTypeId=" + staffTypeId +
", staffTypeName='" + staffTypeName + '\'' +
'}';
}
}
package cn.block.entity;
public class Stafftype {
private int staffTypeId;
private String staffTypeName;
public int getStaffTypeId() {
return staffTypeId;
}
public void setStaffTypeId(int staffTypeId) {
this.staffTypeId = staffTypeId;
}
public String getStaffTypeName() {
return staffTypeName;
}
public void setStaffTypeName(String staffTypeName) {
this.staffTypeName = staffTypeName;
}
public Stafftype() {
}
public Stafftype(int staffTypeId, String staffTypeName) {
this.staffTypeId = staffTypeId;
this.staffTypeName = staffTypeName;
}
@Override
public String toString() {
return "Stafftype{" +
"staffTypeId=" + staffTypeId +
", staffTypeName='" + staffTypeName + '\'' +
'}';
}
}
package cn.block.entity;
public class User {
private int userId;
private String userName;
private String phone;
private double userbalance;
private String userAccount;
private String userPassword;
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", phone='" + phone + '\'' +
", userbalance=" + userbalance +
", userAccount='" + userAccount + '\'' +
", userPassword='" + userPassword + '\'' +
'}';
}
public User() {
}
public User(int userId, String userName, String phone, double userbalance, String userAccount, String userPassword) {
this.userId = userId;
this.userName = userName;
this.phone = phone;
this.userbalance = userbalance;
this.userAccount = userAccount;
this.userPassword = userPassword;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public double getUserbalance() {
return userbalance;
}
public void setUserbalance(double userbalance) {
this.userbalance = userbalance;
}
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
package cn.block.entity;
public class Userblock {
private int userBlockId;
private int userBlockStaffId;
private double rechargeable;
private String openBlockTime;
private int blockTypeId;
private int userId;
private String userName;
public int getUserBlockId() {
return userBlockId;
}
public void setUserBlockId(int userBlockId) {
this.userBlockId = userBlockId;
}
public int getUserBlockStaffId() {
return userBlockStaffId;
}
public void setUserBlockStaffId(int userBlockStaffId) {
this.userBlockStaffId = userBlockStaffId;
}
public double getRechargeable() {
return rechargeable;
}
public void setRechargeable(double rechargeable) {
this.rechargeable = rechargeable;
}
public String getOpenBlockTime() {
return openBlockTime;
}
public void setOpenBlockTime(String openBlockTime) {
this.openBlockTime = openBlockTime;
}
public int getBlockTypeId() {
return blockTypeId;
}
public void setBlockTypeId(int blockTypeId) {
this.blockTypeId = blockTypeId;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public Userblock() {
}
@Override
public String toString() {
return "Userblock{" +
"userBlockId=" + userBlockId +
", userBlockStaffId=" + userBlockStaffId +
", rechargeable=" + rechargeable +
", openBlockTime='" + openBlockTime + '\'' +
", blockTypeId=" + blockTypeId +
", userId=" + userId +
", userName='" + userName + '\'' +
'}';
}
public Userblock(int userBlockId, int userBlockStaffId, double rechargeable, String openBlockTime, int blockTypeId, int userId, String userName) {
this.userBlockId = userBlockId;
this.userBlockStaffId = userBlockStaffId;
this.rechargeable = rechargeable;
this.openBlockTime = openBlockTime;
this.blockTypeId = blockTypeId;
this.userId = userId;
this.userName = userName;
}
}
定义对数据库的增删改查的方法
package cn.block.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class BaseDao {
public static String driver;
public static String url;
public static String user;
public static String password;
//静态方法,创建该类时自动调用
static {
init();
}
//配置文件
public static void init(){
String file="database.properties";
InputStream in=BaseDao.class.getClassLoader().getResourceAsStream(file);
Properties ties=new Properties();
try {
ties.load(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
driver= ties.getProperty("driver");
url=ties.getProperty("url");
user=ties.getProperty("user");
password=ties.getProperty("password");
}
//建立数据库连接
public static Connection getConn(){
Connection conn=null;
//启动驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
try {
conn= DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return conn;
}
//关闭资源
public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (ps!=null){
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
//执行增加,删除,修改操作
public static int update(String sql,Object [] objects){
Connection conn=getConn();
PreparedStatement preparedStatement=null;
int a=0;
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(objects!=null){
for (int i = 0; i < objects.length; i++) {
try {
preparedStatement.setObject(i+1, objects[i]);
} catch (SQLException e) {
System.out.println("宝宝出错了哦");
e.printStackTrace();
}
}
System.out.println(preparedStatement.toString());
}
try {
a=preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
closeAll(conn,preparedStatement,null);
return a;
}
}
package cn.block.dao;
import cn.block.entity.Blocktype;
import java.sql.SQLException;
import java.util.List;
public interface blockTypeDao {
//查询所有会员卡类型的信息
public List<Blocktype> getAllBlockType() throws SQLException;
//通过充值金额 显示会员卡类型ID
public Blocktype getBlockTypeIDByPrice(double price) throws SQLException;
//通过会员卡 ID 显示会员卡信息
public Blocktype getBlockTypeByblockTypeID(int blockTypeID) throws SQLException;
}
package cn.block.dao;
import cn.block.entity.Consumption;
import java.sql.SQLException;
import java.util.List;
public interface consumptionDao {
//修改员工ID 修改外另一位员工ID
public int updateStaffIDbyStaffID(int staffID);
//查询所有用户的消费记录
public List<Consumption> getAllConsumption() throws SQLException;
public List<Consumption> selectConsumptionByUserID(int UserID);
//增加消费记录
public int insertConsumption(Consumption consumption);
//根据用户的账户和密码 查询自己的消费记录
public List<Consumption> getConsumptionByUseraccountANDUserPassword(String userAccount,String userPassword) throws SQLException;
//查询刚消费的用户消费记录
public Consumption getMaxConsumptionByUserID(int UserID) throws SQLException;
}
package cn.block.dao;
import cn.block.entity.Manageruser;
import java.sql.SQLException;
public interface managerUserDao {
//通过账户密码查看用户信息
public Manageruser getmanagerUserInfoByManagerUserNameANDManagerPassword(String ManagerUserName, String ManagerPassword) throws SQLException;
}
package cn.block.dao;
import cn.block.entity.Projecttype;
import java.sql.SQLException;
import java.util.List;
/*
* 项目类型表
* */
public interface projectTypeDao {
//查询所有项目类型
public List<Projecttype> selectAllProjectTypes() throws SQLException;
//通过 projectTypeID 获取 projectTypePrice
public double getProjectTypePriceByprojectTypeID(int projectTypeID) throws SQLException;
//增加项目
public int insertProjectType(Projecttype projectType) throws SQLException;
//通过 projectTypeID 查询项目类型
public Projecttype getProjectTypeByprojectTypeID(int projectTypeID) throws SQLException;
//查询项目,不显示特殊业务
public List<Projecttype> selectAliProjectTypes() throws SQLException;
}
package cn.block.dao;
import cn.block.entity.Staff;
import java.sql.SQLException;
import java.util.List;
/*
* 员工表
* */
public interface staffDao {
//查询所有员工
public List<Staff> selectAllStaff() throws SQLException;
//通过员工ID查询员工
public Staff selectStaffByStaffID (int staffID) throws SQLException;
//添加员工
public int insertStaff(Staff staff) throws SQLException;
//通过员工ID删除员工
public int deleteStaff(int staffID) throws SQLException;
//通过员工ID查询员工类型ID
public int getStaffTypeIDByStaffID(int staffID) throws SQLException;
}
package cn.block.dao;
import cn.block.entity.Stafftype;
import java.sql.SQLException;
import java.util.List;
/*
* 员工类型
* */
public interface staffTypeDao {
//查询所有员工类型
public List<Stafftype> getAllStaffTypes() throws SQLException;
//添加员工类型
public int insertStaffType(Stafftype staffType) throws SQLException;
//通过员工类型ID 查询员工类型信息
public Stafftype getStaffTypeInfoByStaffTypeID(int staffTypeID) throws SQLException;
}
package cn.block.dao;
import cn.block.entity.Userblock;
import java.sql.SQLException;
/*
* 会员用户
* */
public interface userblockDao {
//添加会员用户
public int insertUserblock(Userblock userblock);
//修改员工ID 修改外另一位员工ID
public int updateuserBlockStaffIDbyuserBlockStaffID(int staffID);
//通过用户账户ID用户会员卡等级
public int updateUserblockTyleIDByuserAccount(int blockTypeID,int userID);
//通过用户ID 显示会员信息
public Userblock getUserblockInfoByUserID(int userID) throws SQLException;
//通过用户 ID 修改用户会员等级
}
package cn.block.dao;
import cn.block.entity.User;
import java.sql.SQLException;
import java.util.List;
/*
* 用户表
* */
public interface userDao {
//添加用户
public int insertUser(User user) throws SQLException;
//删除用户 根据用户ID
public int deleteUserbyUserID(int userID) throws SQLException;
//根据用户账户 修改用户名称
public int updateUserNameByUserID(String userAccount,String userName) throws SQLException;
//根据用户账户 修改用户手机号
public int updateUserPhoneByUserID(String userAccount,String userPhone);
//根据用户账户 修改用户余额
public int updateUserbalanceByUserAccount(String userAccount,double userbalance);
//查询所有用户
public List<User> getAllUser() throws SQLException;
//根据用户账户 获取用户余额
public double getUserBalanceByUserAccoount(String userAccount) throws SQLException;
//通过账户密码查看用户信息
public User getUserInfoByUserAccountANDUserPassword(String userAccount,String userPassword) throws SQLException;
//通过账户密码查看用户信息
public User getUserInfoByUserAccount(String userAccount) throws SQLException;
}
实现 dao 包下所定义的接口类,进行对数据库的增删改查
package cn.block.dao.impl;
import cn.block.dao.blockTypeDao;
import cn.block.dao.BaseDao;
import cn.block.entity.Blocktype;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class blockTypeDaoMySQLImpl extends BaseDao implements blockTypeDao {
@Override
public List<Blocktype> getAllBlockType() throws SQLException {
List<Blocktype> listBlocktype=new ArrayList<Blocktype>();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from blocktype";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while(rs.next()){
Blocktype blocktype=new Blocktype(rs.getInt("blockTypeID"),rs.getString("blockTypeName"),rs.getDouble("blockdiscount"),rs.getDouble("maxRecharge"),rs.getDouble("minRecharge"));
listBlocktype.add(blocktype);
}
closeAll(conn,preparedStatement,rs);
return listBlocktype;
}
/*
*
* */
@Override
public Blocktype getBlockTypeIDByPrice(double money) throws SQLException {
Blocktype blocktype=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="Select * from blocktype WHERE ? BETWEEN minRecharge AND maxRecharge";
try {
preparedStatement=conn.prepareStatement(sql);
preparedStatement.setDouble(1,money);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
blocktype=new Blocktype(rs.getInt("blockTypeID"),rs.getString("blockTypeName"),rs.getDouble("blockdiscount"),rs.getDouble("maxRecharge"),rs.getDouble("minRecharge"));
}
closeAll(conn,preparedStatement,rs);
return blocktype;
}
@Override
public Blocktype getBlockTypeByblockTypeID(int blockTypeID) throws SQLException {
Blocktype blocktype=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from blocktype where blockTypeID=?";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
preparedStatement.setInt(1, blockTypeID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
blocktype=new Blocktype(rs.getInt("blockTypeID"),rs.getString("blockTypeName"),rs.getDouble("blockdiscount"),rs.getDouble("maxRecharge"),rs.getDouble("minRecharge"));
}
closeAll(conn,preparedStatement,rs);
return blocktype;
}
}
package cn.block.dao.impl;
import cn.block.dao.consumptionDao;
import cn.block.dao.BaseDao;
import cn.block.entity.Consumption;
import java.io.*;
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 consumptionDaoMySQLImpl extends BaseDao implements consumptionDao {
@Override
public int updateStaffIDbyStaffID(int staffID) {
String sql="update consumption set staffID=1 where staffID=?;";
Object [] objects={staffID};
return update(sql, objects);
}
@Override
public List<Consumption> getAllConsumption() throws SQLException {
List<Consumption> listConsumption = new ArrayList<Consumption>();
Consumption consumption=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="SELECT consumptionID,user.userName,projecttype.projectTypeName,staff.staffName,consumption.consumptionBalance,consumption.userBalance,consumptionDatetime,user.userAccount,user.userPassword\n" +
"FROM consumption\n" +
"INNER JOIN `user` ON consumption.consumptionUserID=`user`.userID \n" +
"INNER JOIN projecttype ON consumption.consumptionProjectID=projecttype.projectTypeID \n" +
"INNER JOIN staff ON consumption.staffID=staff.staffID ;" ;
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while(rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
consumption=new Consumption(rs.getInt("consumptionID"),0,0,0,rs.getDouble("consumption.consumptionBalance"),rs.getDouble("consumption.userBalance"),rs.getString("consumptionDatetime"),rs.getString("user.userName"),rs.getString("staff.staffName"),rs.getString("projecttype.projectTypeName"),rs.getString("userAccount"),rs.getString("userPassword"));
listConsumption.add(consumption);
}
closeAll(conn,preparedStatement,rs);
return listConsumption;
}
@Override
public List<Consumption> selectConsumptionByUserID(int UserID) {
return null;
}
@Override
public int insertConsumption(Consumption consumption) {
String sql="insert into consumption (consumptionUserID, consumptionProjectID, staffID, consumptionBalance,userBalance)\n" +
"values (?,?,?,?,?);";
Object [] objects={consumption.getConsumptionUserId(),consumption.getConsumptionProjectId(),consumption.getStaffId(),consumption.getConsumptionBalance(),consumption.getUserBalance()};
//打印发票
int result=update(sql, objects);
try {
daYin(consumption);
} catch (IOException e) {
e.printStackTrace();
System.out.println("出错了哦,宝宝");
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("出错了哦,宝宝");
}
//使用 start() 方法启动线程
return result;
}
//打印小票
public void daYin(Consumption consumption1) throws IOException, InterruptedException {
Consumption consumption=null;
try {
consumption=getMaxConsumptionByUserID(consumption1.getConsumptionUserId());
} catch (SQLException e) {
e.printStackTrace();
System.out.println("出错了哦,宝宝");
}
String line="";
String Name=consumption.getUserName();
int ID=consumption.getConsumptionId();
String Pin=Name+ ID+".txt";
String fileName="D:\\BeiDaQingNiao\\S2\\MySQL_JDBC\\src\\cn\\block\\consumptionPiao\\"+Pin;
String FileName=fileName.trim();
File file=new File(FileName);
Writer writer=null;
try {
writer=new FileWriter(file);
} catch (IOException e) {
e.printStackTrace();
System.out.println("不好意思,出错了哦宝宝");
}
BufferedWriter bw=new BufferedWriter(writer);
System.out.println(Name+":"+ID+":发票");
line="消费用户:"+consumption.getUserName();
System.out.println(line);
bw.write(line);
bw.newLine();
line="消费项目:"+consumption.getProjectTypeName();
System.out.println(line);
bw.write(line);
bw.newLine();
line="服务员工:"+consumption.getStaffName();
System.out.println(line);
bw.write(line);
bw.newLine();
line="金额流水:"+consumption.getConsumptionBalance();
System.out.println(line);
bw.write(line);
bw.newLine();
line="账户余额:"+consumption.getUserBalance();
System.out.println(line);
bw.write(line);
bw.newLine();
line="消费时间:"+consumption.getConsumptionDatetime();
System.out.println(line);
bw.write(line);
bw.flush();
bw.close();
}
@Override
public List<Consumption> getConsumptionByUseraccountANDUserPassword(String userAccount, String userPassword) throws SQLException {
List<Consumption> listConsumption=new ArrayList<Consumption>();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="SELECT consumptionID,user.userName,projecttype.projectTypeName,staff.staffName,consumption.consumptionBalance,consumption.userBalance,consumptionDatetime,user.userAccount,user.userPassword\n" +
"FROM consumption\n" +
"INNER JOIN `user` ON consumption.consumptionUserID=`user`.userID \n" +
"INNER JOIN projecttype ON consumption.consumptionProjectID=projecttype.projectTypeID \n" +
"INNER JOIN staff ON consumption.staffID=staff.staffID \n" +
"WHERE user.userAccount=? AND user.userPassword=? ;";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
preparedStatement.setString(1,userAccount);
preparedStatement.setString(2,userPassword);
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while (rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
listConsumption.add(new Consumption(rs.getInt("consumptionID"),0,0,0,rs.getDouble("consumption.consumptionBalance"),rs.getDouble("consumption.userBalance"),rs.getString("consumptionDatetime"),rs.getString("user.userName"),rs.getString("staff.staffName"),rs.getString("projecttype.projectTypeName"),rs.getString("userAccount"),rs.getString("userPassword")));
}
closeAll(conn,preparedStatement,rs);
return listConsumption;
}
@Override
public Consumption getMaxConsumptionByUserID(int UserID) throws SQLException {
Consumption consumption=new Consumption();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="SELECT consumptionID,user.userName,projecttype.projectTypeName,staff.staffName,consumption.consumptionBalance,consumption.userBalance,consumptionDatetime,user.userAccount,user.userPassword\n" +
"FROM consumption\n" +
"INNER JOIN `user` ON consumption.consumptionUserID=`user`.userID\n" +
"INNER JOIN projecttype ON consumption.consumptionProjectID=projecttype.projectTypeID\n" +
"INNER JOIN staff ON consumption.staffID=staff.staffID\n" +
"WHERE consumptionUserID=? AND consumptionDatetime=(SELECT MAX(consumptionDatetime) FROM consumption WHERE consumptionUserID=?);\n";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
preparedStatement.setInt(1,UserID);
preparedStatement.setInt(2,UserID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while (rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
consumption=new Consumption(rs.getInt("consumptionID"),0,0,0,rs.getDouble("consumption.consumptionBalance"),rs.getDouble("consumption.userBalance"),rs.getString("consumptionDatetime"),rs.getString("user.userName"),rs.getString("staff.staffName"),rs.getString("projecttype.projectTypeName"),rs.getString("userAccount"),rs.getString("userPassword"));
}
closeAll(conn,preparedStatement,rs);
return consumption;
}
}
package cn.block.dao.impl;
import cn.QiaoYun.dao.BaseDao;
import cn.block.dao.managerUserDao;
import cn.block.entity.Manageruser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import static cn.block.dao.BaseDao.getConn;
public class managerUserDaoMySQLImpl extends BaseDao implements managerUserDao {
@Override
public Manageruser getmanagerUserInfoByManagerUserNameANDManagerPassword(String ManagerUserName, String ManagerPassword) throws SQLException {
Manageruser manageruser=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from manageruser where ManagerUserName=? and ManagerPassword=? ";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
preparedStatement.setString(1, ManagerUserName);
preparedStatement.setString(2, ManagerPassword);
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
manageruser =new Manageruser(rs.getInt("ManagerID"),rs.getString("ManagerUserName"),rs.getString("ManagerPassword"));
}
closeAll(conn,preparedStatement,rs);
return manageruser;
}
}
package cn.block.dao.impl;
import cn.block.dao.projectTypeDao;
import cn.block.dao.BaseDao;
import cn.block.entity.Projecttype;
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 projectTypeDaoMySQLImpl extends BaseDao implements projectTypeDao {
@Override
public List<Projecttype> selectAllProjectTypes() throws SQLException {
List<Projecttype> listProjectTypes=new ArrayList<Projecttype>();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select projectTypeID,projectTypeName,projectTypePrice,staffTypeName from projecttype INNER JOIN stafftype ON projecttype.staffTypeID=stafftype.staffTypeID";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while(rs.next()){
Projecttype projectType=new Projecttype(rs.getInt("projectTypeID"),rs.getString("projectTypeName"),rs.getDouble("projectTypePrice"),rs.getInt("projectTypeID"),rs.getString("staffTypeName"));
listProjectTypes.add(projectType);
}
closeAll(conn,preparedStatement,rs);
return listProjectTypes;
}
public List<Projecttype> selectAliProjectTypes() throws SQLException {
List<Projecttype> listProjectTypes=new ArrayList<Projecttype>();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select projectTypeID,projectTypeName,projectTypePrice,staffTypeName from projecttype INNER JOIN stafftype ON projecttype.staffTypeID=stafftype.staffTypeID WHERE NOT projecttype.staffTypeID=1";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while(rs.next()){
Projecttype projectType=new Projecttype(rs.getInt("projectTypeID"),rs.getString("projectTypeName"),rs.getDouble("projectTypePrice"),rs.getInt("projectTypeID"),rs.getString("staffTypeName"));
listProjectTypes.add(projectType);
}
closeAll(conn,preparedStatement,rs);
return listProjectTypes;
}
@Override
public double getProjectTypePriceByprojectTypeID(int projectTypeID) throws SQLException {
double i=0;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from projecttype where projectTypeID=?";
try {
preparedStatement=conn.prepareStatement(sql);
preparedStatement.setInt(1,projectTypeID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
i=rs.getInt("projectTypePrice");
}
closeAll(conn,preparedStatement,rs);
return i;
}
@Override
public int insertProjectType(Projecttype projectType) throws SQLException {
String sql="insert into projecttype (projectTypeName, projectTypePrice, staffTypeID)\n" +
"values (?,?,?);";
Object [] objects={projectType.getProjectTypeName(),projectType.getProjectTypePrice(),projectType.getStaffTypeId()};
return update(sql, objects);
}
@Override
public Projecttype getProjectTypeByprojectTypeID(int projectTypeID) throws SQLException {
Projecttype projecttype=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from projecttype where projectTypeID=?";
try {
preparedStatement=conn.prepareStatement(sql);
preparedStatement.setInt(1,projectTypeID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
projecttype=new Projecttype(rs.getInt("projectTypeID"),rs.getString("projectTypeName"),rs.getDouble("projectTypePrice"),rs.getInt("staffTypeID"),"1");
}
closeAll(conn,preparedStatement,rs);
return projecttype;
}
}
package cn.block.dao.Impl;
import cn.block.dao.staffDao;
import cn.block.dao.BaseDao;
import cn.block.entity.Staff;
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 staffDaoMySQLImpl extends BaseDao implements staffDao {
@Override
public List<Staff> selectAllStaff() throws SQLException {
List<Staff> listStaffs=new ArrayList<Staff>();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="SELECT staffID,staffName,stafftype.staffTypeID,staffTypeName from staff INNER JOIN stafftype ON staff.staffTypeID=stafftype.staffTypeID";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while(rs.next()){
Staff staff=new Staff(rs.getInt("staffID"),rs.getString("staffName"),rs.getInt(3),rs.getString("staffTypeName"));
listStaffs.add(staff);
}
closeAll(conn,preparedStatement,rs);
return listStaffs;
}
@Override
public Staff selectStaffByStaffID(int staffID) throws SQLException {
Staff staff=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from staff where staffID=? ";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
preparedStatement.setInt(1, staffID);
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
staff=new Staff(rs.getInt("staffID"),rs.getString("staffName"),rs.getInt("staffTypeID"),"");
}
closeAll(conn,preparedStatement,rs);
return staff;
}
@Override
public int insertStaff(Staff staff) throws SQLException {
String sql="insert into staff (staffName, staffTypeID)\n" +
"values (?,?);";
Object [] objects={staff.getStaffName(),staff.getStaffTypeId()};
return update(sql, objects);
}
@Override
public int deleteStaff(int staffID) throws SQLException {
String sql="delete\n" +
"from staff\n" +
"where staffID=?;";
Object [] objects={staffID};
return update(sql, objects);
}
@Override
public int getStaffTypeIDByStaffID(int staffID) throws SQLException {
int i=0;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select staffTypeID from staff where staffID=?";
try {
preparedStatement=conn.prepareStatement(sql);
preparedStatement.setInt(1,staffID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
i=rs.getInt("staffTypeID");
}
closeAll(conn,preparedStatement,rs);
return i;
}
}
package cn.block.dao.impl;
import cn.block.dao.staffTypeDao;
import cn.block.dao.BaseDao;
import cn.block.entity.Stafftype;
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 staffTypeDaoMySQLImpl extends BaseDao implements staffTypeDao {
@Override
public List<Stafftype> getAllStaffTypes() throws SQLException {
List<Stafftype> listStafftype=new ArrayList<Stafftype>();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from stafftype";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while(rs.next()){
Stafftype stafftype=new Stafftype(rs.getInt("staffTypeID"),rs.getString("staffTypeName"));
listStafftype.add(stafftype);
}
closeAll(conn,preparedStatement,rs);
return listStafftype;
}
@Override
public int insertStaffType(Stafftype staffType) throws SQLException {
String sql="insert into stafftype (staffTypeName)\n" +
"values (?);";
Object [] objects={staffType.getStaffTypeName()};
return update(sql, objects);
}
@Override
public Stafftype getStaffTypeInfoByStaffTypeID(int staffTypeID) throws SQLException {
Stafftype stafftype=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from stafftype where staffTypeID=?";
try {
preparedStatement=conn.prepareStatement(sql);
preparedStatement.setInt(1,staffTypeID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
stafftype=new Stafftype(rs.getInt("staffTypeID"),rs.getString("staffTypeName"));
}
closeAll(conn,preparedStatement,rs);
return stafftype;
}
}
package cn.block.dao.impl;
import cn.block.dao.userblockDao;
import cn.block.dao.BaseDao;
import cn.block.entity.Userblock;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class userblockDaoMySQLImpl extends BaseDao implements userblockDao {
@Override
public int insertUserblock(Userblock userblock) {
String sql="insert into userblock (userBlockStaffID, rechargeable, blockTypeID, userID)\n" +
"values (?,?,?,?);";
Object [] objects={userblock.getUserBlockStaffId(),userblock.getRechargeable(),userblock.getBlockTypeId(),userblock.getUserId()};
return update(sql, objects);
}
@Override
public int updateuserBlockStaffIDbyuserBlockStaffID(int staffID) {
String sql="update userblock set userBlockStaffID=1 where userBlockStaffID=?;";
Object [] objects={staffID};
return update(sql, objects);
}
@Override
public int updateUserblockTyleIDByuserAccount(int blockTypeID,int userID) {
String sql="update userblock\n" +
"set blockTypeID=? \n" +
"where userID=? ;";
Object [] objects={blockTypeID,userID};
return update(sql, objects);
}
@Override
public Userblock getUserblockInfoByUserID(int userID) throws SQLException {
Userblock userblock=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from userblock where userID=?";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
preparedStatement.setInt(1, userID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
userblock=new Userblock(rs.getInt("userBlockID"),rs.getInt("userBlockStaffID"),rs.getDouble("rechargeable"),"",rs.getInt("blockTypeID"),rs.getInt("userID"),"");
}
closeAll(conn,preparedStatement,rs);
return userblock;
}
}
package cn.block.dao.impl;
import cn.block.dao.userDao;
import cn.block.dao.BaseDao;
import cn.block.entity.User;
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 userDaoMySQLImpl extends BaseDao implements userDao {
@Override
public int insertUser(User user) throws SQLException {
String sql="insert into block.user (userName, phone, userbalance,userAccount,userPassword) values (?,?,?,?,?);";
Object [] objects={user.getUserName(), user.getPhone(), user.getUserbalance(),user.getUserAccount(),user.getUserPassword()};
return update(sql, objects);
}
@Override
public int deleteUserbyUserID(int userID) throws SQLException {
String sql="delete from block.user where userID=?;";
Object [] objects={userID};
return update(sql, objects);
}
@Override
public int updateUserNameByUserID(String userAccount, String userName) throws SQLException {
String sql="update block.user set userName=? where userAccount=?;";
Object [] objects={userName,userAccount};
return update(sql, objects);
}
@Override
public int updateUserPhoneByUserID(String userAccount, String userPhone) {
String sql="update block.user set phone=? where userAccount=?;";
Object [] objects={userPhone,userAccount};
return update(sql, objects);
}
@Override
public int updateUserbalanceByUserAccount(String userAccount, double userbalance) {
String sql="update block.user set userbalance=? where userAccount=?;";
Object [] objects={userbalance,userAccount};
return update(sql, objects);
}
@Override
public List<User> getAllUser() throws SQLException {
List<User> listUsers=new ArrayList<User>();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from user";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
while(rs.next()){
User user=new User(rs.getInt("userID"),rs.getString("userName"),rs.getString("phone"),rs.getDouble("userbalance"),rs.getString("userAccount"),rs.getString("userPassword"));
listUsers.add(user);
}
closeAll(conn,preparedStatement,rs);
return listUsers;
}
@Override
public double getUserBalanceByUserAccoount(String userAccount) throws SQLException {
double userbalance=0;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select userbalance from user where userAccount=? ";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
preparedStatement.setString(1, userAccount);
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
userbalance=rs.getDouble("userbalance");
}
closeAll(conn,preparedStatement,rs);
return userbalance;
}
@Override
public User getUserInfoByUserAccountANDUserPassword(String userAccount, String userPassword) throws SQLException {
User user=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from user where userAccount=? and userPassword=? ";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
preparedStatement.setString(1, userAccount);
preparedStatement.setString(2, userPassword);
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
user=new User(rs.getInt("userID"),rs.getString("userName"),rs.getString("phone"),rs.getDouble("userbalance"),rs.getString("userAccount"),rs.getString("userPassword"));
}
closeAll(conn,preparedStatement,rs);
return user;
}
@Override
public User getUserInfoByUserAccount(String userAccount) throws SQLException {
User user=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
Connection conn=getConn();
String sql="select * from user where userAccount=?";
try {
preparedStatement=conn.prepareStatement(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
preparedStatement.setString(1, userAccount);
try {
rs=preparedStatement.executeQuery();
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(rs.next()){
//consumptionDatetime, String userName, String staffName, String projectTypeName, String userAccount,String userPassword
user=new User(rs.getInt("userID"),rs.getString("userName"),rs.getString("phone"),rs.getDouble("userbalance"),rs.getString("userAccount"),rs.getString("userPassword"));
}
closeAll(conn,preparedStatement,rs);
return user;
}
}
定义各种业务的方法
负责服务客户的接口
package cn.block.service;
import java.sql.SQLException;
/*
* 客户端
* */
public interface clientManagerService {
//用户消费
/*
* 查询所有的项目类型
* 查询项目类型价格
* 查询工作人员
* 修改用户余额
* 增加消费记录
* */
public void userConsume(String userAccount,String userPassword) throws SQLException;
//用户登录系统,判断用户是否登录成功
public boolean userLogin(String userAccount,String userPassword);
//获取用户消费记录
public void getUserConsumption(String userAccount,String userPassword);
//查看用户信息(余额)
public void getUserInfo(String userAccount,String userPassword);
//查看所有类型卡的信息
public void getAllBlockType();
}
负责服务管理员的接口
package cn.block.service;
import cn.block.entity.User;
import java.sql.SQLException;
import java.util.List;
/*
* 服务端
* */
public interface ServiceManagerService {
//查看所有用户信息
public void getAllUserInfo() throws SQLException;
//添加用户 注册账号
void register() throws SQLException;
//根据用户账户 添加用户为会员用户
void setUserBlockByUserAccount() throws SQLException;
//查看所有员工
void selectAllStaff() throws SQLException;
//查看所有员工类型
void selectAllStaffType() throws SQLException;
//查询所有用户消费记录
public void SelectAllUserConsumption();
//充值业务
public void updateUserbalance() throws SQLException;
//升级用户会员等级
public void updateUserBlock() throws SQLException;
//增加项目
public void insertProject() throws SQLException;
//添加员工
public void insertStaff() throws SQLException;
//添加员工类型
public void insertStaffType();
//删除员工
public void deleteStaff() throws SQLException;
//管理员登录
public boolean ManagerLogin(String ManagerUserName,String ManagerPassword);
}
实现 Service 包中定义的接口,重新各种方法
通过调用各种 Dao 中的方法,来实现各种业务的完成
提供给客户使用的类
package cn.block.service.Impl;
import cn.block.dao.Impl.*;
import cn.block.dao.*;
import cn.block.entity.*;
import cn.block.service.clientManagerService;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class clientManagerServiceImpl implements clientManagerService {
Scanner input=new Scanner(System.in);
blockTypeDao blockTypeDao=new blockTypeDaoMySQLImpl();
consumptionDaoMySQLImpl consumptionDao=new consumptionDaoMySQLImpl();
projectTypeDao projectTypeDao=new projectTypeDaoMySQLImpl();
staffDao staffDao=new staffDaoMySQLImpl();
staffTypeDao staffTypeDao=new staffTypeDaoMySQLImpl();
userblockDao userblockDao=new userblockDaoMySQLImpl();
userDaoMySQLImpl userDao=new userDaoMySQLImpl();
/*
* 查询所有的项目类型
* 查询项目类型价格
* 查询工作人员
* 修改用户余额
* 增加消费记录
*/
//用户消费
@Override
public void userConsume(String userAccount,String userPassword) throws SQLException {
//用户折扣
double userDiscount=0;
//用户原余额
double userYuanBalance=0;
//消费后的金额
double userHouBalance=0;
//项目类型ID
int projectTypeID=0;
//项目类型价格
double projectTypePrice=0;
//工作人员ID
int choiceStaffID=0;
//工作人员类型ID
int staffTypeID=0;
//用户名
String userName="";
//流水
double LiuShui=0;
//用户ID
int userID=0;
Projecttype projecttype=null;
//存储集合
List<Projecttype> listProjtcType=null;
//通过账户密码获得用户ID
User user=userDao.getUserInfoByUserAccountANDUserPassword(userAccount,userPassword);
userID=user.getUserId();
userName=user.getUserName();
System.out.println("项目编号ID\t项目名称\t\t项目价格\t项目员工类型");
//查询所有项目类型
try {
listProjtcType=projectTypeDao.selectAliProjectTypes();
for (Projecttype projecttype2 : listProjtcType) {
System.out.println(projecttype2.getProjectTypeId()+"\t\t\t"+projecttype2.getProjectTypeName()+"\t\t"+projecttype2.getProjectTypePrice()+"\t"+projecttype2.getStaffTypeName());
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.out.print("请选择您要消费的项目编号:");
try{
projectTypeID=input.nextInt();
}catch (Exception e){
System.out.println("宝宝不要随便输入哦~");
}
//判断项目是否存在
boolean iss=false;
for(Projecttype p:listProjtcType){
if(p.getProjectTypeId()==projectTypeID){
iss=true;
}
}
if(iss){
//员工类型对象
//判断输入的项目是否存在
try {
//获取项目价格
Projecttype ppp=projectTypeDao.getProjectTypeByprojectTypeID(projectTypeID);
projectTypePrice=ppp.getProjectTypePrice();
//获取服务的工作人员类型id
staffTypeID=ppp.getStaffTypeId();
} catch (Exception e) {
e.printStackTrace();
System.out.println("出错了哦宝宝");
}
//选择服务用户的工作人员
System.out.println("工作人员编号\t工作人员姓名\t工作人员类型");
for (Staff staff : staffDao.selectAllStaff()) {
System.out.println(staff.getStaffId()+"\t\t"+staff.getStaffName()+"\t\t\t"+staff.getStaffTypeName());
}
System.out.print("请选择服务的员工编号:");
choiceStaffID=0;
try{
choiceStaffID=input.nextInt();
}catch (Exception e){
System.out.println("宝宝不要随便输入哦~");
}
//获取选择的工作人员的类型ID
int choiceStaffTypeID=staffDao.getStaffTypeIDByStaffID(choiceStaffID);
//判断是否有此员工
if(choiceStaffTypeID<=0){
System.out.println("选择失败,没有此员工");
}else{
//判断 如果选择的服务项目员工类型 与 选择工作人员的员工类型
if(staffTypeID!=choiceStaffTypeID){
System.out.println("选择失败");
System.out.println("体验项目的员工类型与您选择服务人员的类型不匹配");
}else{
//获取用户原金额
userYuanBalance=userDao.getUserBalanceByUserAccoount(userAccount);
if(userYuanBalance<projectTypePrice){
System.out.println("Sorry,您的余额不足");
}else {
//判断这个人是否有会员,进行打折
Userblock userblock=userblockDao.getUserblockInfoByUserID(userID);
if(userblock!=null){
Blocktype blocktype=blockTypeDao.getBlockTypeByblockTypeID(userblock.getBlockTypeId());
System.out.println("发现您的该用户是:"+blocktype.getBlockTypeName());
System.out.println("折扣额度为"+(blocktype.getBlockdiscount()*100)+"折");
projectTypePrice =projectTypePrice*blocktype.getBlockdiscount();
LiuShui=0-projectTypePrice;
System.out.println("实际消费为:"+projectTypePrice);
userHouBalance=userYuanBalance-projectTypePrice;
}else {
LiuShui=0-projectTypePrice;
userHouBalance=userYuanBalance-projectTypePrice;
}
//消费后的金额
//修改用户的金额
int a=userDao.updateUserbalanceByUserAccount(userAccount, userHouBalance);
if(a>0){
System.out.println("金额已扣除,本次消费"+projectTypePrice+"元");
System.out.println("流水:"+LiuShui);
}
//增加消费记录
int b=consumptionDao.insertConsumption(new Consumption(0,userID,projectTypeID,choiceStaffID,LiuShui,userHouBalance,"0","","","","",""));
if(a>0){
System.out.println("用户:"+userName+"已成功添加消费记录");
}
}
}
}
}else {
System.out.println("选择失败,没有此项目");
}
}
//用户登录系统,判断用户是否登录成功
public boolean userLogin(String userAccount,String userPassword){
boolean result=false;
User user=null;
try {
user=userDao.getUserInfoByUserAccountANDUserPassword(userAccount,userPassword);
} catch (SQLException e) {
System.out.println("宝宝出错了哦,不好意思奥!");
e.printStackTrace();
}
if(user!=null){
System.out.println("登录成功,欢迎你"+user.getUserName());
result=true;
}else{
result=false;
}
return result;
}
//获取用户消费记录
public void getUserConsumption(String userAccount,String userPassword){
List<Consumption> listConsumption=new ArrayList<Consumption>();
try {
listConsumption=consumptionDao.getConsumptionByUseraccountANDUserPassword(userAccount,userPassword);
} catch (SQLException e) {
System.out.println("宝宝出错了哦,不好意思奥!");
e.printStackTrace();
}
System.out.println("消费记录编号\t消费用户\t消费项目\t服务员工\t金额流水\t\t账户余额\t消费时间");
for (Consumption consumption : listConsumption) {
System.out.println(consumption.getConsumptionId()+"\t\t\t"+consumption.getUserName()+"\t\t"+consumption.getProjectTypeName()+"\t"+consumption.getStaffName()+"\t\t"+consumption.getConsumptionBalance()+"\t"+consumption.getUserBalance()+"\t\t"+consumption.getConsumptionDatetime());
}
}
//获取用户信息
@Override
public void getUserInfo(String userAccount, String userPassword) {
//用户对象
User user=null;
//
try {
user=userDao.getUserInfoByUserAccountANDUserPassword(userAccount,userPassword);
} catch (SQLException e) {
System.out.println("宝宝出错了哦,不好意思奥!");
e.printStackTrace();
}
System.out.println("用户编号\t用户姓名\t用户电话\t用户余额");
System.out.println(user.getUserId()+"\t\t"+user.getUserName()+"\t\t"+user.getPhone()+"\t\t"+user.getUserbalance());
}
@Override
public void getAllBlockType() {
List<Blocktype> listBlockTypes = new ArrayList<Blocktype>();
try {
listBlockTypes=blockTypeDao.getAllBlockType();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
System.out.println("会员卡编号\t会员卡类型\t会员卡打折额度\t充值最大额度\t充值最小额度");
for (Blocktype blockType : listBlockTypes) {
System.out.println(blockType.getBlockTypeId()+"\t\t\t"+blockType.getBlockTypeName()+"\t\t"+blockType.getBlockdiscount()+"\t\t\t"+blockType.getMaxRecharge()+"\t\t"+blockType.getMinRecharge());
}
}
}
提供给管理员使用的类
package cn.block.service.Impl;
import cn.block.dao.Impl.*;
import cn.block.dao.*;
import cn.block.entity.*;
import cn.block.service.ServiceManagerService;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ServiceManagerServiceImpl implements ServiceManagerService {
managerUserDao managerUserDao=new managerUserDaoMySQLImpl();
userDao userDao=new userDaoMySQLImpl();
consumptionDao consumptionDao=new consumptionDaoMySQLImpl();
userblockDao userblockDao=new userblockDaoMySQLImpl();
blockTypeDao blockTypeDao=new blockTypeDaoMySQLImpl();
Scanner input=new Scanner(System.in);
staffDao staffDao=new staffDaoMySQLImpl();
staffTypeDao staffTypeDao=new staffTypeDaoMySQLImpl();
projectTypeDao projectTypeDao=new projectTypeDaoMySQLImpl();
@Override
public void getAllUserInfo() throws SQLException {
System.out.println("用户编号\t用户姓名\t用户电话\t用户余额\t用户账户\t用户密码");
for (User user : userDao.getAllUser()) {
System.out.println(user.getUserId()+"\t"+user.getUserName()+"\t"+user.getPhone()+"\t"+user.getUserbalance()+"\t"+user.getUserAccount()+"\t"+user.getUserPassword());
}
}
@Override
//用户注册
public void register() throws SQLException {
//流水
double LiuShui=0;
//卡类型对象
Blocktype blockType1=null;
//用户对象
User user=null;
//办卡员工ID
int BlockStaffID=0;
//用户首次充值金额
double rechargeable=0;
//卡类型ID
int blockTypeID=0;
//用户ID
int userID=0;
//余额
double userbalance=0;
String userPassword=null;
System.out.print("请输入用户名:");
String userName=input.next();
System.out.print("请输入用户手机号:");
String userPhone=input.next();
System.out.print("请设置登录账号:");
String userAccount=input.next();
//判断账号是否重复
try {
user=userDao.getUserInfoByUserAccount(userAccount);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
//如果没有 则执行
if(user==null){
System.out.print("请设置登录密码:");
userPassword=input.next();
System.out.println("新人充值金额送会员");
List<Blocktype> listBlockTypes = new ArrayList<Blocktype>();
try {
listBlockTypes=blockTypeDao.getAllBlockType();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
System.out.println("会员卡编号\t会员卡类型\t会员卡打折额度\t充值最大额度\t充值最小额度");
for (Blocktype blockType : listBlockTypes) {
System.out.println(blockType.getBlockTypeId()+"\t\t\t"+blockType.getBlockTypeName()+"\t\t"+blockType.getBlockdiscount()+"\t\t\t"+blockType.getMaxRecharge()+"\t\t"+blockType.getMinRecharge());
}
System.out.print("是否需要充值(Y/N):");
String isChongZhi=input.next();
if(isChongZhi.equalsIgnoreCase("y")){
System.out.print("请输入充值金额(最低200):");
rechargeable=input.nextDouble();
if(rechargeable<200){
System.out.println("充值失败,金额最低200");
}else {
//根据充值金额 判断卡的类型
try {
blockType1=blockTypeDao.getBlockTypeIDByPrice(rechargeable);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
System.out.println("恭喜您,你获得了:"+blockType1.getBlockTypeName());
blockTypeID=blockType1.getBlockTypeId();
System.out.println("工作人员编号\t工作人员姓名\t工作人员类型");
for (Staff staff : staffDao.selectAllStaff()) {
System.out.println(staff.getStaffId()+"\t\t"+staff.getStaffName()+"\t\t\t"+staff.getStaffTypeName());
}
System.out.print("请选择服务的员工编号:");
BlockStaffID=0;
try{
BlockStaffID=input.nextInt();
}catch (Exception e){
System.out.println("宝宝不要随便输入哦~");
}
//获取选择的工作人员的类型ID
int choiceStaffTypeID= 0;
try {
choiceStaffTypeID = staffDao.getStaffTypeIDByStaffID(BlockStaffID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
//判断是否有此员工
if(choiceStaffTypeID<=0){
System.out.println("选择失败,没有此员工");
}else{
//添加用户信息
int a=0;
try {
a=userDao.insertUser(new User(0,userName,userPhone,rechargeable,userAccount,userPassword));
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
if(a>0){
System.out.println("用户注册成功");
//获取用户I User user2=new User();
System.out.println(userDao.getUserInfoByUserAccountANDUserPassword(userAccount, userPassword));
//获取用户ID
User u1=userDao.getUserInfoByUserAccountANDUserPassword(userAccount, userPassword);
int userID1=u1.getUserId();
//添加会员信息
int b=userblockDao.insertUserblock(new Userblock(0,BlockStaffID,rechargeable,"",blockTypeID,userID1,"0"));
if(b>0){
System.out.println("用户会员添加成功");
//添加消费记录
int c=consumptionDao.insertConsumption(new Consumption(0,userID1,5,BlockStaffID,rechargeable,rechargeable,"0","","","","",""));
if(c>0){
System.out.println("消费记录添加成功");
}else {
System.out.println("消费记录添加失败");
}
}else {
System.out.println("用户会员添加失败");
}
}else {
System.out.println("注册失败");
}
}
}
}else {
}
}else{
System.out.println("已存在的用户,注册失败");
}
}
//根据用户账户 添加用户为会员用户
@Override
public void setUserBlockByUserAccount() throws SQLException {
//流水
double LiuSHui=0;
//用户ID
int userID=0;
//办卡员工
int BlockStaffID;
//卡类型对象
Blocktype blockType1=null;
//卡ID
int blockTypeID=0;
//原余额
double userYuanBalance=0;
//充值后余额
double userHouBalance=0;
//用户对象
User user=null;
//用户第一次充值金额
double rechargeable=0;
System.out.print("请输入用户账户:");
String userAccount=input.next();
//判断是否有此用户
try {
user=userDao.getUserInfoByUserAccount(userAccount);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
if(user!=null) {
//获取用户ID
User user2 = userDao.getUserInfoByUserAccount(userAccount);
userID = user2.getUserId();
//判断用户是否有会员
Userblock Userblock=userblockDao.getUserblockInfoByUserID(userID);
if(Userblock==null){
List<Blocktype> listBlockTypes = new ArrayList<Blocktype>();
try {
listBlockTypes = blockTypeDao.getAllBlockType();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
System.out.println("会员卡编号\t会员卡类型\t会员卡打折额度\t充值最大额度\t充值最小额度");
for (Blocktype blockType : listBlockTypes) {
System.out.println(blockType.getBlockTypeId() + "\t\t\t" + blockType.getBlockTypeName() + "\t\t" + blockType.getBlockdiscount() + "\t\t\t" + blockType.getMaxRecharge() + "\t\t" + blockType.getMinRecharge());
}
System.out.print("请输入充值金额(最低200):");
rechargeable = input.nextDouble();
if (rechargeable < 200) {
System.out.println("充值失败,金额最低200");
} else {
//根据充值金额 判断卡的类型
try {
blockType1 = blockTypeDao.getBlockTypeIDByPrice(rechargeable);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
System.out.println("恭喜您,你获得了:" + blockType1.getBlockTypeName());
blockTypeID = blockType1.getBlockTypeId();
System.out.println("工作人员编号\t工作人员姓名\t工作人员类型");
for (Staff staff : staffDao.selectAllStaff()) {
System.out.println(staff.getStaffId() + "\t\t" + staff.getStaffName() + "\t\t\t" + staff.getStaffTypeName());
}
System.out.print("请选择服务的员工编号:");
BlockStaffID = 0;
try {
BlockStaffID = input.nextInt();
} catch (Exception e) {
System.out.println("宝宝不要随便输入哦~");
}
//获取选择的工作人员的类型ID
int choiceStaffTypeID = 0;
try {
choiceStaffTypeID = staffDao.getStaffTypeIDByStaffID(BlockStaffID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
//判断是否有此员工
if (choiceStaffTypeID <= 0) {
System.out.println("选择失败,没有此员工");
} else {
//添加会员信息
int b = userblockDao.insertUserblock(new Userblock(0, BlockStaffID, rechargeable, "", blockTypeID, userID, "0"));
if (b > 0) {
System.out.println("用户会员添加成功");
//添加消费记录
int c = consumptionDao.insertConsumption(new Consumption(0, userID, 5, BlockStaffID, rechargeable, rechargeable, "0", "", "", "", "", ""));
if (c > 0) {
System.out.println("消费记录添加成功");
//修改用户余额
userYuanBalance=userDao.getUserBalanceByUserAccoount(userAccount);
//消费后的金额
userHouBalance=userYuanBalance+rechargeable;
System.out.println(userHouBalance);
//修改用户的金额
int a=userDao.updateUserbalanceByUserAccount(userAccount, userHouBalance);
if(a>0) {
System.out.println("开通会员成功,账户余额" + userHouBalance + "元");
}else {
System.out.println("开通会员失败");
}
} else {
System.out.println("消费记录添加失败");
}
} else {
System.out.println("用户会员添加失败");
}
}
}
}else {
System.out.println("用户"+user2.getUserName()+"已有会员卡");
}
}else {
System.out.println("没有找到此用户");
}
}
@Override
public void selectAllStaff() throws SQLException {
System.out.println("员工编号\t\t员工姓名\t员工类型");
for (Staff staff : staffDao.selectAllStaff()) {
System.out.println(staff.getStaffId()+"\t"+staff.getStaffName()+"\t"+staff.getStaffTypeName());
}
}
@Override
public void selectAllStaffType() throws SQLException {
System.out.println("员工类型编号\t员工类型名称");
for (Stafftype staffType : staffTypeDao.getAllStaffTypes()) {
System.out.println(staffType.getStaffTypeId()+"\t"+staffType.getStaffTypeName());
}
}
//查询所有用户消费记录
@Override
public void SelectAllUserConsumption() {
List<Consumption> listConsumption=new ArrayList<Consumption>();
try {
listConsumption=consumptionDao.getAllConsumption();
} catch (SQLException e) {
System.out.println("宝宝出错了哦,不好意思奥!");
e.printStackTrace();
}
System.out.println("消费记录编号\t消费用户\t消费项目\t服务员工\t金额流水\t\t账户余额\t消费时间");
for (Consumption consumption : listConsumption) {
System.out.println(consumption.getConsumptionId()+"\t\t\t"+consumption.getUserName()+"\t\t"+consumption.getProjectTypeName()+"\t"+consumption.getStaffName()+"\t\t"+consumption.getConsumptionBalance()+"\t"+consumption.getUserBalance()+"\t\t"+consumption.getConsumptionDatetime());
}
}
@Override
public void updateUserbalance() throws SQLException {
getAllUserInfo();
//用户原余额
double userYuanBalance=0;
//用户充值后余额
double userHouBanlance=0;
//充值金额
double chongZhi=0;
//用户对象
User user=null;
System.out.print("请输入用户账户:");
String userAccount=input.next();
//判断是否有此用户
try {
user=userDao.getUserInfoByUserAccount(userAccount);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
if(user!=null) {
System.out.print("请输入您要充值的金额:");
try {
chongZhi=input.nextDouble();
}catch (Exception e){
System.out.println("请输入正确金额");
}
//获取账户原余额
try {
userYuanBalance=userDao.getUserBalanceByUserAccoount(userAccount);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦");
}
//充值后余额
userHouBanlance=userYuanBalance+chongZhi;
//修改用户余额
int a=userDao.updateUserbalanceByUserAccount(userAccount,userHouBanlance);
if(a>0){
System.out.println("用户:"+user.getUserName()+"充值成功,请及时查看");
//增加消费记录
int b=consumptionDao.insertConsumption(new Consumption(0,user.getUserId(),8,1,chongZhi,userHouBanlance,"0","","","","",""));
if(b>0){
System.out.println("消费记录添加成功");
}else {
System.out.println("消费记录添加失败");
}
}else {
System.out.println("用户:"+user.getUserName()+"充值失败");
}
}
}
@Override
public void updateUserBlock() throws SQLException {
//流水
double LiuSHui=0;
//会员卡对象
Blocktype blocktype;
//会员卡编号
int BlockID=0;
//用户ID
int userID=0;
//判断用户是否有会员
Userblock Userblock= null;
//用户对象
User user=null;
//用户的会员卡ID
int userBlockID=0;
//升级用户会员等级
System.out.print("请输入用户账户:");
String userAccount=input.next();
//判断是否有此用户
try {
user=userDao.getUserInfoByUserAccount(userAccount);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
if(user!=null) {
//获取用户ID
User user2 = null;
try {
user2 = userDao.getUserInfoByUserAccount(userAccount);
} catch (SQLException e) {
throw new RuntimeException(e);
}
userID = user2.getUserId();
try {
Userblock = userblockDao.getUserblockInfoByUserID(userID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(Userblock!=null){
userBlockID=Userblock.getBlockTypeId();
//获取用户的会员卡等级
Blocktype blocktype1=blockTypeDao.getBlockTypeByblockTypeID(userBlockID);
//用户会员卡的最小值
Double userMin=blocktype1.getMinRecharge();
//显示用户所要修改的会员等级
List<Blocktype> listBlockTypes = new ArrayList<Blocktype>();
try {
listBlockTypes = blockTypeDao.getAllBlockType();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("宝宝出错了哦,不好意思奥!");
}
System.out.println("会员卡编号\t会员卡类型\t会员卡打折额度\t充值最大额度\t充值最小额度");
for (Blocktype blockType : listBlockTypes) {
System.out.println(blockType.getBlockTypeId() + "\t\t\t" + blockType.getBlockTypeName() + "\t\t" + blockType.getBlockdiscount() + "\t\t\t" + blockType.getMaxRecharge() + "\t\t" + blockType.getMinRecharge());
}
System.out.print("请选择要修改的会员卡编号(升级支付最小费用,降级不收费):");
BlockID=input.nextInt();
//判断是否选择了正确的
blocktype= blockTypeDao.getBlockTypeByblockTypeID(BlockID);
if(blocktype!=null) {
//判断会员等级是否低级
if(BlockID==blocktype1.getBlockTypeId()){
System.out.println("您已经是"+blocktype1.getBlockTypeName()+",无需升级");
}else {
if(userMin>blocktype.getMaxRecharge()){
System.out.println("你的会员卡"+blocktype1.getBlockTypeName()+"转换为"+blocktype.getBlockTypeName()+",高级转为低级,无需费用");
int a=userblockDao.updateUserblockTyleIDByuserAccount(BlockID,userID);
if(a>0){
System.out.println("修改成功");
}
//获取原余额
double userYuanBalance=userDao.getUserBalanceByUserAccoount(userAccount);
int c = consumptionDao.insertConsumption(new Consumption(0, userID, 6, 1, 0, userYuanBalance, "0", "", "", "", "", ""));
if(c>0){
System.out.println("消费记录增加成功");
}else {
System.out.println("消费记录增加失败");
}
}else{
System.out.println("你的会员卡"+blocktype1.getBlockTypeName()+"转换为"+blocktype.getBlockTypeName()+",低级卡转为高级卡,需要费用");
double min=blocktype.getMinRecharge();
System.out.println("支付费用"+min+"元");
int b=userblockDao.updateUserblockTyleIDByuserAccount(BlockID,userID);
if(b>0){
System.out.println("修改成功,您以成功升级为"+blocktype.getBlockTypeName());
//获取原余额
double userYuanBalance=userDao.getUserBalanceByUserAccoount(userAccount);
//消费后余额
double userHouBalance=userYuanBalance+min;
LiuSHui=0-min;
//增加消费记录
int c = consumptionDao.insertConsumption(new Consumption(0, userID, 6, 1, LiuSHui, userHouBalance, "0", "", "", "", "", ""));
if(c>0){
System.out.println("消费记录增加成功");
//修改用户的金额
int a=userDao.updateUserbalanceByUserAccount(userAccount, userHouBalance);
if(a>0){
System.out.println("金额已修改");
}else {
System.out.println("金额修改失败");
}
}else {
System.out.println("消费记录增加失败");
}
}
}
}
}else {
System.out.println("没有此类型的卡哦");
}
}
}else {
System.out.println("此用户不存在");
}
}
@Override
public void insertProject() throws SQLException {
Scanner input=new Scanner(System.in);;
System.out.print("请输入项目名称:");
String projectName=input.nextLine();
System.out.print("请输入项目价格:");
double projectPrice=input.nextDouble();
System.out.println("请选择项目的服务员工类型");
System.out.println("员工类型编号\t员工类型名称");
for (Stafftype staffType : staffTypeDao.getAllStaffTypes()) {
System.out.println(staffType.getStaffTypeId()+"\t"+staffType.getStaffTypeName());
}
System.out.print("请输入您选择的员工类型编号:");
int staffTypeID=input.nextInt();
//判断是否存在
Stafftype stafftype=staffTypeDao.getStaffTypeInfoByStaffTypeID(staffTypeID);
if(stafftype==null){
System.out.println("您选择的员工类型不存在哦");
}else {
try {
int a=projectTypeDao.insertProjectType(new Projecttype(0,projectName,projectPrice,staffTypeID,""));
if(a>0){
System.out.println("服务项目添加成功");
}else {
System.out.println("服务项目添加失败");
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("出错了哦,宝宝");
}
}
}
@Override
public void insertStaff() throws SQLException {
Scanner input=new Scanner(System.in);;
System.out.print("请输入员工姓名:");
String staffName=input.nextLine();
System.out.println("请选择员工类型");
System.out.println("员工类型编号\t员工类型名称");
for (Stafftype staffType : staffTypeDao.getAllStaffTypes()) {
System.out.println(staffType.getStaffTypeId()+"\t"+staffType.getStaffTypeName());
}
System.out.print("请输入您选择的员工类型编号:");
int staffTypeID=input.nextInt();
//判断是否存在
Stafftype stafftype=staffTypeDao.getStaffTypeInfoByStaffTypeID(staffTypeID);
if(stafftype==null){
System.out.println("您选择的员工类型不存在哦");
}else {
//添加员工
int a= staffDao.insertStaff(new Staff(0,staffName,staffTypeID,""));
if(a>0){
System.out.println("员工添加成功");
}else {
System.out.println("员工添加失败");
}
}
}
@Override
public void insertStaffType() {
Scanner input=new Scanner(System.in);;
System.out.print("请输入员工类型名称:");
String staffTypeName=input.next();
int a=0;
try {
a=staffTypeDao.insertStaffType(new Stafftype(0,staffTypeName));
} catch (SQLException e) {
e.printStackTrace();
System.out.println("出错了哦,宝宝");
}
if(a>0){
System.out.println("员工类型添加成功");
}else {
System.out.println("员工类型添加失败");
}
}
@Override
public void deleteStaff() throws SQLException {
System.out.println("工作人员编号\t工作人员姓名\t工作人员类型");
for (Staff staff : staffDao.selectAllStaff()) {
System.out.println(staff.getStaffId() + "\t\t" + staff.getStaffName() + "\t\t\t" + staff.getStaffTypeName());
}
System.out.print("请选择要删除的员工编号:");
int BlockStaffID = 0;
try {
BlockStaffID = input.nextInt();
} catch (Exception e) {
System.out.println("宝宝不要随便输入哦~");
}
//工作人员对象
Staff staff = null;
try {
staff = staffDao.selectStaffByStaffID(BlockStaffID);
} catch (SQLException e) {
throw new RuntimeException(e);
}
//判断是否有此员工
if (staff ==null) {
System.out.println("选择失败,没有此员工");
}else {
//修改要删除的员工 的名下业务 全为老板的业务
int a=consumptionDao.updateStaffIDbyStaffID(BlockStaffID);
if(a>0){
System.out.println("员工"+staff.getStaffName()+"名下消费记录业务已被删除");
}else {
System.out.println("删除员工"+staff.getStaffName()+"名下消费记录业务失败");
}
int b=userblockDao.updateuserBlockStaffIDbyuserBlockStaffID(BlockStaffID);
if(a>0){
System.out.println("员工"+staff.getStaffName()+"名下办卡业务已被删除");
}else {
System.out.println("删除员工"+staff.getStaffName()+"名下办卡业务失败");
}
//执行删除
int c=staffDao.deleteStaff(BlockStaffID);
if(c>0){
System.out.println("员工"+staff.getStaffName()+"成功删除");
}else {
System.out.println("员工"+staff.getStaffName()+"删除失败");
}
}
}
@Override
public boolean ManagerLogin(String ManagerUserName, String ManagerPassword) {
boolean result=false;
Manageruser manageruser=null;
try {
manageruser=managerUserDao.getmanagerUserInfoByManagerUserNameANDManagerPassword(ManagerUserName,ManagerPassword);
} catch (SQLException e) {
System.out.println("宝宝出错了哦,不好意思奥!");
e.printStackTrace();
}
if(manageruser!=null){
System.out.println("管理员登录成功"+manageruser.getManagerUserName());
result=true;
}else{
result=false;
}
return result;
}
}
通过在 Main 方法中调用各种服务类,来完成项目的各种需求
import cn.block.service.Impl.ServiceManagerServiceImpl;
import cn.block.service.Impl.clientManagerServiceImpl;
import cn.block.service.ServiceManagerService;
import cn.block.service.clientManagerService;
import java.sql.SQLException;
import java.util.Scanner;
public class MainBlock {
static String userAccount;
static String userPassword;
static String ManagerUserName;
static String ManagerPassword;
static clientManagerService cm=new clientManagerServiceImpl();
static ServiceManagerService sms=new ServiceManagerServiceImpl();
public static void main(String[] args) throws SQLException {
Scanner input=new Scanner(System.in);
System.out.println("1.客户登录");
System.out.println("2.管理员登录");
System.out.println("3.注册客户登录账户");
System.out.print("请选择登录身份:");
String choice=input.next();
switch (choice){
case "1":
System.out.print("请输入用户账户:");
userAccount=input.next();
System.out.print("请输入用户密码:");
userPassword=input.next();
//判断是否登录成功
boolean isUserLogin=cm.userLogin(userAccount,userPassword);
if(isUserLogin){
System.out.println("欢迎进入客户端服务:");
boolean tiao=false;
String iss="Y";
while(iss.equalsIgnoreCase("y")){
System.out.println("1.消费业务");
System.out.println("2.查看自己的消费记录");
System.out.println("3.查看自己的信息(余额)");
System.out.println("4.查看不同会员卡的信息");
System.out.println("5.退出登录");
System.out.print("请选择执行项目:");
String isUserChoice=input.next();
switch (isUserChoice){
case "1":
//执行消费业务
cm.userConsume(userAccount,userPassword);
break;
case "2":
//查看自己的消费记录
cm.getUserConsumption(userAccount,userPassword);
break;
case "3":
//查看自己信息(余额)
cm.getUserInfo(userAccount,userPassword);
break;
case "4":
//查看不同会员卡的信息
cm.getAllBlockType();
break;
case "5":
System.out.println("谢谢使用");
tiao=true;
break;
default:
System.out.println("没有此选项");
break;
}
if(tiao){
break;
}
System.out.println("是否继续(Y/N)");
iss=input.next();
}
}else {
System.out.println("用户名、密码错误或不存在,登录失败");
}
break;
case "2":
System.out.print("请输入管理员账户:");
ManagerUserName=input.next();
System.out.print("请输入管理员密码:");
ManagerPassword=input.next();
//判断是否登录成功
boolean isManagerLogin= sms.ManagerLogin(ManagerUserName,ManagerPassword);
if(isManagerLogin){
System.out.println("欢迎进入管理员服务:");
boolean tiao=false;
String iss="Y";
while(iss.equalsIgnoreCase("y")){
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("7.添加新会员用户");
System.out.println("8.查看所有员工");
System.out.println("9.查看所有员工类型");
System.out.println("10.添加员工");
System.out.println("11.添加员工类型");
System.out.println("12.删除员工");
System.out.println("退出登录");
System.out.print("请选择执行项目:");
String isUserChoice=input.next();
switch (isUserChoice){
case "1":
//查看所有用户信息
sms.getAllUserInfo();
break;
case "2":
//注册用户账户
sms.register();
break;
case "3":
//添加会员用户
sms.updateUserBlock();
break;
case "4":
//查看所有用户消费记录
sms.SelectAllUserConsumption();
break;
case "5":
//用户充值业务
sms.updateUserbalance();
break;
case "6":
//增加服务项目
sms.insertProject();
break;
case "7":
//添加新会员用户 setUserBlockByUserAccount
sms.setUserBlockByUserAccount();
break;
case "8":
//查看所有员工
sms.selectAllStaff();
break;
case "9":
//查看所有员工类型
sms.selectAllStaffType();
break;
case "10":
//添加员工
sms.insertStaff();
break;
case "11":
//添加员工类型
sms.insertStaffType();
break;
case "12":
//删除员工
sms.deleteStaff();
break;
default:
tiao=true;
System.out.println("没有此选项");
break;
}
if(tiao){
break;
}
System.out.println("是否继续(Y/N)");
iss=input.next();
}
}else {
System.out.println("用户名、密码错误或不存在,登录失败");
}
break;
case "3":
sms.register();
break;
default:
System.out.println("没有此选项");
break;
}
}
}
System.out.println("1.消费业务");
System.out.println("2.查看自己的消费记录");
System.out.println("3.查看自己的信息(余额)");
System.out.println("4.查看不同会员卡的信息");
System.out.println("5.退出登录");
System.out.print("请选择执行项目:");
String isUserChoice=input.next();
switch (isUserChoice){
case "1":
//执行消费业务
cm.userConsume(userAccount,userPassword);
break;
case "2":
//查看自己的消费记录
cm.getUserConsumption(userAccount,userPassword);
break;
case "3":
//查看自己信息(余额)
cm.getUserInfo(userAccount,userPassword);
break;
case "4":
//查看不同会员卡的信息
cm.getAllBlockType();
break;
case "5":
System.out.println("谢谢使用");
tiao=true;
break;
default:
System.out.println("没有此选项");
break;
}
if(tiao){
break;
}
System.out.println("是否继续(Y/N)");
iss=input.next();
}
}else {
System.out.println("用户名、密码错误或不存在,登录失败");
}
break;
case "2":
System.out.print("请输入管理员账户:");
ManagerUserName=input.next();
System.out.print("请输入管理员密码:");
ManagerPassword=input.next();
//判断是否登录成功
boolean isManagerLogin= sms.ManagerLogin(ManagerUserName,ManagerPassword);
if(isManagerLogin){
System.out.println("欢迎进入管理员服务:");
boolean tiao=false;
String iss="Y";
while(iss.equalsIgnoreCase("y")){
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("7.添加新会员用户");
System.out.println("8.查看所有员工");
System.out.println("9.查看所有员工类型");
System.out.println("10.添加员工");
System.out.println("11.添加员工类型");
System.out.println("12.删除员工");
System.out.println("退出登录");
System.out.print("请选择执行项目:");
String isUserChoice=input.next();
switch (isUserChoice){
case "1":
//查看所有用户信息
sms.getAllUserInfo();
break;
case "2":
//注册用户账户
sms.register();
break;
case "3":
//添加会员用户
sms.updateUserBlock();
break;
case "4":
//查看所有用户消费记录
sms.SelectAllUserConsumption();
break;
case "5":
//用户充值业务
sms.updateUserbalance();
break;
case "6":
//增加服务项目
sms.insertProject();
break;
case "7":
//添加新会员用户 setUserBlockByUserAccount
sms.setUserBlockByUserAccount();
break;
case "8":
//查看所有员工
sms.selectAllStaff();
break;
case "9":
//查看所有员工类型
sms.selectAllStaffType();
break;
case "10":
//添加员工
sms.insertStaff();
break;
case "11":
//添加员工类型
sms.insertStaffType();
break;
case "12":
//删除员工
sms.deleteStaff();
break;
default:
tiao=true;
System.out.println("没有此选项");
break;
}
if(tiao){
break;
}
System.out.println("是否继续(Y/N)");
iss=input.next();
}
}else {
System.out.println("用户名、密码错误或不存在,登录失败");
}
break;
case "3":
sms.register();
break;
default:
System.out.println("没有此选项");
break;
}
}
}