概述
1.封装已有的数据库连接
2.JDBC事务
3.三层架构
4.数据库连接池
Druid
5.DButils
封装已有的数据库连接
因为在以前每次操作数据库时都需要写连接数据库与关闭资源的代码
导致代码冗余,所以我们将其封装,使其便于使用,并降低代码的冗余
封装代码如下:
package demo;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class MyDBUtils {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
//创建Map,Properties加载文件中的数据
Properties properties = new Properties();
//获取配置信息文件的输入流
InputStream is = MyDBUtils.class.getResourceAsStream("MyDBInfo.properties");
try {
//加载输入流
properties.load(is);
//获取配置文件中url对应的值
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
Connection connection = DriverManager.getConnection(url, username, password);
return connection;
} catch (SQLException e) {
e.printStackTrace();
System.out.println("配置错误");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
try {
if (resultSet != null && !resultSet.isClosed()) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null && !statement.isClosed()) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null && connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
对应配置文件内容如下
配置文件名: MyDBInfo.properties
配置文件位置: 与MyDBUtils在同一路径(同一个文件夹)下
url = jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf-8
username = root
password = 123456
使用封装类示例: 查询全部用户信息
package demo;
import java.sql.*;
public class Demo01 {
public static void main(String[] args) throws SQLException {
Connection connection = MyDBUtils.getConnection();
String sql = "select * from myusers";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int s_id = resultSet.getInt("s_id");
String s_name = resultSet.getString("s_name");
String s_username = resultSet.getString("s_username");
String s_password = resultSet.getString("s_password");
String s_sex = resultSet.getString("s_sex");
int s_age = resultSet.getInt("s_age");
User user = new User(s_id, s_name, s_username, s_password, s_sex, s_age);
System.out.println(user);
}
MyDBUtils.close(resultSet, statement, connection);
}
}
JDBC处理事务
方法:
void setAutoCommit(boolean autoCommit)
作用:设置自动提交
参数:是否自动提交,默认为true,需要关闭自动提交将参数设为false
void commit()
作用:提交
void rollback()
作用:回滚
注意:以上方法由Connection(数据库连接对象提供),除提交或回滚外,当连接关闭后,其中的事物也将被销毁
示例: 转账
package demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo02 {
public static void main(String[] args) {
//转账业务
//获取连接
Connection connection = MyDBUtils.getConnection();
try {
//1.关闭自动提交
connection.setAutoCommit(false);
//2.张三金额减少
String sql = "update myusers set s_money = s_money - ? where s_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setDouble(1, 500);
statement.setInt(2, 1);
statement.executeUpdate();
//模拟事务中出现异常
//int num = 10 / 0;
//3.李四金额增多
String sql0 = "update myusers set s_money = s_money + ? where s_id = ?";
PreparedStatement statement0 = connection.prepareStatement(sql0);
statement0.setDouble(1, 500);
statement0.setInt(2, 2);
statement0.executeUpdate();
//提交
connection.commit();
} catch (Exception e) {
//e.printStackTrace();
try {
//出现异常回滚
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
} finally {
try {
if (connection != null && !connection.isClosed()) {
connection.setAutoCommit(true);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
三层架构
分包
controller: 控制层
service: 业务处理
dao: 操作数据库
bean: 对应实体
utils: 工具类
数据库连接池
作用: 存储与管理数据库连接,帮助我们创建,销毁复用数据库连接
因为自己封装数据库连接有以下弊端:
1.优化不足
2.复用不足
3.难度较大
所以使用市面上流行的数据库连接池
如: c3p0,Druid(德鲁伊),Spring自带的
综合能力最好的就是Druid
性能上最好的是Spring自带的
Druid的使用
1.下载Druid的jar包
2.导入jar包
3.编写代码
DButils的使用
下载jar包 -> 导包 -> 编写代码
核心类:
QueryRunner
方法:
query:
作用:执行DQL语句,类似于executeQuery方法
update:
作用:执行DML语句,类似于executeUpdate方法
ResultSetHandler:接口
子类:
BeanHandler:返回一个对象
注意:
1.bean类必须提供无参构造函数,如果没有无参构造会报错
2.bean类的属性名要与查询的结果的列名一致,如果名称不一致可能会取不到值
BeanListHandler:返回一个集合
注意:
1.bean类必须提供无参构造函数,如果没有无参构造会报错
2.bean类的属性名要与查询的结果的列名一致,如果名称不一致可能会取不到值
ScalarHandler:返回一个值