jdbc学习笔记3,blob类型数据的操作,preparedstatement的批量操作,数据库的事务,以及事务的特性,隔离权限的修改

jdbc学习笔记3

实现操作blob类型的数据

mysql中的blob类型

BLOB是mysql中的大型数据
tinyblob,最大255字节
blob,最大65k
mediumblob,最大16m
longblob,最大4g
插入blob数据代码实现

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class Blobtest {
    @Test
    public void test() throws Exception {
        Connection connection = JdbcUtils.getConnection();
        String sql="insert customers(name,email,birth,photo)value (?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1,"李沁");
        preparedStatement.setObject(2,"liqin.com");
        preparedStatement.setObject(3,"1990-10-01");
        FileInputStream is=new FileInputStream(new File("15159794979876372.jpg"));//io流文件自定义路径
        preparedStatement.setBlob(4,is);
        preparedStatement.execute();
        JdbcUtils.closeResourse(connection,preparedStatement);
    }
}

从列表中读取blob

import org.junit.Test;

import java.io.*;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Blobtest {
    @Test
    public void test() throws Exception {
        Connection connection = JdbcUtils.getConnection();
        String sql="insert customers(name,email,birth,photo)value (?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1,"李沁");
        preparedStatement.setObject(2,"liqin.com");
        preparedStatement.setObject(3,"1990-10-01");
        FileInputStream is=new FileInputStream(new File("15159794979876372.jpg"));
        preparedStatement.setBlob(4,is);
        preparedStatement.execute();
        JdbcUtils.closeResourse(connection,preparedStatement);
    }
    @Test
    public void test2(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        InputStream is= null;
        FileOutputStream fos = null;
        try {
            connection = JdbcUtils.getConnection();
            String sql="select photo from customers where name=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"李沁");
            resultSet = preparedStatement.executeQuery();
            Blob photo = resultSet.getBlob("photo");
            is = photo.getBinaryStream();
            fos = new FileOutputStream("123.jpg");
            byte[] bytes = new byte[1024];
            int len;
            while ((len=is.read(bytes))!=-1) {
                fos.write(bytes,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (is!=null)
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {if (fos!=null)
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            JdbcUtils.closeResourse2(connection,preparedStatement,resultSet);
        }
    }
}

preparstatement的批量操作

创建一个数据表

create table goods(
 id INT primary key auto_increment,
 name varchar(25)
);

批量插入数据

import org.junit.Test;

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

public class Inserttest {
    //批量操作
    //update,dete本身具有批量操作的功能
    @Test
    public void test(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JdbcUtils.getConnection();
            String sql ="insert into goods(name)value (?)";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < 20000; i++) {
                preparedStatement.setObject(1,"name_"+i);
                preparedStatement.execute();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.closeResourse(connection,preparedStatement);
        }

    }
}

批量插入2(使用addbatch,clearcatch,executebatch)分组处理提高效率

   @Test
    public void test2(){
        //addbanth()//
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JdbcUtils.getConnection();
            String sql ="insert into goods(name)value (?)";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i <=20000; i++) {
                preparedStatement.setObject(1,"name_"+i);
                preparedStatement.addBatch();
                if (i%500==0){
                    preparedStatement.executeBatch();
                    preparedStatement.clearBatch();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.closeResourse(connection,preparedStatement);
        }
    }

数据库事务

事务是一组逻辑操作单元(一个或多个DML操作)
个人理解需要多个增删改查操作都完成完成一个事务
当一个操作完成其他操作失败时,将数据回滚,保证事务的整体性操作
DML使用set autocomit =false取消自动提交方便回滚
默认关闭资源连接时进行提交
故保证事务的整体性
1.不适用DDL操作
2.set autocomit =false
3.使用一个连接完成所有操作
修改update方法

 public void update1(Connection connection,String sql,Object ...args){
        //获取连接
        PreparedStatement preparedStatement = null;
        try {
            connection = JdbcUtils.getConnection();
            //预编译sql语句
            preparedStatement = connection.prepareStatement(sql);
            //填充占位符
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i+1,args[i]);
            }
            preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.closeResourse(null,preparedStatement);
        }

    }

代码实现事务的整体性操作

    @Test
    public void test2() {


        Connection connection = null;
        try {
            connection = JdbcUtils.getConnection();
            //取消数据自动提交
            connection.setAutoCommit(false);
            String sql="update user_table set balance =balance -100 where user=?";
            update1(connection,sql,"AA");
            String sql2="update user_table set balance =balance +100 where user=?";
            update1(connection,sql2,"BB");
            connection.commit();//提交数据
            System.out.println("CHENGG");
        } catch (Exception e) {
           try {
                connection.rollback();//出现错误回滚数据
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
         try {
                connection.setAutoCommit(true);//恢复数据自动提交
            } catch (SQLException e) {
                e.printStackTrace();
            }
            JdbcUtils.closeResourse(connection,null);
        }
    }
事务的acid属性

原子性:事务是一个不可分割的单位要么都发生,要么都不发生
一致性:事务必须从一个状态变成另外一个状态
隔离性:不能影响其他事物,互不干扰
持久性:事务完成后就不可回滚

数据库的并发问题

脏读:对于两个事务t1,t2,当t1读取的数据为t2未提交的数据
不可重复读:对于事务t1,t2,t1读取数据后t2进行修改后t1读取后数据不同,不显示最新数据
幻读:t1在一个表中读了一个字段,t2插入行,数据不相同不显示最新数据

mysql设置隔离级别
查看隔离级别
select @@tx_isolation;
mysql默认repeatable-read(隔离级别)避免脏读和不可重复读
修改当前连接的隔离级别
set transaction isolation level read committed;
修改全局隔离级别
set global transaction isolation level read conmitted;(read committed隔离级别:避免脏读)

java修改数据库的隔离级别

connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTIED);

总结

本篇文章主要学习了jdbc中blob数据类型的增删改查,以及perparedStatement批量操作数据
以及数据库的事务,包括事务的特性和修改隔离权限
剑谱最终页,无爱即是神

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值