更新時間:2021-10-28 來源:黑馬程序員 瀏覽量:
大數(shù)據(jù)處理主要指的是對CLOB和BLOB類型數(shù)據(jù)的操作。在應(yīng)用程序中,要想操作這兩種數(shù)據(jù)類型,必須使用PreparedStatement完成,并且所有的操作都要以IO流的形式進(jìn)行存放和讀取。下面將針對CLOB數(shù)據(jù)和BLOB數(shù)據(jù)的處理方式進(jìn)行詳細(xì)的介紹。
在實(shí)際開發(fā)中,CLOB用于存儲大文本數(shù)據(jù),但是,對MySQL而言,大文本數(shù)據(jù)的存儲是用TEXT類型表示的。為了幫助讀者更好地學(xué)習(xí)JDBC中CLOB數(shù)據(jù)的處理方式,下面通過一個案例來演示,具體步驟如下。
(1)首先在數(shù)據(jù)庫chapter01中,創(chuàng)建一個數(shù)據(jù)表testclob,創(chuàng)建表的SQL語句如下所示。
create table testclob( id int primary key auto_increment, resume text );
(2) 在工程chapter01中,新建一個類CLOBDemo01,該類實(shí)現(xiàn)了向數(shù)據(jù)庫寫入大文本數(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ù),然后通過PreparedStatement對象將數(shù)據(jù)寫入到表testclob的resume字段中。
(3) 在工程chapter01中,新建一個類CLOBDemo02,,類用于讀取表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對象讀取到的數(shù)據(jù)保存到ResultSet中,然后通過循環(huán)的方式不斷把內(nèi)容取出來,寫人到resume.txt文件中。程序執(zhí)行完畢后,會在工程chapter01的根目錄下發(fā)現(xiàn)resume.txt文件。
BLOB類型的操作與CLOB類似,只 BLOB專門用于存放二進(jìn)制數(shù)據(jù),如圖片、電影等。為了幫助大家更好地學(xué)習(xí)BLOB數(shù)據(jù)的處理方式,接下來,在D盤下保存一個itcast.jpg圖片,通過一個具體的案例來演示圖片的存儲和讀取,具體步驟如下。
(1) 首先在數(shù)據(jù)庫chapter01中,創(chuàng)建一個數(shù)據(jù)表testblob,創(chuàng)建表的SOL語句如下所示。
create table testblob(
id int primary key auto_increment,
img blob
);
(2) 在工程chapter01中,新建一個類BLOBDemo01,該類用于將圖片寫人表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語句查看表testblob中的數(shù)據(jù),則只能顯示一些二進(jìn)制數(shù)據(jù),圖片是無法顯示的。
(3) 在工程chapter01中,新建一個類BLOBDemo02,該類用于從數(shù)據(jù)庫中讀取要獲取的圖片,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對象讀取數(shù)據(jù)宏中所右礎(chǔ)的見比出王詩取出來的圖片無法顯示,因此,將讀取出來的圖保存到img.jpg中。程序執(zhí)行完畢后,可以直接可以直接在工程的根目錄下發(fā)現(xiàn)img.jpg圖片。
猜你喜歡