数据库-JDBC
JDBC 规范定义接口,具体的实现由各大数据库厂商来实现。
JDBC 是 Java 访问数据库的标准规范,真正怎么操作数据库还需要具体的实现类,也就是数据库驱动。每个数据库厂商根据自家数据库的通信格式编写好自己数据库的驱动。所以我们只需要会调用 JDBC 接口中的方法即可,数据库驱动由数据库厂商提供。
使用 JDBC 的好处:
- 程序员如果要开发访问数据库的程序,只需要会调用 JDBC 接口中的方法即可,不用关注类是如何实现的
- 使用同一套 Java 代码,进行少量的修改就可以访问其他 JDBC 支持的数据库
使用 JDBC 开发使用到的包:
# JDBC 的核心 API
# 加载和注册驱动
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException {
// 抛出类找不到的异常,注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
}
}
2
3
4
5
6
7
8
com.mysql.jdbc.Driver 源代码:
// Driver 接口,所有数据库厂商必须实现的接口,表示这是一个驱动类
public class Driver implements java.sql.Driver {
public Driver() throws SQLException {
}
static {
try {
DriverManager.registerDriver(new Driver()); // 注册数据库驱动
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
注:从 JDBC3 开始,目前已经普遍使用的版本。可以不用注册驱动而直接使用。Class.forName 这句话可以省略。
# DriverManager 类
# DriverManager 作用
管理和注册驱动
创建数据库的连接
# 类中的方法
# 使用 JDBC 连接数据库的四个参数
# 连接数据库的 URL 地址格式
协议名:子协议:// 服务器名或 IP 地址:端口号 / 数据库名?参数 = 参数值
URL 用于标识数据库的位置,程序员通过 URL 地址告诉 JDBC 程序连接那个数据库,URL 的写法为:
# 乱码的处理
如果数据库出现乱码,可以指定参数:?characterEncoding=utf8,表示让数据库以 UTF-8 编码来处理数据。
jdbc:mysql://localhost:3306/ 数据库?characterEncoding=utf8
# 案例
得到 MySQL
- 使用用户名、密码、URL 得到连接对象
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 得到连接对象
*/
public class Demo2 {
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/day24";
//1) 使用用户名、密码、URL 得到连接对象
Connection connection = DriverManager.getConnection(url, "root", "root");
//com.mysql.jdbc.JDBC4Connection@68de145
System.out.println(connection);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 使用属性文件和 url 得到连接对象
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class Demo3 {
public static void main(String[] args) throws SQLException {
//url 连接字符串
String url = "jdbc:mysql://localhost:3306/day24";
// 属性对象
Properties info = new Properties();
// 把用户名和密码放在 info 对象中
info.setProperty("user","root");
info.setProperty("password","root");
Connection connection = DriverManager.getConnection(url, info);
//com.mysql.jdbc.JDBC4Connection@68de145
System.out.println(connection);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Connection 接口
作用
Connection 接口,具体的实现类由数据库的厂商实现,代表一个连接对象。
Connection 方法
# Statement 接口
# JDBC 访问数据库的步骤
注册和加载驱动(可以省略)
获取连接
Connection 获取 Statement 对象
使用 Statement 对象执行 SQL 语句
返回结果集
释放资源
# Statement 作用
代表一条语句对象,用于发送 SQL 语句给服务器,用于执行静态 SQL 语句并返回它所生成结果的对象
# Statement 中的方法
# 释放资源
需要释放的对象:ResultSet 结果集,Statement 语句,Connection 连接
释放原则:先开的后关,后开的先关。ResultSet Statement Connection
放在哪个代码块中:finally 块
# 执行 DDL 操作
需求
- 使用 JDBC 在 MySQL 的数据库中创建一张学生表
代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 创建一张学生表
*/
public class Demo4DDL {
public static void main(String[] args) {
//1. 创建连接
Connection conn = null;
Statement statement = null;
try {
conn = DriverManager.getConnection("jdbc:mysql:///day24", "root", "root");
//2. 通过连接对象得到语句对象
statement = conn.createStatement();
//3. 通过语句对象发送 SQL 语句给服务器
//4. 执行 SQL
statement.executeUpdate("create table student (id int PRIMARY key auto_increment, name varchar(20) not null, gender boolean, birthday date)");
//5. 返回影响行数 (DDL 没有返回值)
System.out.println("创建表成功");
} catch (SQLException e) {
e.printStackTrace();
}
//6. 释放资源
finally {
// 关闭之前要先判断
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 执行 DML 操作
需求:
- 向学生表中添加 4 条记录,主键是自动增长
步骤:
- 创建连接对象
- 创建 Statement 语句对象
- 执行 SQL 语句:executeUpdate(sql)
- 返回影响的行数
- 释放资源
代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 向学生表中添加 4 条记录,主键是自动增长
*/
public class Demo5DML {
public static void main(String[] args) throws SQLException {
// 1) 创建连接对象
Connection connection = DriverManager.getConnection("jdbc:mysql:///day24", "root","root");
// 2) 创建 Statement 语句对象
Statement statement = connection.createStatement();
// 3) 执行 SQL 语句:executeUpdate(sql)
int count = 0;
// 4) 返回影响的行数
count += statement.executeUpdate("insert into student values(null, '孙悟空', 1, '1993-03-24')");
count += statement.executeUpdate("insert into student values(null, '白骨精', 0, '1995-03-24')");
count += statement.executeUpdate("insert into student values(null, '猪八戒', 1, '1903-03-24')");
count += statement.executeUpdate("insert into student values(null, '嫦娥', 0, '1993-03-11')");
System.out.println("插入了" + count + "条记录");
// 5) 释放资源
statement.close();
connection.close();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 执行 DQL 操作
ResultSet 接口:
作用:
- 封装数据库查询的结果集,对结果集进行遍历,取出每一条记录
# 常用数据类型转换表
java.sql.Date、Time、Timestamp(时间戳),三个共同父类是:java.util.Date
# 案例
需求
- 确保数据库中有 3 条以上的记录,查询所有的学员信息
步骤
- 得到连接对象
- 得到语句对象
- 执行 SQL 语句得到结果集 ResultSet 对象
- 循环遍历取出每一条记录
- 输出的控制台上
- 释放资源
结果
import java.sql.*;
/**
* 查询所有的学生信息
*/
public class Demo6DQL {
public static void main(String[] args) throws SQLException {
//1) 得到连接对象
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/day24","root","root");
//2) 得到语句对象
Statement statement = connection.createStatement();
//3) 执行 SQL 语句得到结果集 ResultSet 对象
ResultSet rs = statement.executeQuery("select * from student");
//4) 循环遍历取出每一条记录
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
boolean gender = rs.getBoolean("gender");
Date birthday = rs.getDate("birthday");
//5) 输出的控制台上
System.out.println("编号:" + id + ", 姓名:" + name + ", 性别:" + gender + ", 生日:" +birthday);
}
//6) 释放资源
rs.close();
statement.close();
connection.close();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 注意事项
如果光标在第一行之前,使用 rs.getXX() 获取列值,报错:Before start of result set
如果光标在最后一行之后,使用 rs.getXX() 获取列值,报错:After end of result set
使用完毕以后要关闭结果集 ResultSet,再关闭 Statement,再关闭 Connection
# 数据库工具类 JdbcUtils
什么时候自己创建工具类?
- 如果一个功能经常要用到,我们建议把这个功能做成一个工具类,可以在不同的地方重用。
需求
上面写的代码中出现了很多重复的代码,可以把这些公共代码抽取出来。
创建类 JdbcUtil 包含 3 个方法:
可以把几个字符串定义成常量:用户名,密码,URL,驱动类
得到数据库的连接:getConnection()
关闭所有打开的资源:
- close(Connection conn, Statement stmt),close(Connection conn, Statement stmt, ResultSet rs)
import java.sql.*;
/**
* 访问数据库的工具类
*/
public class JdbcUtils {
// 可以把几个字符串定义成常量:用户名,密码,URL,驱动类
private static final String USER = "root";
private static final String PWD = "root";
private static final String URL = "jdbc:mysql://localhost:3306/day24";
private static final String DRIVER= "com.mysql.jdbc.Driver";
/**
* 注册驱动
*/
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 得到数据库的连接
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL,USER,PWD);
}
/**
* 关闭所有打开的资源
*/
public static void close(Connection conn, Statement stmt) {
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭所有打开的资源
*/
public static void close(Connection conn, Statement stmt, ResultSet rs) {
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
close(conn, stmt);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# SQL 注入问题
当我们输入以下密码,我们发现我们账号和密码都不对竟然登录成功了
请输入用户名:
newboy
请输入密码:
a' or '1'='1
select * from user where name='newboy' and password='a' or '1'='1'
登录成功,欢迎您:newboy
2
3
4
5
6
7
8
问题分析:
select * from user where name='newboy' and password='a' or '1'='1'
-- name='newboy' and password='a' 为假
-- '1'='1' 真相当于
select * from user where true; -- 查询了所有记录
2
3
4
我们让用户输入的密码和 SQL 语句进行字符串拼接。用户输入的内容作为了 SQL 语句语法的一部分,改变了原有 SQL 真正的意义,以上问题称为 SQL 注入。要解决 SQL 注入就不能让用户输入的密码和我们的 SQL 语句进行简单的字符串拼接。
# PreparedStatement 接口
PreparedStatement 是 Statement 接口的子接口,继承于父接口中所有的方法。它是一个预编译的 SQL 语句
# 执行原理
因为有预先编译的功能,提高 SQL 的执行效率。
可以有效的防止 SQL 注入的问题,安全性更高。
# Connection 创建 PreparedStatement 对象
# PreparedStatement 接口中的方法
# PreparedSatement 的好处
prepareStatement() 会先将 SQL 语句发送给数据库预编译。PreparedStatement 会引用着预编译后的结果,可以多次传入不同的参数给 PreparedStatement 对象并执行。减少 SQL 编译次数,提高效率。
安全性更高,没有 SQL 注入的隐患。
提高了程序的可读性
# 使用 PreparedStatement 的步骤
编写 SQL 语句,未知内容使用?占位:"SELECT * FROM user WHERE name=? AND password=?"
获得 PreparedStatement 对象
设置实际参数:setXxx(占位符的位置,真实的值)
执行参数化 SQL 语句
关闭资源
# JDBC 事务的处理
之前我们是使用 MySQL 的命令来操作事务。接下来我们使用 JDBC 来操作银行转账的事务。
# 准备数据
CREATE TABLE account (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10),
balance DOUBLE
);
-- 添加数据
INSERT INTO account (NAME, balance) VALUES ('Jack', 1000), ('Rose', 1000);
2
3
4
5
6
7
8
9
# API 介绍
# 开发步骤
获取连接
开启事务
获取到 PreparedStatement
使用 PreparedStatement 执行两次更新操作
正常情况下提交事务
出现异常回滚事务
最后关闭资源
import com.itheima.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo12Transaction {
// 没有异常,提交事务,出现异常回滚事务
public static void main(String[] args) {
//1) 注册驱动
Connection connection = null;
PreparedStatement ps = null;
try {
//2) 获取连接
connection = JdbcUtils.getConnection();
//3) 开启事务
connection.setAutoCommit(false);
//4) 获取到 PreparedStatement
// 从 jack 扣钱
ps = connection.prepareStatement("update account set balance = balance - ? where name=?");
ps.setInt(1, 500);
ps.setString(2,"Jack");
ps.executeUpdate();
// 出现异常
System.out.println(100 / 0);
// 给 rose 加钱
ps = connection.prepareStatement("update account set balance = balance + ? where name=?");
ps.setInt(1, 500);
ps.setString(2,"Rose");
ps.executeUpdate();
// 提交事务
connection.commit();
System.out.println("转账成功");
} catch (Exception e) {
e.printStackTrace();
try {
// 事务的回滚
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
System.out.println("转账失败");
}
finally {
//7) 关闭资源
JdbcUtils.close(connection,ps);
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53