JDBC简介
概念
JDBC就是使用Java语言操作关系型数据库的一套API
全称 Java Data Base Connectivity Java数据库连接
本质
定义的一套操作所有关系数据库的规则,即接口
快速入门
步骤
API详解
DriverManager
驱动管理类 作用:
- 注册驱动
- 获取数据库连接
Class.forName("com.mysql.jdbc.Driver");
此句可省略。
Connection
数据库连接对象 作用:
- 获取执行SQL的对象
- 管理事务
- 普通执行SQL对象
Statement createStatement();
- 预编译SQL的执行SQL对象:防止SQL注入
PreparedStatement prepareStatement(sql);
- 执行存储过程的对象
CallableStatement prepareCall(sql);
Statement
作用:执行sql语句
executeUpdate(sql); //执行DML、DDL语句 返回int类型
//返回值:(1)DML语句影响的行数 (2)DDL语句执行后,执行成功也可能返回0
executeQuery(sql); //执行DQL语句 返回ResultSet类型
//返回值:(1)ResultSet jie'guo'ji
执行DDL语句的返回值可能是0
ResultSet
结果集对象:封装了DDL查询语句的结果
stmt.executeQuery(sql); //执行DQL语句,返回ResultSet对象
使用步骤:
- 游标向下移动一行,并判断改行是否有数据:next()
- 获取数据:getXxx(参数)
//循环判断游标是否是最后一行末尾
while(rs.next()){
rs.getXxx(参数);
}
- 定义一个实体类Account
- 查询数据,封装到Account对象中
- 讲数据存入ArrayList集合中
!!!
PreparedStatement
作用:
- 预编译SQL语句并执行:预防SQL注入问题
SQL注入:通过操作输入来修改事先定义好的SQL语句,用以达到执行代码对服务器进行攻击的方法。
原理:
好处:
- 预编译SQL,性能更高。
- 防止SQL注入,将敏感字符进行转义。
数据库连接池
简介
数据库连接池是一个容器,负责分配,管理数据库连接(connection)。
它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个。
释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏
好处
- 资源重用
- 提升系统响应时间
- 避免数据库连接遗漏。
数据库连接池实现
标准接口: DataSource
官方提供的数据库连接池标准接口,由第三方组织实现此接口。
Connection getConnection();
常见的数据库连接池:
- DBCP
- C3P0
- Druid:(德鲁伊) 阿里巴巴开源的数据库连接池项目,功能强大,性能优秀,是Java语言最好的数据库连接池之一
练习
准备环境
-
-
查询
-
添加
-
修改
-
删除
//查询所有数据
@Test
public void testSelectAll() throws Exception {
//加载配置文件
Properties prop=new Properties();
prop.load(new FileInputStream("src/druid.properties"));
//获取连接池对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//获取数据库连接Connection
Connection connection=dataSource.getConnection();
//定义sql
String sql="select * from tb_brand";
//获取
PreparedStatement pstmt = connection.prepareStatement(sql);
//执行SQL
ResultSet rs=pstmt.executeQuery();
//创建集合
List<Brand> list=new ArrayList<>();
//处理结果,遍历所有数据
Brand brand=null;
while (rs.next()){
int id= rs.getInt("id");
String brandName= rs.getString("brand_name");
String companyName = rs.getString("company_name");
int ordered = rs.getInt("ordered");
String description = rs.getString("description");
int status = rs.getInt("status");
//封装对象
brand=new Brand();
brand.setId(id);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
//存入集合
list.add(brand);
}
System.out.println(list);
//释放资源
rs.close();
pstmt.close();
connection.close();
}
//添加
@Test
public void testAdd() throws Exception {
//模拟接收数据
String brandName="香飘飘";
String companyName="香飘飘";
int ordered=1;
String description="绕地球一圈";
int status=1;
//加载配置文件
Properties prop=new Properties();
prop.load(new FileInputStream("src/druid.properties"));
//获取连接池对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//获取数据库连接Connection
Connection connection=dataSource.getConnection();
//定义sql
String sql="insert into tb_brand(brand_name, company_name, ordered, description, status) VALUES(?,?,?,?,?)";
//获取
PreparedStatement pstmt = connection.prepareStatement(sql);
//设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
//执行SQL
int count= pstmt.executeUpdate();
//处理结果
if (count>0){
System.out.println(true);
}else {
System.out.println(false);
}
//释放资源
pstmt.close();
connection.close();
}
//修改
@Test
public void testUpate() throws Exception {
//模拟接收数据
String brandName="香飘飘";
String companyName="香飘飘";
int ordered=1;
String description="绕地球三圈";
int status=1;
int id=4;
//加载配置文件
Properties prop=new Properties();
prop.load(new FileInputStream("src/druid.properties"));
//获取连接池对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//获取数据库连接Connection
Connection connection=dataSource.getConnection();
//定义sql
String sql="update tb_brand\n"+
" set brand_name=?,\n"+
" company_name =?,\n"+
" ordered =?,\n"+
" description =?,\n"+
" status =?\n "+
" where id = ?";
//获取
PreparedStatement pstmt = connection.prepareStatement(sql);
//设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
pstmt.setInt(6,id);
//执行SQL
int count= pstmt.executeUpdate();
//处理结果
if (count>0){
System.out.println(true);
}else {
System.out.println(false);
}
//释放资源
pstmt.close();
connection.close();
}
//删除
@Test
public void testDelete() throws Exception {
//模拟接收数据
int id=4;
//加载配置文件
Properties prop=new Properties();
prop.load(new FileInputStream("src/druid.properties"));
//获取连接池对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//获取数据库连接Connection
Connection connection=dataSource.getConnection();
//定义sql
String sql="delete from tb_brand where id=?";
//获取
PreparedStatement pstmt = connection.prepareStatement(sql);
//设置参数
pstmt.setInt(1,id);
//执行SQL
int count= pstmt.executeUpdate();
//处理结果
if (count>0){
System.out.println(true);
}else {
System.out.println(false);
}
//释放资源
pstmt.close();
connection.close();
}