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

news/2024/5/18 22:43:21 标签: 数据库, sql, mysql, jdbc

jdbc3_0">jdbc学习笔记3

实现操作blob类型的数据

sqlblob_2">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插入行,数据不相同不显示最新数据

sql_236">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批量操作数据
以及数据库的事务,包括事务的特性和修改隔离权限
剑谱最终页,无爱即是神


http://www.niftyadmin.cn/n/764781.html

相关文章

石子合并(区间dp)

设有 N 堆石子排成一排&#xff0c;其编号为 1&#xff0c;2&#xff0c;3&#xff0c;…&#xff0c;N。 每堆石子有一定的质量&#xff0c;可以用一个整数来描述&#xff0c;现在要将这 N 堆石子合并成为一堆。 每次只能合并相邻的两堆&#xff0c;合并的代价为这两堆石子的…

jdbc学习笔记4

jdbc的学习笔记4 数据库连接池 数据库连接池的必要性 java调用drivermanger类时&#xff0c;每次想数据库建立连接时都会将connection加载到内存中&#xff0c;执行完成后再断开连接&#xff0c;这样的方式会导致&#xff0c;会消耗大量的内存和时间&#xff0c;数据库的连接…

P1880 [NOI1995] 石子合并

[NOI1995] 石子合并 - 洛谷 /** Description: To iterate is human, to recurse divine.* Autor: Recursion* Date: 2022-04-02 23:00:48* LastEditTime: 2022-04-05 10:38:03*/ #include <bits/stdc.h> #define LL long long using namespace std; const int maxn 1e…

javaweb学习笔记(html常用标签,css样式,JavaScript基础)数据库约束)

JAVAWEB学习笔记 HTML 通过标签显示在网页的各个部分来显示内容 <!DOCTYPE html><!--声明约束--> <html lang"zh_CN"> <head><!--头部文件&#xff0c;一般包括标题&#xff0c;css样式&#xff0c;js代码--><meta charset"…

P1775 石子合并(弱化版)(区间dp模板)

石子合并&#xff08;弱化版&#xff09; - 洛谷 /** Description: To iterate is human, to recurse divine.* Autor: Recursion* Date: 2022-04-05 10:48:41* LastEditTime: 2022-04-05 11:52:33*/ #include <bits/stdc.h> #define LL long long using namespace std…

javaweb学习笔记2(jquery的使用,以及常用的方法,选择器,过滤器)

javaweb学习笔记2 javascript正则表达式 regfxp对象 方式1&#xff1a; var puttnew RegExp("e");//表示要求字符串中必须包含字符串evar str"abcde";alert(putt.test(str)); 方式2&#xff1a;var putt/e/;var str "abcde";alert(putt.test(…

P4702 取石子

取石子 - 洛谷 /** Description: To iterate is human, to recurse divine.* Autor: Recursion* Date: 2022-04-05 12:18:55* LastEditTime: 2022-04-05 12:21:26*/ #include <bits/stdc.h> #define LL long long using namespace std; const int maxn 1e6 10; const…

javaweb学习笔记3(tomcat安装,idea建立一个web工程,servlet常见方法和工具类)

javaweb学习笔记3 javaweb概念 javaweb指所有通过java语言编写的通过浏览器访问的程序的综合 javaweb是基于请求和响应来开发 请求&#xff1a; 是指客户端个服务器发送数据&#xff0c;request 响应&#xff1a;服务器给客户端回传数据为response web资源的分类 按实现技术…