使用JDBC处理大数据(大文本/二进制数据)

本文介绍如何使用JDBC处理大数据,包括大文本(CLOB)和二进制数据(BLOB)。详细展示了插入和读取这两种类型数据的具体步骤,并提供了完整的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

使用JDBC处理大数据(大文本/二进制数据):

大数据(LOB):clod和blob

MySql中存储大文本只能用text

二进制数据用blob:图像。图片、声音等

 

一、举例向数据库中插入和读取大文本数据:

准备:

(1)从数据库中建表       表名为testclob

    create database jdbc;

    user jdbc;

    create table testclob

    (

       id int primary key auto_increment;

       resume text

    );

(2)jdbc驱动

(3)建立com.hbsi.utils包 ,DBManager类---载入驱动

(4)在项目中放置一个文本文件1.txt

编写代码:Demo1.java实现

// 大文本数据的存储

    public void insert() {

       Connection con = null;

       PreparedStatement st = null;

       ResultSet rs = null;

      

       try {

           //获取连接

           con = DBManager.getConnection();

           //定义sql语句

           String sql = "insert into testclob (resume) value(?)";

           //创建预处理对象

           st = con.prepareStatement(sql);

           File f = new File("1.txt");

           //系统会自动的读取1.txt文件中的内容并放到流中

           st.setCharacterStream(1, new FileReader(f),f.length());

           //执行更新语句

           int result = st.executeUpdate();

           if(result>0){

              System.out.println("插入成功");

           }else{

              System.out.println("插入失败");

           }

      

           DBManager.release(con, st, rs);

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

   

    }

 

    //大文本数据的读取

    public void find(){

       Connection con = null;

       PreparedStatement st = null;

       ResultSet rs = null;

       try {

           //获取连接

           con = DBManager.getConnection();

           //定义sql语句

           String sql = "select resume from testclob where id=1";

           //创建预处理对象

           st = con.prepareStatement(sql);

           //执行更新

           rs = st.executeQuery();

           if(rs.next()){

              //如果文本太大,会死机

              //放到流中

              Reader reader = rs.getCharacterStream("resume");

              //创建缓冲区

              char buff[] = new char[1024];

              //通过循环的方法读取

              int len = 0;

              while((len = reader.read(buff))>0){

                  //读取后放到文件中

                  System.out.println(new String(buff,0,len));

              }

          

           }

           DBManager.release(con, st, rs);

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

 

二、二进制数据的插入和读取:

// 把大二进制储存刀数据库中

    public void insert() {

       Connection con = null;

       PreparedStatement st = null;

       ResultSet rs = null;

       try {

           // 获取连接

           con = DBManager.getConnection();

           // 定义sql语句

           String sql = "insert into testblob (image) value(?)";

           // 创建预处理对象

           st = con.prepareStatement(sql);

      

           File f = new File("1.jpg");

           st.setBinaryStream(1, new FileInputStream(f), f.length());

           // 执行更新语句

           int result = st.executeUpdate();

           // 判断

           if (result > 0) {

              System.out.println("插入成功");

           } else {

              System.out.println("插入失败");

           }

 

           DBManager.release(con, st, rs);

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

 

    // 大二进制数据的读取

    public void find() {

       Connection con = null;

       PreparedStatement st = null;

       ResultSet rs = null;

 

       try {

 

           // 获取连接

           con = DBManager.getConnection();

           // 定义sql语句

           String sql = "select image from testblob where id=1";

           // 创建预处理对象

           st = con.prepareStatement(sql);

           // 执行更新

           rs = st.executeQuery();

           //判断

           if (rs.next()) {

              //读取

              InputStream fis =  rs.getBinaryStream("image");

              //定义缓冲区,读取到缓冲区 中

              byte[] buff = new byte[1024];

              int len= 0;

              FileOutputStream fos = new FileOutputStream("3.jpg");

              while((len=fis.read(buff))>0){

                  fos.write(buff,0,len);

              }

           }

           DBManager.release(con, st, rs);

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值