jdbc通用学习

133 阅读11分钟

jdbc

JDBC(Java Database Connectivity,Java 数据库连接) 是 Java 提供的一套 标准 API,用于让 Java 应用程序与各种数据库进行交互。它允许 Java 程序通过 SQL 访问数据库,并对数据库执行查询、更新等操作。

JDBC 本质上是 Java 访问数据库的桥梁,它定义了一组 接口(interface),不同数据库厂商(如 MySQL、Oracle)会提供自己的 驱动程序(Driver) 来实现这些接口,使 Java 可以统一访问各种数据库。

image-20250304012253131.png

这里就是java中多态的体现

jdbc是一套接口,没有具体实现,是让各个数据库厂商来实现

下载

下载地址 downloads.mysql.com/archives/c-…

image.png

下载zip, source是linux里的,另外注意你版本号要和本地mysq版本一致,这里我是8.0.13版本

intelij idea中的配置

1.第一步打开project structure

image.png 2.第二步,project Structure进行配置 (点 libraries里进行)

image.png

3.应用后 如下成功

image.png

注意事项

现在官网的jdbc下载下来是zip包,你需要解压一下才能拿到jar包

image.png

使用

1.注册驱动

5之后的加载记得使用

 private static String jdbcName = "com.mysql.cj.jdbc.Driver";

2获取连接

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-04日--02:15
 */
public class jdbcDemo2 {

    //数据库地址
    private static String dbUrl = "jdbc:mysql://localhost:3306/db_jdbc?serverTimezone=Asia/Shanghai";

    //用户名
    private static String user = "root";
    //密码
    private static String password = "sqlyutong6443!";

    //驱动名称
    private static String driver = "com.mysql.cj.jdbc.Driver";

    public static void main(String[] args) {
        try {
            Class.forName(driver);
            System.out.println("加载驱动成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("加载驱动失败");
        }
        Connection connection = null;
        try {
            //获取数据库连接
            connection = DriverManager.getConnection(dbUrl, user, password);
            System.out.println("数据库连接成功");
            System.out.println("开始数据库操作");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }

        finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}

常用数据库URL地址的写法: Oracle写法:jdbc:oracle:thin:@localhost:1521:sid SqlServer: jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=sid MySql: jdbc:mysql://localhost:3306/sid Mysql的url地址的简写形式: jdbc:mysql:///sid 常用属性:useUnicode=true&characterEncoding=UTF-8

java.sql.Statement接口: 操作sql语句,并返回相应结果

1.实现添加操作

import utils.DbUtil;

import java.sql.Connection;
import java.sql.Statement;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-04日--02:44
 */
public class jdbcDemo3 {

    public static void main(String[] args) throws Exception{
        // 使用封装后的工具类
        DbUtil dbUtil = new DbUtil();
        String sql = "insert into t_book values(2,'golang牛逼',888,'童哥',1)";

        // 获取数据连接
        Connection con = dbUtil.getCon();

        Statement statement = con.createStatement();
        int result = statement.executeUpdate(sql);
        if(result >0){
            System.out.println("添加成功: " + result +"条数据");
        }else
            System.out.println("添加失败");

        statement.close();
        con.close();
    }
}

实际上,最好事先封装一个Dbutil,免得每次都设置用户名,链接,密码等,这也体现了面向对象的做法

package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * @description  封装jdbc
 * @author: RicksonYu
 * @create: 2025年-03月-04日--02:44
 */
public class DbUtil {


    //数据库地址
    private static String dbUrl = "jdbc:mysql://localhost:3306/db_jdbc?serverTimezone=Asia/Shanghai";

    //用户名
    private static String user = "root";
    //密码
    private static String password = "sqlyutong6443!";

    //驱动名称
    private static String driver = "com.mysql.cj.jdbc.Driver";


    /**
     * 获取数据库连接
     * **/
    public Connection getCon() throws Exception{

        Class.forName(driver);
        Connection con = DriverManager.getConnection(dbUrl, user, password);

        return con;
    }
    public void close(Statement statement,Connection con) throws Exception{


        if(statement != null)
        {
            statement.close();
            if(con != null)
            {
                con.close();
            }
        }

    }
}

2.Statement的更新操作

package sec03;

import model.Book;
import utils.DbUtil;

import java.sql.Connection;
import java.sql.Statement;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-04日--03:33
 */
public class Demo1 {

    private static DbUtil dbUtil = new DbUtil();

    public static int updateBook(Book  book) throws Exception{
        Connection con = dbUtil.getCon();
        String sql = "update t_book set  bookName='"+book.getBookName()+"',price="+book.getPrice()+",author='"+book.getAuthor()+"',bookTypeId="+book.getBookTypeId()+" where id="+book.getId();
        Statement stmt = con.createStatement();
        int result = stmt.executeUpdate(sql);
        dbUtil.close(stmt, con);
        return result;
    }

    public static void main(String[] args) throws Exception{

        Book book = new Book(1, "java并发编程", 39.9f, "于童", 1);
        int result = updateBook(book);
        if(result >0)
            System.out.println("更新成功");
        else
            System.out.println("更新失败");
    }
}

3.Statement删除

javapackage sec04;

import utils.DbUtil;

import java.sql.Connection;
import java.sql.Statement;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-04日--22:15
 */
public class Demo1 {

    private static DbUtil dbUtil = new DbUtil();
    private static int deleteBook(int id) throws Exception{

        Connection con = dbUtil.getCon();
        String sql = "delete from t_book where id=" + id;
        Statement stmt = con.createStatement();
        int result = stmt.executeUpdate(sql);
        dbUtil.close(stmt, con);
        return result;

    }

    public static void main(String[] args) throws Exception{

        int result = deleteBook(4);

        if(result >0)
            System.out.println("删除成功");
        else
            System.out.println("删除失败");
    }
}

PreparedStatement接口引入

PreparedStatement是 Statement的子接口,属于预处理操作,与直接使用 Statement不同的是,PreparedStatemen在操作时,是先在数据表中准备好了一条 SQL语句,但是此 SQL 语句的具体内容暂时不设置,而是之后再进行设置。 (以后开发一般用 PreparedStatement,不用Statement )

statement需要拼接Sql语句,非常繁琐

PreparedStatement接口实现添加数据操作

package Preparedstatement;

import model.Book;
import utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-04日--22:44
 */
public class demo1 {

    static DbUtil dbUtil = new DbUtil();

    //添加操作
    private static int addBook(Book book) throws Exception{

        Connection con = dbUtil.getCon();//获取连接
        String sql = "insert into t_book values(?,?,?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1,book.getId());
        preparedStatement.setString(2,book.getBookName());
        preparedStatement.setFloat(3,book.getPrice());
        preparedStatement.setString(4,book.getAuthor());
        preparedStatement.setInt(5,book.getBookTypeId());
        int result = preparedStatement.executeUpdate();
       dbUtil.close(preparedStatement,con);
       return result;
    }

    public static void main(String[] args) throws Exception{
        Book book = new Book(5, "jvm虚拟机调优", 39.9f, "于童", 1);

        int result = addBook(book);
        if(result >0)
            System.out.println("添加成功");
        else
            System.out.println("添加失败");

    }

}

PreparedStatement接口实现更新数据操作

package Preparedstatement;

import model.Book;
import utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-04日--23:07
 */
public class Demo02 {

    private static DbUtil dbUtil = new DbUtil();
    private static int updateBook(Book book) throws Exception{


        /**
         * 更新
         * */

        Connection connection = dbUtil.getCon();
        String sql = "update t_book set  bookName=?,price=?,author=?,bookTypeId=? where id=?";
        PreparedStatement pstmt = connection.prepareStatement(sql);
        pstmt.setString(1,book.getBookName());
        pstmt.setFloat(2,book.getPrice());
        pstmt.setString(3,book.getAuthor());
        pstmt.setInt(4,book.getBookTypeId());
        pstmt.setInt(5,book.getId());
        int result = pstmt.executeUpdate();
        dbUtil.close(pstmt,connection); //要记得用完关闭
        return result;
    }

    public static void main(String[] args) throws Exception{
        Book book = new Book(5, "jvm虚拟机调优第二版", 39.9f, "于童", 1);

        int result = updateBook(book);

        if (result >0)
        {
            System.out.println("更新成功");
        }
        else {
            System.out.println("更新失败");
        }

    }
}

PreparedStatement接口实现删除数据操作

package Preparedstatement;

import model.Book;
import utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-04日--23:07
 */
public class Demo02 {

    private static DbUtil dbUtil = new DbUtil();

    /**
     * 更新
     * */
    private static int updateBook(Book book) throws Exception{

        Connection connection = dbUtil.getCon();
        String sql = "update t_book set  bookName=?,price=?,author=?,bookTypeId=? where id=?";
        PreparedStatement pstmt = connection.prepareStatement(sql);
        pstmt.setString(1,book.getBookName());
        pstmt.setFloat(2,book.getPrice());
        pstmt.setString(3,book.getAuthor());
        pstmt.setInt(4,book.getBookTypeId());
        pstmt.setInt(5,book.getId());
        int result = pstmt.executeUpdate();
        dbUtil.close(pstmt,connection); //要记得用完关闭
        return result;
    }

    public static int deleteBook(int id) throws Exception{

        Connection connection = dbUtil.getCon();
        String sql = "delete from t_book where id=?";
        PreparedStatement pstmt = connection.prepareStatement(sql);
        pstmt.setInt(1,id);
        int result = pstmt.executeUpdate();
        dbUtil.close(pstmt,connection); //要记得用完关闭
        return result;

    }

    public static void main(String[] args) throws Exception{

      int result = deleteBook(5);
        if (result == 1)
            System.out.println("删除成功");
        else
            System.out.println("删除失败");


    }
}

ResultSet结果集

当我们查询数据库时,返回的是一个二维的结果集,我们这时候需要使用 Resutset 来遍历结果集,获取每一行的数据。

image.png

package resultset;

import model.Book;
import utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 遍历查询结果
 * @author: RicksonYu
 * @create: 2025年-03月-05日--00:10
 */
public class Demo1 {

    private static DbUtil dbUtil = new DbUtil();


    //columnIndex
    private static void listBook() throws Exception{
        Connection con = dbUtil.getCon();
        String sql = "select * from t_book";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        ResultSet rs = preparedStatement.executeQuery();//返回结果集 result
        while(rs.next()){
            int id = rs.getInt(1);
            String bookName = rs.getString(2);
            float price = rs.getFloat(3);
            String author = rs.getString(4);
            int bookTypeId = rs.getInt(5);
            System.out.println(id+"\t"+bookName+"\t"+price+"\t"+author+"\t"+bookTypeId);
        }

    }

    //实际开发用这种形式  columnlabel
    private static void listBook2() throws Exception{
        Connection con = dbUtil.getCon();
        String sql = "select * from t_book";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        ResultSet rs = preparedStatement.executeQuery();//返回结果集 result
        while(rs.next()){
            int id = rs.getInt("id");
            String bookName = rs.getString("bookName");
            float price = rs.getFloat("price");
            String author = rs.getString("author");
            int bookTypeId = rs.getInt("bookTypeId");
            System.out.println(id+"\t"+bookName+"\t"+price+"\t"+author+"\t"+bookTypeId);
        }

    }


    //实际开发返回集合
    private static List<Book> listBook3() throws Exception{
       List<Book> bookList = new ArrayList<>();
        Connection con = dbUtil.getCon();
        String sql = "select * from t_book";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        ResultSet rs = preparedStatement.executeQuery();//返回结果集 result
        while(rs.next()){
            int id = rs.getInt("id");
            String bookName = rs.getString("bookName");
            float price = rs.getFloat("price");
            String author = rs.getString("author");
            int bookTypeId = rs.getInt("bookTypeId");

            bookList.add(new Book(id,bookName,price,author,bookTypeId));
        }
        return bookList;
    }


    public static void main(String[] args) throws Exception{
        //1.遍历了一个结果集
//        listBook();
//        listBook2();
         List<Book> bookList = listBook3();
         for(Book book:bookList)
             System.out.println(book);
    }
}


记得在实体要 toString一下,不然会返回对象,当然直接在实体类上打上@Data,一劳永逸

大数据对象处理

image.png

处理clob数据

package 大数据对象处理;

import model.Book;
import utils.DbUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-05日--11:02
 */
public class Demo1 {

    private static DbUtil dbUtil = new DbUtil();

    /**
     * 添加图书
     * */

    private static int addBook(Book book) throws Exception{

        //获取连接
        Connection con = dbUtil.getCon();
        String sql = "insert into t_book values(?,?,?,?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1,book.getId());
        preparedStatement.setString(2,book.getBookName());
        preparedStatement.setFloat(3,book.getPrice());
        preparedStatement.setString(4,book.getAuthor());
        preparedStatement.setInt(5,book.getBookTypeId());
        File context=book.getContext();//获取文件
        //定义输入流
        InputStream inputStream = new FileInputStream(context);
        preparedStatement.setAsciiStream(6,inputStream,context.length());

        int result = preparedStatement.executeUpdate();
        dbUtil.close(preparedStatement,con);
        return result;
    }

    //读取文本
    public static void getBook(int id) throws Exception{

        Connection con = dbUtil.getCon();
        String sql = "select * from t_book where id=?";
        PreparedStatement pres = con.prepareStatement(sql);
        pres.setInt(1,id);
        ResultSet result = pres.executeQuery();
        if(result.next()){

            int id1 = result.getInt("id");
            String bookName = result.getString("bookName");
            float price = result.getFloat("price");
            String author = result.getString("author");
            int bookTypeId = result.getInt("bookTypeId");
            Clob c = result.getClob("context");
            String context = c.getSubString(1,(int)c.length());
            System.out.println(id1+"\t"+bookName+"\t"+price+"\t"+author+"\t"+bookTypeId+"\t"+context);
      }
        dbUtil.close(pres,con);

    }

    public static void main(String[] args) throws Exception{
//        File context = new File("C:/Users/Bravo _Tango/Desktop/HelloWorld.txt");
//        Book book = new Book(4,"java高级应用",39.9f,"于童",1,context);
//        int result = addBook(book);
//
//        if(result >0)
//            System.out.println("添加成功");
//        else
//            System.out.println("添加失败");
        getBook(4);
    }
}

Blog数据

package 大数据对象处理;

import model.Book;
import utils.DbUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-05日--11:02
 */
public class Demo2 {

    private static DbUtil dbUtil = new DbUtil();

    /**
     * 添加图书
     * */

    private static int addBook(Book book) throws Exception{

        //获取连接
        Connection con = dbUtil.getCon();
        String sql = "insert into t_book values(?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1,book.getId());
        preparedStatement.setString(2,book.getBookName());
        preparedStatement.setFloat(3,book.getPrice());
        preparedStatement.setString(4,book.getAuthor());
        preparedStatement.setInt(5,book.getBookTypeId());
        File context=book.getContext();//获取文件
        //定义输入流
        InputStream inputStream = new FileInputStream(context);
        preparedStatement.setAsciiStream(6,inputStream,context.length());

        File pic = book.getPic();
        InputStream inputStream2 = new FileInputStream(pic);
        preparedStatement.setBinaryStream(7,inputStream2,pic.length());


        int result = preparedStatement.executeUpdate();
        dbUtil.close(preparedStatement,con);
        return result;
    }

    //读取文本
    public static void getBook(int id) throws Exception{

        Connection con = dbUtil.getCon();
        String sql = "select * from t_book where id=?";
        PreparedStatement pres = con.prepareStatement(sql);
        pres.setInt(1,id);
        ResultSet result = pres.executeQuery();
        if(result.next()){

            int id1 = result.getInt("id");
            String bookName = result.getString("bookName");
            float price = result.getFloat("price");
            String author = result.getString("author");
            int bookTypeId = result.getInt("bookTypeId");
            Clob c = result.getClob("context");
            String context = c.getSubString(1,(int)c.length());
            System.out.println(id1+"\t"+bookName+"\t"+price+"\t"+author+"\t"+bookTypeId+"\t"+context);
        }
        dbUtil.close(pres,con);

    }

    public static void main(String[] args) throws Exception{
        File context = new File("C:/Users/Bravo _Tango/Desktop/HelloWorld.txt");
        File pic = new File("C:/Users/Bravo _Tango/Desktop/pic.png");
        Book book = new Book(5,"java高级应用",39.9f,"于童",1,context,pic);
        int result = addBook(book);

        if(result >0)
            System.out.println("添加成功");
        else
            System.out.println("添加失败");

    }
}

image.png

查看,发现存储成功

写入:

package 大数据对象处理;

import model.Book;
import utils.DbUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-05日--11:02
 */
public class Demo2 {

    private static DbUtil dbUtil = new DbUtil();

    /**
     * 添加图书
     * */

    private static int addBook(Book book) throws Exception{

        //获取连接
        Connection con = dbUtil.getCon();
        String sql = "insert into t_book values(?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1,book.getId());
        preparedStatement.setString(2,book.getBookName());
        preparedStatement.setFloat(3,book.getPrice());
        preparedStatement.setString(4,book.getAuthor());
        preparedStatement.setInt(5,book.getBookTypeId());
        File context=book.getContext();//获取文件
        //定义输入流
        InputStream inputStream = new FileInputStream(context);
        preparedStatement.setAsciiStream(6,inputStream,context.length());

        File pic = book.getPic();
        InputStream inputStream2 = new FileInputStream(pic);
        preparedStatement.setBinaryStream(7,inputStream2,pic.length());


        int result = preparedStatement.executeUpdate();
        dbUtil.close(preparedStatement,con);
        return result;
    }

    //读取文本
    public static void getBook2(int id) throws Exception{

        Connection con = dbUtil.getCon();
        String sql = "select * from t_book where id=?";
        PreparedStatement pres = con.prepareStatement(sql);
        pres.setInt(1,id);
        ResultSet result = pres.executeQuery();
        if(result.next()){

            int id1 = result.getInt("id");
            String bookName = result.getString("bookName");
            float price = result.getFloat("price");
            String author = result.getString("author");
            int bookTypeId = result.getInt("bookTypeId");
            Clob c = result.getClob("context");
            String context = c.getSubString(1,(int)c.length());
            Blob b = result.getBlob("pic");
            //图片导出到另外的盘
            //定义输出流
            FileOutputStream f = new FileOutputStream(new File("D:/test.png"));
            f.write(b.getBytes(1,(int)b.length()));
            f.close();
            System.out.println(id1+"\t"+bookName+"\t"+price+"\t"+author+"\t"+bookTypeId+"\t"+context);
        }
        dbUtil.close(pres,con);

    }

    public static void main(String[] args) throws Exception{
//        File context = new File("C:/Users/Bravo _Tango/Desktop/HelloWorld.txt");
//        File pic = new File("C:/Users/Bravo _Tango/Desktop/pic.png");
//        Book book = new Book(5,"java高级应用",39.9f,"于童",1,context,pic);
//        int result = addBook(book);
//
//        if(result >0)
//            System.out.println("添加成功");
//        else
//            System.out.println("添加失败");
//
//    }
        getBook2(5);
    }
}

image.png

写入成功,以流的形式从库里取出

使用CallableStatement接口调用存储过程

image.png

package callablestatement;

import utils.DbUtil;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;

/**
 * @description 测试CallableStatement 存储过程
 * @author: RicksonYu
 * @create: 2025年-03月-05日--12:38
 */
public class Demo1 {

    private static DbUtil dbUtil = new DbUtil();

    /**
     * 调用存储过程,通过id查询bookName
    * **/
    private static String getBookNameById(int id) throws Exception{

        Connection con = dbUtil.getCon();
        String sql = "{call pro_getBookNameById(?,?)}";
        CallableStatement call = con.prepareCall(sql);
        call.setInt(1,id); //设置第一个参数
        call.registerOutParameter(2, Types.VARCHAR);//设置返回类型
        call.execute();
        String bookName = call.getString("bN");
        dbUtil.close(call,con);
        return bookName;
    }

    public static void main(String[] args) throws Exception{

        System.out.println("图书名称是: "+getBookNameById(2));
    }
}

DatabaseMetaData 的使用

image.png

package databasemeta;

import utils.DbUtil;

import java.sql.Connection;
import java.sql.DatabaseMetaData;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-05日--13:04
 */
public class Demo1 {

    public static void main(String[] args) throws Exception{

        DbUtil dbUtil = new DbUtil();
        Connection con = dbUtil.getCon();
        //获取元数据
        DatabaseMetaData dmd = con.getMetaData();
        System.out.println("获取数据库名称: "+dmd.getDatabaseProductName());
        System.out.println("获取数据库版本: "+dmd.getDatabaseProductVersion());
    }
}

重要的是第二个,获取ResultSet相关信息

package databasemeta;

import utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

/**
 * @description
 * @author: RicksonYu
 * @create: 2025年-03月-05日--13:41
 */
public class Demo2 {

    public static void main(String[] args) throws Exception{
        DbUtil dbUtil = new DbUtil();
        Connection con = dbUtil.getCon();
        String sql = "select * from t_book";
        PreparedStatement pstmt = con.prepareStatement(sql);
        ResultSetMetaData rsmd = pstmt.getMetaData();
        int num = rsmd.getColumnCount();//获取元数据列的总数
        System.out.println("共有"+num+"列");
        for(int i = 1; i <= num; i++)
        {
            System.out.println("第"+i+"列的名称为:"+rsmd.getColumnName(i));

        }
    }
}

事务

1.事务的概念

事务处理在数据库开发中有着非常重要的作用,所谓事务就是所有的操作要么一起成功,要么一起失败,事务本身具有原子性(Atomiciy)、一致性(Consistency)、隔离性或独立性(Isolation)、持久性(Durability)4个特性,这4个特性也被称为 ACID 特征。原子性:原子性是事务最小的单元,是不可再分隔的单元,相当于一个个小的数据库操作,这些操作必须同时成功,如果一个失败了,则一切的操作将全部失败。一致性:指的是在数据库操作的前后是完全一致的,保证数据的有效性,如果事务正常操作则系统会维持有效性,如果事务出现了错误,则回到最原始状态,也要维持其有效性,这样保证事务开始时和结束时系统处干致状态。 隔离性:多个事务可以同时进行目彼此之间无法访问,只有当事务完成最终操作时,才可以看到结果,持久性:事务完成之后,它对于系统的影响是永久性的。该修改即使出现致命的系统故障也将一直保持。

2.Mysql对事务的支持

image.png