概念:是一种用于执行java语句的API
本质:一套帮助开发人员实现不同关系型数据库链接的规范(接口)
总结:首先注册驱动,然后获得一个数据库连接的对象connection,通过使用该对象获得一个执行者对象,然后改执行者对象可以执行sql语句。最后记得释放资源。
代码如下:
package com.itheima01;
import com.mysql.jdbc.Driver;
import java.sql.*;
import java.util.Collection;
public class JDBCDemo01 {
public static void main(String[] args) throws Exception {
//1 导入jar包
//2 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//3 获取数据库连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/study", "root", "123456");
//4 获取执行者对象
Statement statement = connection.createStatement();
//5 执行sql语句返回结果
String sql = "select * from student";
ResultSet resultSet = statement.executeQuery(sql);
//6 处理结果
while (resultSet.next()){
System.out.println(resultSet.getInt("sid") + "\t" + resultSet.getString("name"));
}
//7 释放资源
connection.close();
statement.close();
resultSet.close();
}
}
输出结果如下:
1、获取执行者对象:
①获取预编译执行者对象
②获取普通执行者对象
2、管理事务
①开启事务: setAutoCommit(bool autoCommit);参数是false,则开启事务。
②提交事务:commit();
③回滚事务:rollback();
3、释放资源
1、执行sql语句的对象:
①DML语句: int executeUpdata(String sql):
返回值int: 影响的行数;
参数sql:insert、updata、delete 语句;
②DQL语句:ResultSet executeQuery(String sql);
返回值ResultSet:封装的查询结果;
参数sql:可以执行的select语句;
2、释放资源
1、Result结果集对象:
①判断结果集是否有数据:
next():如果有数据返回true,并将索引只想下一行。没有数据返回false。
②获取结果集中数据
案例需求: 使用JDBC完成对student表的CRUD操作
数据准备:
①创建数据库和数据表
②创建dao domain service controller
step1:首先创建学生类(domain数据表对象层)
package com.itheima02.domain;
public class Student {
private Integer sid;
private String name;
private String sex;
//无参构造
//有参构造
//get set
//toString
}
step2:创建学生操作接口,学生操作接口的实现类(编写dao数据访问层)
package com.itheima02.dao;
import com.itheima02.domain.Student;
import java.util.ArrayList;
public interface StudentDao {
//查询所有学生信息
public abstract ArrayList findAll();
//条件查询,根据id获取学生信息
public abstract Student findById(Integer sid);
//新增学生信息
public abstract int insert(Student student);
//修改学生信息
public abstract int update(Student student);
//删除学生信息
public abstract int delete(Integer sid);
}
//接口的实现类
package com.itheima02.dao;
import com.itheima02.domain.Student;
import java.sql.*;
import java.util.ArrayList;
public class StudentDaolmpl implements StudentDao{
@Override
public ArrayList findAll() {
ArrayList list = new ArrayList<>();
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//1 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2 获取数据库链接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/study", "root", "123456");
//3 获取执行者对象
statement = connection.createStatement();
//4 执行sql语句并且接受返回的结果集
String sql = "select * from student";
resultSet = statement.executeQuery(sql);
//5 处理结果集
while (resultSet.next()){
Integer sid = resultSet.getInt("sid");
String name = resultSet.getString("name");
String sex = resultSet.getString("sex");
// 封装成Student对象
Student student = new Student(sid, name, sex);
// 将student 放入集合中
list.add(student);
System.out.println();
}
//6 释放资源(不能放在try里面实现,要在finally里面)
}catch (Exception e){
e.printStackTrace();
}finally {
if (connection != null){
try {
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if (statement != null){
try {
statement.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if (resultSet != null){
try {
resultSet.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
return list;
}
@Override
public Student findById(Integer sid) {
return null;
}
@Override
public int insert(Student student) {
return 0;
}
@Override
public int update(Student student) {
return 0;
}
@Override
public int delete(Integer sid) {
return 0;
}
}
step3:Service接口 与service实现类
//service的实现类
//service调用dao
package com.itheima02.service;
import com.itheima02.dao.StudentDao;
import com.itheima02.dao.StudentDaolmpl;
import com.itheima02.domain.Student;
import java.util.ArrayList;
public class StudentServicelmpl implements StudentService{
private StudentDao dao = new StudentDaolmpl();
@Override
public ArrayList findAll() {
return dao.findAll();
}
@Override
public Student findById(Integer sid) {
return null;
}
@Override
public int insert(Student student) {
return 0;
}
@Override
public int update(Student student) {
return 0;
}
@Override
public int delete(Integer sid) {
return 0;
}
}
package com.itheima02.service;
import com.itheima02.domain.Student;
import java.util.ArrayList;
public interface StudentService {
//查询所有学生信息
public abstract ArrayList findAll();
//条件查询,根据id获取学生信息
public abstract Student findById(Integer sid);
//新增学生信息
public abstract int insert(Student student);
//修改学生信息
public abstract int update(Student student);
//删除学生信息
public abstract int delete(Integer sid);
}
step4:控制层
package com.itheima02.controller;
import com.itheima02.domain.Student;
import com.itheima02.service.StudentService;
import com.itheima02.service.StudentServicelmpl;
import org.junit.Test;
import java.util.ArrayList;
public class StudentController {
private StudentService service = new StudentServicelmpl();
@Test
public void findAll(){
ArrayList list = service.findAll();
for (Student student: list){
System.out.println(student);
}
}
}