做java项目时遇到的一些问题和方法总结

1.正则表达式在Java和python等多项编程软件中都很有作用,但是我觉得不需要精通,至少要会做到能读懂和能写出一些基本的正则表达式为基础。
2.MySQL的基本操作,表的管理,DML,条件查询,模糊查询,排序查询,分组查询,多表查询,分页查询
3.Java数据库连接:JDBC(Java Database Connectivity)
4.根据页面需求开发实体类
5.设置测试类
6.根据代码的冗余程度进行必要的开发和重构

正则表达式

Java中的 \d 要写成\d,在eclipse中,两个\ \代表一个\。
例如:表示邮箱 xxx@xxx.xxx 在Java中正则表达式就写为 ^\w+@\w+\.\w+$ 。 ^开头$结尾
想表示身份证 :^\d{17}(\d|x) $
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

MySQL的基本操作

1、表的管理

  • 创建数据库 create database test01;

  • 查看数据库 show databases;

  • 选择数据库 use test01;

  • 查询端口号:show global variables like ‘port’; 3336

  • mysql常用数据类型
    int,整型,id int(4)
    double,浮点,price double(8,2)
    char,定长字符 idcard char(18)
    varchar,可变长字符 title varchar(20)
    date 日期类型
    timestamp 时间戳类型

  • 创建表
    创建学生表:id,name,age,sex,idcard,score,birth,cteated
    create table t_student1(
    id int(4),
    name varchar(10),
    age int(2),
    sex char(1),
    idcard char(18),
    score double(4,1),
    birth date,
    created timestamp
    );

  • mysql数据库约束
    主键约束:字段的值非空唯一,一个表中一个主键
    primary key, 主键约束约束类型
    auto_increment, 自动增长选项,自动生成唯一主键字段值
    create table t_student2(
    id int(4) primary key auto_increment,
    name varchar(10),
    age int(2),
    sex char(1),
    idcard char(18),
    score double(4,1),
    birth date,
    created timestamp
    );
    复合主键:
    create table t_stu_tea(
    stuid int(4),
    teaid int(4),
    created timestamp,
    primary key(stuid,teaid)
    );
    外键约束:限制表中字段的值必须引用于其他表中或本表中主键字段的值
    foreign key,references

    create table t_student3(
       id int(4) primary key auto_increment,
       name varchar(10),
       age int(2),
       sex char(1),
       idcard char(18),
       score double(4,1),
       birth date,
       created timestamp
    );
    create table t_teacher3(
       id int(4) primary key auto_increment,
       name varchar(10),
       sal double(8,2)
    );
    添加外键约束语法1、
    create table t_stu_tea3(
       stuid int(4) references t_student3(id),
       teaid int(4) references t_teacher3(id),
       created timestamp,
       primary key(stuid,teaid)
    );	
    添加外键约束语法2、
    create table t_stu_tea4(
       stuid int(4),
       teaid int(4),
       created timestamp,
       primary key(stuid,teaid),
       foreign key(stuid) references t_student3(id),
       foreign key(teaid) references t_teacher3(id)
    );
    通过修改表的语句添加外键、
    create table t_stu_tea5(
       stuid int(4),
       teaid int(4),
       created timestamp,
       primary key(stuid,teaid)
    );
    create table t_student5(
       id int(4) primary key auto_increment,
       name varchar(10),
       age int(2),
       sex char(1),
       idcard char(18),
       score double(4,1),
       birth date,
       created timestamp
    );
    create table t_teacher5(
       id int(4) primary key auto_increment,
       name varchar(10),
       sal double(8,2)
    );
    添加外键:
    alter table t_stu_tea5 add foreign key(stuid) references t_student5(id);
    alter table t_stu_tea5 add foreign key(teaid) references t_teacher5(id);     
    

    非空约束:not null
    唯一性约束,unique
    create table t_student6(
    id int(4) primary key auto_increment,
    name varchar(10),
    age int(2),
    sex char(1),
    idcard char(18) not null unique,
    score double(4,1),
    birth date,
    created timestamp
    );

2、DML(CRUD)
insert update delete select
职员表(empno,ename,job,sal,hiredate,mgr,deptno)
部门表(deptno,dname,loc)
drop table if exists emp;
create table emp(
empno int(4) primary key auto_increment,
ename varchar(10),
job varchar(10),
sal double(8,2),
hiredate date,
mgr int(4),
deptno int(2)
);
create table dept(
deptno int(2) primary key auto_increment,
dname varchar(20),
loc varchar(30)
);
添加外键
alter table emp add foreign key(deptno) references dept(deptno);
alter table emp add foreign key(mgr) references emp(empno);
添加字段:
alter table emp add column comm double(7,2) after mgr;
查看表:show tables;
查看表结构:desc emp;
删除表:drop table student1;
如果表存在,先删除再创建
drop table if exists t_stu_tea;
create table t_stu_tea(
stuid int(4),
teaid int(4),
created timestamp,
primary key(stuid,teaid)
);

insert语句:
需求:创建3个部门
insert into dept values(10,‘研发部’,‘西湖大学教学楼301’);
insert into dept(deptno,dname,loc) values(20,‘教学部’,‘西湖大学实训楼405’);
insert into dept(deptno,dname,loc) values(30,‘财务部’,‘西湖大学主楼201’);
select * from dept;

需求:分别向3个部门中添加5个员工,每个部门有一个经理,再添加一个总裁
研发部:
insert into emp values(7000,‘马云’,‘总裁’,8000,‘2001-9-1’,null,null,10);
insert into emp values(7089,‘一一’,‘经理’,28000,‘2019-9-1’,7000,2000,10);
insert into emp values(7090,‘二二’,‘程序员’,22000,‘2020-8-13’,7089,1000,10);
insert into emp values(7091,‘三三’,‘程序员’,23000,‘2018-5-16’,7089,0,10);
insert into emp values(7092,‘四四’,‘程序员’,21000,‘2019-10-21’,7089,200,10);
insert into emp values(7093,‘五五’,‘程序员’,20500,‘2020-8-13’,7089,null,10);

教学部:
insert into emp values(7030,‘六六’,‘教学总监’,23600,‘2016-6-3’,7000,1500,20);
insert into emp values(7031,‘七七’,‘讲师’,23000,‘2016-6-8’,7030,500,20);
insert into emp values(7032,‘八八’,‘讲师’,24000,‘2017-7-5’,7030,300,20);
insert into emp values(7033,‘九九’,‘讲师’,23000,‘2019-7-15’,7030,800,20);
insert into emp values(7034,‘十十’,‘讲师’,25000,‘2017-7-5’,7030,null,20);

财务部:
insert into emp values(7010,‘张三’,‘经理’,15600,‘2002-1-1’,7000,2000,30);
insert into emp values(7011,‘李四’,‘出纳’,7600,‘2003-3-5’,7010,null,30);
insert into emp values(7018,‘王二麻子’,‘出纳’,8000,‘2003-4-5’,7010,null,30);
insert into emp values(7019,‘刘五’,‘会计’,6600,‘2003-3-5’,7010,null,30);
insert into emp values(7012,‘王七’,‘会计’,6900,‘2003-4-5’,7010,null,30);

update语句:
update emp set ename=‘鼠标’ where empno=7019;
update emp set sal=sal+1000 where sal<28000;

delete语句:
delete from emp where empno=1000;
删除所有数据
delete from emp;

3、查询
select语句

  • 基本查询
    需求:列出所有员工信息
    select empno,ename,job,sal,hiredate,mgr,comm,deptno from emp;
    select * from emp;
    需求:列出员工的姓名,薪水,职位,入职日期
    select ename,sal,job,hiredate from emp;

  • 条件查询
    需求:列出工资大于25000的员工姓名,工资,职位
    select ename,sal,job from emp where sal>25000;

    范围:between…and,not between…and
    需求:列出2010年到2019年入职的员工姓名,职位,入职日期
    select ename,job,hiredate from emp
    where hiredate between ‘2010-1-1’ and ‘2019-12-31’;
    需求:列出2010年之前和2018年之后入职的员工信息
    select empno,ename,job,sal,hiredate,mgr,comm,deptno from emp
    where hiredate not between ‘2010-1-1’ and ‘2017-12-31’;

    and、or用法
    需求:列出10号部门,工资大于24000的员工姓名,薪水,入职日期
    select ename,sal,hiredate from emp where deptno=10 and sal>24000;
    需求:列出工资低于20000,高于25000的员工的姓名、职位,薪水,入职日期
    select ename,job,sal,hiredate from emp
    where sal<20000 or sal>25000;

    in,not in
    需求:列出10、20部门中员工的姓名,薪水,职位,入职日期,部门号
    select ename,sal,job,hiredate,deptno from emp
    where deptno in(10,20);

  • 模糊查询
    like ,
    %,任意位数的任意字符
    _,一位任意字符
    需求:列出姓张的员工的姓名,职位,薪水,入职日期,奖金
    select ename,job,sal,hiredatre,sal from emp
    where ename like ‘张%’;
    需求:列出3月份入职的员工姓名,薪水,入职日期
    select ename,sal,hiredate from emp where hiredate like ‘_____03%’;
    需求:列出姓张或名字中有琦的员工的姓名,薪水,职位,入职日期,部门号。
    select ename,sal,job,hiredate,deptno from emp
    where ename like ‘张%’ or ename like ‘%琦%’;

  • 排序查询
    order by 参考字段
    asc 升序
    desc 降序
    需求:列出员工的姓名,薪水,职位,入职日期,根据薪水降序排序。
    select ename,sal,job,hiredate from emp order by sal desc;
    需求:列出员工姓名,薪水,职位,入职日期,根据入职日期升序排序。
    select ename,sal,job,hiredate from emp order by hiredate asc;
    需求:列出员工姓名,薪水,职位,入职日期,根据入职日期升序,薪水降序排序。
    select ename,sal,job,hiredate from emp order by hiredate asc ,sal desc;

  • 分组查询
    group by,指定分组字段
    组函数(聚合函数)
    count(),统计记录数
    sum(),max(),min(),avg()
    需求:列出每个部门薪水的总和
    select deptno, sum(sal) from emp group by deptno;
    需求:列出每个部门平均薪水
    需求:列出每个部门最高薪水
    需求:列出每个部门最低薪水
    select deptno,sum(sal) as 薪水总和,avg(sal) as 平均薪水,max(sal) as 最高薪水,min(sal) as 最低薪水
    from emp group by deptno;
    注意:分组查询只查询通过组函数运算的结果和分组字段
    count(),用来统计记录数
    需求:统计每个部门的员工数量
    select deptno, count() from emp group by deptno;
    需求:统计公司中员工的数量
    select count(
    ) from emp;
    having,对分组后的结果再过滤
    需求:列出工资总和大于10万的部门号
    select deptno from emp group by deptno having sum(sal)>100000;
    需求:列出公司中所有的职位
    select job from emp; --结果集中会有重复
    select distinct job from emp; --distinct,去重
    select job from emp group by job;–分组方式去重

  • 多表查询(联合查询)

    需求:列出员工姓名,职位,入职日期,部门名,部门地址
    select ename,job,hiredate,dname,loc from emp,dept;
    笛卡尔乘积现象
    联合查询通过连接条件避免出现笛卡尔乘积现象
    select ename,job,hiredate,dname,loc from emp,dept where emp.deptno=dept.deptno;
    多表查询语法:
    等值查询:
    select ename,job,hiredate,dname,loc from emp,dept where emp.deptno=dept.deptno;

    内连接查询:(功能等同于等值查询)
    select ename,job,hiredate,dname,loc from emp e inner join dept d on e.deptno=d.deptno;

    左外连接查询:左表中所有的数据,右表中满足连接条件的数据
    select ename,job,hiredate,dname,loc from emp e left outer join dept d on e.deptno=d.deptno;

    实验:添加数据:
    insert into emp(empno,ename) values(1000,‘哈哈哈’);
    insert into dept values(40,‘业务部’,‘山西农大2-201’);

    需求:列出员工姓名,职位,入职日期,部门名,部门地址

    内连接,会丢失没有部门的员工信息
    select ename,job,hiredate,dname,loc from emp e inner join dept d on e.deptno=d.deptno;
    select ename,job,hiredate,dname,loc from emp e join dept d on e.deptno=d.deptno;

    左外连接,将员工表设置为左表,员工信息不会丢失
    select ename,job,hiredate,dname,loc from emp e left outer join dept d on e.deptno=d.deptno;
    select ename,job,hiredate,dname,loc from emp e left join dept d on e.deptno=d.deptno;

    右外连接:右表中所有的数据,左表中满足连接条件的数据
    select ename,job,hiredate,dname,loc from dept d right outer join emp e on e.deptno=d.deptno;
    select ename,job,hiredate,dname,loc from dept d right join emp e on e.deptno=d.deptno;

  • mysql分页查询
    limit
    int pageSize = 4;// 每页显示的记录数
    int page = 2;// 当前页
    int begin = (page-1)*pageSize;
    select * from emp limit 4,4;

JDBC

1、什么是jdbc
Java语言与数据库交互的技术
JDBC是Java程序与关系型数据库软件交互的标准,提供了统一的操作界面
JDBC是一组接口,制定了Java程序与各种数据库软件交互的统一API

数据库软件产品Oracle DB2 MYSQL SQL Server

JDBC指定的标准,程序员根据这套标准,编写JAVA程序
软件运行期间,Java程序调用的是数据库厂商提供的具体API实现

JDBC程序--------一组类(驱动程序)---------Oracle数据
JDBC程序--------一组类(驱动程序)---------DB2数据
JDBC程序--------一组类(驱动程序)---------mysql数据

Java程序员学习JDBC,实际即是学习JDBC提供的一组接口
(设置驱动程序)在这里插入图片描述

2.JDBC的编码步骤

  1. 加载驱动程序
  2. 创建java与数据库的连接
  3. 发送SQL语句
  4. 如果发送查询语句,需要处理结果集
  5. 关闭连接(增删改) 查询有稍许的区别

步骤如图
查询: 查询这里用ResultSet executeQuery来发送select语句在这里插入图片描述

建立连接(调用程序就可以连接,两个人打电话的过程

3.CRUD基本操作步骤
4.JDBC工具类开发
5.PreparedStatement 预处理语句对象 是Statement接口的子接口
1). *可以避免SQL注入
sql注入:由于用户的输入改变程序中sql语句的本意
*增强sql语句在程序中的可读性
*提高JDBC中,同一条SQL语句(需要装载参数的SQL语句)多次执行效率
2).用法
*sql语句中拼接的Java变量,使用?作为占位符
DELETE FROM t_user WHERE id=?
*实例化:PreparedStatement pstmt =conn.prepareStatement(sql);
*装载占位符(?)值
setXxx(int index,Xxx)
占位符索引从1开始,第一个问号占位符值是1
*执行SQL语句语法
int executeUpdate();执行insert,update,delete语句
ResultSet executeQuery();执行select语句

开发实体类

实体类的开发需要有

  • 1.一组private属性
  • 2.一组private属性所对应的setter和getter
  • 3.一组构造函数
  • 4.重写equals(),hashCode(),toString()方法
  • 5.实现Serializable接口,能够被序列化

设置测试类

创建测试文件夹在工程上新建,source Floder
单元测试的是 右键包 新建other test即可看到
在这里插入图片描述

代码冗余而进行的必要重构操作

由于包含增删改方法 ,需要多次写所以 我们对它进行重构,
对比增删改查我们可以看到他们都需要 定义链接 获取连接 写sql
赋值 拿到易初里语句对象 ,它们就只有sql语句不一样往里面设置的
参数不同(问号不一样),所以我们把共同的部分抽取出来作为一个
做到支持项目中所有的增删改,增删改都属于一种修改,所以就叫
个update
传不同的sql, 问号不一样不同的参数,不确定的参数用…,
由于类型也不确定所以用Object(意味所有类型),然后调用的时候只
用传不同的sql,不同的?就可以了统一处理。
连接JDBCUtil,抛异常,关连接
PreparedS….传sql,传几个参数就相当于有几个问号。
由于set的类型也不同所以用setObject,,paramenters[i]
i从1开始逐渐累加,将问号取完为止,
然后调用执行方法executeUpdate()进行执行。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值