JDBC处理大数据(大文本的存储和读取)

JDBC处理大数据(大文本的存储和读取)

1. 基本概念:大数据也称之为LOB(Large Objects),LOB又分为:clob和blob

•   clob用于存储大文本。Text

•   blob用于存储二进制数据,例如图像、声音、二进制文等。

对MySQL而言只有blob,而没有clob,mysql存储大文本采用的是Text,Text和blob分别又分为:

•   TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT

•   TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB

 

先让我们做一些准备工作,

准备工作

1.建表testclob

creat database idbc;

user jdbc;

create table testclob(

id intprimary key aut_incerment,

resume text

)

2.载入jdbc驱动

3.建立连接

建立com.hbsi.utils包,DBManager类载入驱动

建立连接以及资源的释放

4.Demo1.java

package com.hbsi.test;

 

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.Reader;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import com.hbsi.utils.DBManager;

 

public class Demo1 {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

       // TODO Auto-generatedmethod stub

       Demo1 obj=new Demo1();

       //obj.insert();

       obj.find();

    }

    //大文本数据的存储

    public void insert(){

       Connection con=null;

       PreparedStatement st=null;

       ResultSet rs=null;

      

       try {

           con=DBManager.getConnection();

           String sql="insert intotestclob (resume) value(?)";

           st=con.prepareStatement(sql);

           //流

           //Stringpath=Demo1.class.getClassLoader().getResource("readerme.txt").getPath();

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

           st.setCharacterStream(1, new FileReader(f),f.length());//数字1,表示占位符自动读取1.txt文件中内容存入数据的表中

           int result=st.executeUpdate();

           if(result>0){

              System.out.println("ok");

           }

           else{

              System.out.println("no");

           }

                    

       } catch (SQLException e) {

           // TODO Auto-generatedcatch block

           e.printStackTrace();

       } catch (FileNotFoundException e) {

           // TODO Auto-generatedcatch block

           e.printStackTrace();

       }finally{

           DBManager.release(con, st, rs);

       }

      

    }

    public void find(){

       Connection con=null;

       PreparedStatement st=null;

       ResultSet rs=null;

      

       try {

           con=DBManager.getConnection();

           String sql="selectresume from testclob where id=1";

           st=con.prepareStatement(sql);

           rs=st.executeQuery();

           if(rs.next()){

              //这样容易电脑出现死机

              //String str=rs.getString("resume");

              //System.out.println(str);

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

              char buff[]=new char[1024];

              int len=0;

              FileWriter writer=new FileWriter("3.txt");

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

                  //System.out.println(newString(buff,0,len));

                  writer.write(buff,0,len);

              }

              reader.close();

           }

                    

        } catch (SQLException e) {

           // TODO Auto-generatedcatch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generatedcatch block

           e.printStackTrace();

       }

       finally{

           DBManager.release(con, st, rs);

       }

    }

   

 

}

 

 

你可能感兴趣的:(sql,jdbc,String,null,存储,insert)