Java JDBC — Oracle官方文档 — Trail: JDBC Database Access (The Java™ Tutorials) 2022-07-19 11:04:44
概念
Java DataBase Connectivity
Java 数据库连接, Java语言操作数据库
- JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。
各个数据库厂商去实现这套接口,提供数据库驱动jar包。
我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。
快速入门
- 步骤:
1
2
3
4
5
6
7
8
9
101. 导入驱动jar包 mysql-connector-java-5.1.37-bin.jar
1.复制mysql-connector-java-5.1.37-bin.jar到项目的libs目录下
2.右键-->Add As Library
2. 注册驱动
3. 获取数据库连接对象 Connection
4. 定义sql
5. 获取执行sql语句的对象 Statement
6. 执行sql,接受返回结果
7. 处理结果
8. 释放资源 - 代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17//1. 导入驱动jar包
//2.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库连接对象
Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/db3", "root", "root");
//4.定义sql语句
String sql = "update account set balance = 500 where id = 1";
//5.获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
//6.执行sql
int count = stmt.executeUpdate(sql);
//7.处理结果
System.out.println(count);
//8.释放资源
stmt.close();
conn.close();
组成对象
DriverManager
:驱动管理对象
- 注册驱动:告诉程序该使用哪一个数据库驱动jar
1
2
3
4
5
6
7
8
9
10
11
12
13static void registerDriver(Driver driver) :注册与给定的驱动程序 DriverManager 。
写代码使用: Class.forName("com.mysql.jdbc.Driver");
通过查看源码发现:在com.mysql.jdbc.Driver类中存在静态代码块
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
注意:mysql5之后的驱动jar包可以省略注册驱动的步骤。
已在jar包的META-INF/services/java.sql.Driver文件中定义了 - 获取数据库连接:
1
2
3
4
5
6
7
8* 方法:static Connection getConnection(String url, String user, String password)
* 参数:
* url:指定连接的路径
* 语法:jdbc:mysql://ip:端口号/数据库名称
* 例子:jdbc:mysql://localhost:3306/db3
* 细节:如果连接的是本机3306,可简写为:jdbc:mysql:///数据库名称
* user:用户名
* password:密码
Connection
:数据库连接对象
1 | 1. 获取执行sql 的对象 |
Statement
:执行sql的对象
- 执行sql
1
2
3
41. boolean execute(String sql) :可以执行任意的sql 了解
2. int executeUpdate(String sql) :执行DML(insert、update、delete)语句、DDL(create,alter、drop)语句
* 返回值:影响的行数,可以通过这个影响的行数判断DML语句是否执行成功 返回值>0的则执行成功,反之,则失败。
3. ResultSet executeQuery(String sql) :执行DQL(select)语句 - 练习:
ResultSet
:结果集对象,封装查询结果
1 | * boolean next(): 游标向下移动一行,判断当前行是否是最后一行末尾(是否有数据), |
PreparedStatement
:执行sql的对象
1 | 1. SQL注入问题:在拼接sql时,有一些sql的特殊关键字参与字符串的拼接。会造成安全性问题 |
JDBCUtils
:抽取JDBC工具类
src
目录下的jdbc.properties
1
2
3
4
5
6
7
8
9# 加?serverTimezone=UTC避免时区错误 指定字符集防止编码异常
url=jdbc:mysql:///student?serverTimezone=UTC&characterEncoding=utf-8
user=root
password=123456
# 可省略配置driver,在驱动jar包中已经标注 META-INF/services/java.sql.Driver
# MySQL5 com.mysql.jdbc.Driver
# MySQL8 com.mysql.cj.jdbc.Driver
#driver=com.mysql.jdbc.Driver
#driver=com.mysql.cj.jdbc.Driver
JDBCUtils.java
1 | import java.io.FileReader; |
事务
MySQL 事务
JDBC控制事务:
1 | 1. 事务:一个包含多个步骤的业务操作。 |
连接池
简介
- 概念:
1
2
3其实就是一个容器(集合),存放数据库连接的容器。
当系统初始化好后,容器被创建,容器中会申请一些连接对象,
当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归还给容器。 - 好处:
1
21. 节约资源
2. 用户访问高效 - 实现:
1
2
3
4
5
6
7
8
91. 标准接口:DataSource javax.sql包下的
* 获取连接:getConnection()
* 归还连接:Connection.close()。
如果连接对象Connection是从连接池中获取的,
那么调用Connection.close()方法,则不会再关闭连接了。而是归还连接
2. 一般我们不去实现它,有数据库厂商来实现
1. C3P0:数据库连接池技术
2. Druid:数据库连接池实现技术,由阿里巴巴提供的
C3P0(了解)
:数据库连接池技术
1 | * 步骤: |
c3p0-config.xml
1 | <c3p0-config> |
Druid(重要)
:数据库连接池实现技术,由阿里巴巴提供的
1 | 1. 步骤: |
src
目录下的druid.properties
1 | driverClassName=com.mysql.cj.jdbc.Driver |
连接池改造JDBCUtils.java
1 | import com.alibaba.druid.pool.DruidDataSourceFactory; |