大数据的存储和读取

直接贴代码

package com.wnl.clob_blob;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import com.utils.JdbcUtil;

public class ClobDemo {


    @Test//写入大数据
    public void test1(){
        Connection conn=null;
        PreparedStatement stmt=null;
        try {
             conn=JdbcUtil.getConnection();
             stmt=conn.prepareStatement("insert into a (name,number) values(?,?)");
             stmt.setInt(2, 100);
             //取出大数据内容
             File file=new File("src/myclob");
             Reader reader=new FileReader(file);
             stmt.setCharacterStream(1, reader,(int)file.length());
             stmt.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            JdbcUtil.release(conn, null, stmt);
        }
    }

    @Test
    public void test2(){
        Connection conn=null;
        PreparedStatement stmt=null;
        ResultSet rs=null;
        try {
            conn=JdbcUtil.getConnection();
            stmt=conn.prepareStatement("select * from a where id=?");
            stmt.setInt(1, 19);
            rs=stmt.executeQuery();
            while(rs.next()){
              Reader reader= rs.getCharacterStream("name");
              char[] chuff=new char[1024];
              int len=0;
              if((len=reader.read(chuff))!=-1){
                  System.out.println("获取大数据文本====");
                  String name=new String(chuff, 0, len);
                  System.out.println(name);
              }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

你可能感兴趣的:(大数据的存储和读取)