JavaEE知识体系

一、数据库

  1. MySql管理
  • 启动服务:net start mysql
  • 关闭服务:net stop mysql
  • 登录MySql:mysql -u用户名 -p密码
  1. SQL语句

2.1 DDL

2.1.1 操作database

-- 1.创建database
create database 数据库名;
-- 2.查看database
show databases;
show create database 数据库名;
-- 3.修改database
alter database 数据库名 character set 字符集
-- 4. 删除database
drop database 数据库名;
-- 5. 使用database
use 数据库名;
select database();

2.1.2 操作table

-- 1. 创建table
create table 表名称(
	字段名 类型 [约束],
    ...
    字段名 类型 [约束]
);
-- 2. 查看table
show tables;
show create table 表名称;
desc 表名称; -- 查看表结构
-- 3. 删除table
drop table 表名称;
-- 4. 修改table
-- 4.1 重命名表
rename table 表名称 to 新名称;
-- 4.2 添加字段
alter table 表名称 add 字段名 类型 [约束];
-- 4.3 修改字段类型
alter table 表名称 modify 字段名 新类型 [约束];
-- 4.4 修改字段名称
alter table 表名称 change 字段名 新字段名 类型 [约束];
-- 4.5 删除字符
alter table 表名称 drop 字段名;

2.2 DML

-- 1. 插入数据
insert into 表名称 (字段1, 字段2,...) values (值1,值2,...);
-- 2. 修改数据
update 表名称 set 字段1=值1, 字段2=值2,... where 条件;
-- 3. 删除数据
delete from 表名称 where 条件; 

2.3 DQL

2.3.1 单表查询

-- 1.简单查询
select * from 表名称;
select 字段1,字段2,... from 表名称;
select ifnull(字段,默认值) from 表名称;
select 字段1+字段2, 字段3+100,... from 表名称;
select 字段1 as 别名1, 字段2 别名2 from 表名称;

-- 2.条件查询:>,<,>=,<=,=,<>,like, between...and..., in(),   and, or, not
select * from emp where salary > 5000;
select * from emp where ename like '张%'; -- %表示任意个任意字符; _表示一个任意字符
select * from emp where salary between 2000 and 10000;
select * from emp where dept_id in (10, 30);

-- 3.排序查询:order by 排序字段 排序规则    desc降序,asc升序
select * from emp order by age desc;

-- 4.聚合函数:聚合函数会忽略null值
select count(*) from emp;
select sum(salary) from emp;
select avg(salary) from emp;
select max(salary) from emp;
select min(salary) from emp;

-- 5.分组查询:group by 分组字段 having 分组后过滤条件
select dept_id, count(*) from emp group by dept_id having count(*) > 10;

-- 6.分页查询:limit 起始索引, 查询数量
select * from emp limit 0, 5;

2.3.2 多表查询

-- 1.内连接查询:查询表之间必定有关联的数据
select * from emp e, dept d where e.dept_id = d.id;
select * from emp e inner join dept d on e.dept_id = d.id;

-- 2.外连接查询:查询一张表的全部数据,及另外一张表的关联数据
select * from emp e left join dept d on e.dept_id = d.id;
select * from emp e right join dept d on e.dept_id = d.id;

-- 3.子查询:是查询技巧没有固定语法,是查询嵌套
select * from emp where salary = (select max(salary) from emp);
select * from dept where id in (select dept_id from emp where salary > 10000);
select * from dept d, (select * from emp where salary > 10000) t where d.id = t.dept_id;

2.4 DCL

-- 1. 创建用户
create user 'tom'@'%' identified by 'tom';
-- 2. 用户授权
grant all on *.* to 'tom'@'%';
-- 3. 查看权限
show grants for 'tom'@'%';
-- 4. 取消授权
revoke delete on *.* from 'tom'@'%';
-- 5. 删除用户
drop user 'tom'@'%';
-- 6. 修改密码
set password for 'tom'@'%' = password('1234');

2.5 TCL

-- 1. 开启事务
start transaction;  -- 或者  set autocommit = 0;
-- 2. 执行多条DML语句
-- ......
-- 3. 关闭事务:提交事务
commit;
-- 3. 关闭事务:回滚事务
rollback;
  1. JDBC+预编译对象

    //1.注册驱动
    Class.forName(“com.mysql.jdbc.Driver”);
    //2.获取连接
    Connection conn = DriverManager.getConnection(“url”,“username”,“password”);
    //3.创建SQL执行平台
    PreparedStatement pstmt = conn.prepareStatement(“使用?处理过的sql语句”);
    pstmt.setXXX(参数序号, 参数值);
    //4.执行SQL语句 executeUpdate(), execute()
    ResultSet rs = pstmt.executeQuery();
    //5.处理结果
    while(rs.next()){
    XXX value = rs.getXXX(“字段名称”);
    }
    //6.释放资源
    rs.close();
    pstmt.close();
    conn.close();

  2. 连接池

4.1 c3p0连接池的使用

  1. 导入jar包:数据库驱动包,c3p0的jar包
  2. 提供配置文件:
    • 文件名称:c3p0-config.xml
    • 文件位置:类加载路径下(src下)
  3. 编写代码,使用连接池
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    Connection conn = dataSource.getConnectoin();
    //使用conn操作数据库
    conn.close();

4.2 druid连接池的使用

  1. 导入jar包:数据库驱动包,druid的jar包

  2. 提供配置文件

    • 文件名称:xxx.properties
    • 文件位置:建议放在类加载路径下
  3. 编写代码使用连接池
    DataSource dataSource = DruidDataSourceFactory.createDataSource(Properties对象);
    Connection conn = dataSource.getConnection();
    //使用conn操作数据库
    conn.close();

  4. JdbcTemplate

5.1 使用步骤

  1. 导入jar包:数据库驱动包,连接池的jar包,JdbcTemplate的jar包
  2. 提供连接池的配置文件,和工具类JdbcUtils(工具类里提供了获取连接池对象的方法)
  3. 编写代码,使用JdbcTemplate执行SQL语句
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());
    //使用JdbcTemplate的不同方法,执行不同SQL语句

5.2 常用方法

//1. 查询一个值,比如:查询表里的数量
Integer value = jdbcTemplate.queryForObject("sql", Integer.class, params);

//2. 查询多条数据,得到JavaBean的集合
List<JavaBean> list = jdbcTemplate.query("sql",new BeanPropertyRowMapper<>(JavaBean类名.class), params);

//3. 查询一条数据,得到一个JavaBean对象
JavaBean obj = jdbcTemplate.queryForObject("sql",new BeanPropertyRowMapper<>(JavaBean类名.class),params);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值