JDBC
JDBC –>Java Database Connectivity
用于Java和数据库连接 实现对具体数据库的操作
1.注册驱动 2.获取连接 3.获取结果集 4.处理结果集 5.关闭连接
在配置文件写需要的 用户名 密码 URL 驱动(ClassDriver)
1 | /**封装JDBCUtil*/ |
2 | public class JDBCUtil{ |
3 | //配置文件只需加载一次 使用静态代码块 |
4 | static String name; |
5 | static String password; |
6 | static String url; |
7 | static String ClassDriver; |
8 | static{ |
9 | try{ |
10 | Proreties pro = new Properties(); |
11 | pro.load(new InputStream("src\\JDBC.properties")) |
12 | name = pro.getProperty("name"); |
13 | password = pro.getProperty("password"); |
14 | url = pro.getProperty("url"); |
15 | classDriver = pro.getProperty("ClassDriver"); |
16 | //注册驱动 |
17 | Class.forName(ClassDriver); |
18 | |
19 | }catch(Exception e){ |
20 | throw new RuntimeException(e); |
21 | } |
22 | } |
23 | public static Connection getConnection(){ |
24 | try{ |
25 | return DriverManager.getConnecton(url,name,password); |
26 | }catch(Exception e){ |
27 | throw new RuntimeException(e); |
28 | } |
29 | } |
30 | public static void(ResultSet set,Statement state,Connection con){ |
31 | try{ |
32 | if(set != null){ |
33 | set.close(); |
34 | } |
35 | if(state != null){ |
36 | set.close(); |
37 | } |
38 | if(con != null){ |
39 | con.close(); |
40 | } |
41 | }catch(Exception e){ |
42 | throw new Runtime(e); |
43 | } |
44 | } |
45 | } |
事务
事务(transaction)四大特性(ACID)
原子性(atomicity) :要么都执行 要么都回滚
一致性(consistency):保证数据的状态操作前和操作后保持一致
隔离性(isolation):多个事务同时操作同一个数据库时,一个事务的执行不受另一个事务的干扰
持久性(durability):一个事务一旦提交,则数据持久到本地,除非另一个事务对其进行操作
事务的体现
例如转账 如果期间出现异常的情况
使用事务的步骤 注意:连接要是同一个
try{1.开启事务 con.setAotoCommit(false);
2.执行sql语句
3.执行sql语句没有异常commit
}catch(Exception e){
4.执行sql语句有异常rollback
}finally{
5.关闭连接
}
批量处理
批量(Batch)
和preparedStatement一起使用 减少运行和运行次数
添加sql语句到批量处理包中addBatch()
执行批量处理包中的sql语句executeBatch()
清空批量处理包中的sql语句clearBatch()
使用数据库连接池
提高效率 连接复用
常用的 DBCP/C3P0/Druid
记使用开源框架步骤
1.找jar包 2.看帮助文档(Quick Start) 3.调用方法使用
1 | /**使用数据库理解池获取连接的工具类*/ |
2 | public class JDBCUtilsByDurid{ |
3 | static DataSource ds = null; |
4 | static{ |
5 | try{ |
6 | Properties pro = new Properties(); |
7 | pro.load(new FileInputStream("src\\jdbc.properties")); |
8 | ds = DuridDataSourceFactory.createDataSource(pro); |
9 | }catch(Exception e){ |
10 | throw new RuntimeException(e); |
11 | } |
12 | |
13 | } |
14 | public static Connection getConnection(){ |
15 | try{ |
16 | return ds.getConnection(); |
17 | }catch(Exception e){ |
18 | throw new RuntimeException(e); |
19 | } |
20 | } |
21 | public static void(ResultSet set,Statement statement,Connection con){ |
22 | try{ |
23 | if(set != null){ |
24 | set.close(); |
25 | } |
26 | if(statement != null){ |
27 | statement.close(); |
28 | } |
29 | if(con != null){ |
30 | con.close(); |
31 | } |
32 | }catch(Exception e){ |
33 | throw new RuntimeException(e); |
34 | } |
35 | } |
36 | } |
DBUtils
1.导包 2.看文档 3.调方法
QueryRunner类
update(con,sql,parm)参数分别是 连接/sql语句/占位符 执行增删改操作
query(con,sql,ResultSetHandler
ResultSetHandler接口
BeanHandler 将结果集的第一行封装成一个对象(单个类) new BeanHandler<>(xx.class)
BeanListHandler 将结果集的所有行,封装成集合返回 new BeanListHandler<>(xx.class)
SaclarHander 将结果集的第一行第一列,单条数据返回 new ScalarHanler()
Spring 的JDBCTemplate
导包 创建JDBCTemplate的对象 传入连接
使用方法update进行增删改
使用方法query进行查找
query(sql,new BeanPropertyRowMapper<类型>(类型).class)
把查询的结果封装为对象装入list集合返回
queryObject()用于聚合函数(count)查询 将结果封装为对象
DAO
Data Access Object 数据访问对象