教育行業(yè)A股IPO第一股(股票代碼 003032)

全國(guó)咨詢(xún)/投訴熱線:400-618-4000

JDBC處理CLOB數(shù)據(jù)和BLOB數(shù)據(jù)

更新時(shí)間:2023年09月22日15時(shí)11分 來(lái)源:傳智教育 瀏覽次數(shù):

好口碑IT培訓(xùn)

大數(shù)據(jù)處理主要指的是對(duì)CLOB和BLOB類(lèi)型數(shù)據(jù)的操作。在應(yīng)用程序中,要想操作這兩種數(shù)據(jù)類(lèi)型,必須使用PreparedStatement完成,并且所有的操作都要以IO流的形式進(jìn)行存放和讀取。下面將針對(duì)CLOB數(shù)據(jù)和BLOB數(shù)據(jù)的處理方式進(jìn)行詳細(xì)的介紹。

1. 處理CLOB數(shù)據(jù)

在實(shí)際開(kāi)發(fā)中,CLOB用于存儲(chǔ)大文本數(shù)據(jù),但是,對(duì)MySQL而言,大文本數(shù)據(jù)的存儲(chǔ)是用TEXT類(lèi)型表示的。為了幫助讀者更好地學(xué)習(xí)JDBC中CLOB數(shù)據(jù)的處理方式,下面通過(guò)一個(gè)案例來(lái)演示,具體步驟如下。

(1)首先在數(shù)據(jù)庫(kù)chapter01中,創(chuàng)建一個(gè)數(shù)據(jù)表testclob,創(chuàng)建表的SQL語(yǔ)句如下所示。

create table testclob(
	id int primary key auto_increment,
	resume text
);


(2) 在工程chapter01中,新建一個(gè)類(lèi)CLOBDemo01,該類(lèi)實(shí)現(xiàn)了向數(shù)據(jù)庫(kù)寫(xiě)入大文本數(shù)據(jù)的功能,CLOBDemo01的具體實(shí)現(xiàn)方式如例下所示。

CLOBDemo01.java

package cn.itcast.jdbc.example;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import cn.itcast.jdbc.example.utils.JDBCUtils;
public class CLOBDemo01{
    public static void main (string[] args)(
        Connection conn= null;
        PreparedStatement preStmt= nul1;
        try{
            conn=JDBCUtils.getConnection();
            String sql = "insert into testclob values(?,?)";
            preStmt=conn.prepareStatement(sql);
            File file = new File("D:\itcast.txt");
            Reader reader=new InputStreamReader(
                new FileInputstream(file),"utf-8");
            prestmt.setInt (1,1);
            preStmt.setCharacterStream(2, reader, (int) file.length());
            prestmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            //釋放資源
            JDBCUtils.release(null, preStmt, conn);
        }
    }
}

在上面案例中,由于文本數(shù)據(jù)保存在文件中,因此使用FileInputStream讀取文件中的數(shù)據(jù),然后通過(guò)PreparedStatement對(duì)象將數(shù)據(jù)寫(xiě)入到表testclob的resume字段中。

(3) 在工程chapter01中,新建一個(gè)類(lèi)CLOBDemo02,,類(lèi)用于讀取表testclob中的數(shù)據(jù),CLOBDemo2的具體實(shí)現(xiàn)方式如下所示。

package cn.itcast.jdbc.example;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import cn.itcast.jdbc.example.utils.JDBCUtils;
public class CLOBDemo02{
    public static void main(String[] args){
        Connection conn=null;
        PreparedStatement preStmt=null;
        ResultSet rs=null;
        try{
            conn = JDBCUtils.getConnection();
            String sql = "select * from testclob";
            preStmt = conn.prepareStatement(sql);
            rs = preStmt.executeQuery();
            if (rs.next()){
                Reader reader = rs.getCharacterStream("resume");
                Writer out = new FileWriter("resume.txt");
                int temp;
                while((temp=reader.read()) !=-1){
                    out.write(temp);
                }
                out.close();
                reader.close();
            } 
        }catch(Exception e){
            e.printStackTrace();
        } finally{
            //釋放資源
            JDBCUtils.release(rs, preStmt, conn);
        }
    }
}

在上面案例中,將PreparedStatement對(duì)象讀取到的數(shù)據(jù)保存到ResultSet中,然后通過(guò)循環(huán)的方式不斷把內(nèi)容取出來(lái),寫(xiě)人到resume.txt文件中。程序執(zhí)行完畢后,會(huì)在工程chapter01的根目錄下發(fā)現(xiàn)resume.txt文件。


2. 處理BLOB數(shù)據(jù)

BLOB類(lèi)型的操作與CLOB類(lèi)似,只 BLOB專(zhuān)門(mén)用于存放二進(jìn)制數(shù)據(jù),如圖片、電影等。為了幫助大家更好地學(xué)習(xí)BLOB數(shù)據(jù)的處理方式,接下來(lái),在D盤(pán)下保存一個(gè)itcast.jpg圖片,通過(guò)一個(gè)具體的案例來(lái)演示圖片的存儲(chǔ)和讀取,具體步驟如下。

(1) 首先在數(shù)據(jù)庫(kù)chapter01中,創(chuàng)建一個(gè)數(shù)據(jù)表testblob,創(chuàng)建表的SOL語(yǔ)句如下所示。

create table testblob(
    id int primary key auto_increment,
    img blob
);

(2) 在工程chapter01中,新建一個(gè)類(lèi)BLOBDemo01,該類(lèi)用于將圖片寫(xiě)人表testblob中,BLOBDemo01的具體實(shí)現(xiàn)代碼如下所示。

BLOBDemo01.java

package cn.itcast.jdbc.example;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import cn.itcast.jdbc.example.utils.JDBCUtils;
public class BLOBDemo01{
    public static void main (string[] args){
        Connection conn=null;
        Preparedstatement prestmt=null;
        try{
            conn=JDBCUtils.getConnection();
            String sql= "insert into testblob values (?,?)";
            prestmt = conn.prepareStatement(sql);
            prestmt .setInt(1,1);
            File file=new File("D:\itcast.jpg");
            InputStream in=new FileInputStream(file);
            prestmt.setBinaryStream(2, in,(int) file.length());
            prestmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            //釋放資源
            JDBCUtils.release(null,prestt,conn);
        }
    }
}

程序執(zhí)行后,圖片的信息就以二進(jìn)制的形式保存到表testblob中,如果直接使用SELECT語(yǔ)句查看表testblob中的數(shù)據(jù),則只能顯示一些二進(jìn)制數(shù)據(jù),圖片是無(wú)法顯示的。

(3) 在工程chapter01中,新建一個(gè)類(lèi)BLOBDemo02,該類(lèi)用于從數(shù)據(jù)庫(kù)中讀取要獲取的圖片,BLOBDemo02的具體實(shí)現(xiàn)方式如下所示。

BLOBDemo02.java

package cn.itcast.jdbc.example;
import java.io.*;
import java.sql.Connection;
import java.sql.Preparedstatement;
import java.sql.ResultSet;
import cn.itcast.jdbc.example.utils.JDBCUtils;
public class BLOBDemo02{
    public static void main(String [] args){
        Connection conn=null;
        PreparedStatement stmt=null;
        ResultSet rs=null;
        try{
            conn = JDBCUtils.getConnection();
            String sql = "select * from testblob where id=1";
            stmt = conn.prepareStatement(sql);
            rS = stmt.executeQuery();
            if (rs.next()){
                InputStream in=new BufferedInputStream(
                    rs.getBinaryStream("img"));
                    OutputStream out=new BufferedoutputStream(
                        new FileOutputStream ("img .jpg"));
                    int temp;
                    while((temp=in.read()) != -1){
                        Out.write (temp);
                    }
                    out.close();
                    in.close();
                )
            }catch (Exception e){
                e.printStackTrace();
            } finally{
                JDBCUtils.release(rs, stmt, conn);
            }
        }
    }
}

在上面案例中,使用PreparedStatement對(duì)象讀取數(shù)據(jù)宏中所右礎(chǔ)的見(jiàn)比出王詩(shī)取出來(lái)的圖片無(wú)法顯示,因此,將讀取出來(lái)的圖保存到img.jpg中。程序執(zhí)行完畢后,可以直接可以直接在工程的根目錄下發(fā)現(xiàn)img.jpg圖片。

0 分享到:
和我們?cè)诰€交談!