BLOB (binary large object),二進制大對象,是一個可以存儲二進制檔案的容器。在計算機中,BLOB常常是數據庫中用來存儲二進制檔案的字段類型,BLOB是一個大檔案,典型的BLOB是一張圖片或一個聲音檔案,由於它們的尺寸,必須使用特殊的方式來處理(例如:上傳、下載或者存放到一個數據庫)。
一、基本概念
在實際開發中,有時是需要用程序把大文字或二進制數據直接儲存到數據庫中進行儲存的。
對MySQL而言只有blob,而沒有clob,mysql存儲大文字採用的是Text,Text和blob分別又分爲:
TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT
TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB
二、搭建測試環境
2.1、搭建的測試項目架構
2.2、編寫erties配置檔案
driver=erurl=jdbc:mysql://localhost:3306/jdbcStudyusername=rootpassword=XDP
2.3、編寫JdbcUtils工具類
package s;import tStream;import ection;import erManager;import ltSet;import xception;import ement;import erties;public class JdbcUtils { private static String driver = null; private static String url = null; private static String username = null; private static String password = null; static{ try{ //讀取erties檔案中的數據庫連接資訊 InputStream in = lassLoader()esourceAsStream("erties"); Properties prop = new Properties(); (in); //獲取數據庫連接驅動 driver = roperty("driver"); //獲取數據庫連接URL地址 url = roperty("url"); //獲取數據庫連接用戶名 username = roperty("username"); //獲取數據庫連接密碼 password = roperty("password"); //加載數據庫驅動 ame(driver); }catch (Exception e) { throw new ExceptionInInitializerError(e); } } /** * @Method: getConnection * @Description: 獲取數據庫連接對象 * @Anthor:孤傲蒼狼 * * @return Connection數據庫連接對象 * @throws SQLException */ public static Connection getConnection() throws SQLException{ return onnection(url, username,password); } /** * @Method: release * @Description: 釋放資源, * 要釋放的資源包括Connection數據庫連接對象,負責執行SQL命令的Statement對象,存儲查詢結果的ResultSet對象 * @Anthor:孤傲蒼狼 * * @param conn * @param st * @param rs */ public static void release(Connection conn,Statement st,ResultSet rs){ if(rs!=null){ try{ //關閉存儲查詢結果的ResultSet對象 e(); }catch (Exception e) { tStackTrace(); } rs = null; } if(st!=null){ try{ //關閉負責執行SQL命令的Statement對象 e(); }catch (Exception e) { tStackTrace(); } } if(conn!=null){ try{ //關閉Connection數據庫連接對象 e(); }catch (Exception e) { tStackTrace(); } } }}
三、使用JDBC處理MySQL的大文字
對於MySQL中的Text類型,可調用如下方法設定
haracterStream(index, reader, length);//注意length長度須設定,並且設定爲int型
對MySQL中的Text類型,可調用如下方法獲取
reader = resultSet. getCharacterStream(String columnLabel);2 string s = tring(String columnLabel);
3.1、 測試範例
1、編寫SQL測試腳本
create database jdbcstudy;use jdbcstudy;create table testclob( id int primary key auto_increment, resume text);
2、編寫測試代碼如下:
package ;import ;import Reader;import Writer;import er;import ection;import aredStatement;import ltSet;import Utils;import ;/*** @ClassName: JdbcOperaClob* @Description: 使用JDBC操作MySQL的大文字* @author: 孤傲蒼狼* @date: 2014-9-19 下午10:10:04**/ public class JdbcOperaClob { /** * @Method: add * @Description:向數據庫中插入大文字數據 * @Anthor:孤傲蒼狼 * */ @Test public void add(){ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; Reader reader = null; try{ conn = onnection(); String sql = " into testclob(resume) values("; st = areStatement(sql); //這種方式獲取的路徑,其中的空格會被使用“”代替 String path = lassLoader()esource("")ath(); //將“”替換回空格 path = aceAll("", " "); File file = new File(path); reader = new FileReader(file); haracterStream(1, reader,(int) th()); int num = uteUpdate(); if(num>0){ tln("插入成功!!"); } //關閉流 e(); }catch (Exception e) { tStackTrace(); }finally{ ase(conn, st, rs); } } /** * @Method: read * @Description: 讀取數據庫中的.大文字數據 * @Anthor:孤傲蒼狼 * */ @Test public void read(){ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try{ conn = onnection(); String sql = "select resume from testclob where id=2"; st = areStatement(sql); rs = uteQuery(); String contentStr =""; String content = ""; if(()){ //使用tring("字段名")獲取大文字數據的內容 content = tring("resume"); //使用haracterStream("字段名")獲取大文字數據的內容 Reader reader = haracterStream("resume"); char buffer[] = new char[1024]; int len = 0; FileWriter out = new FileWriter("D:"); while((len=(buffer))>0){ contentStr += new String(buffer); e(buffer, 0, len); } e(); e(); } tln(content); tln("-----------------------------------------------"); tln(contentStr); }catch (Exception e) { tStackTrace(); }finally{ ase(conn, st, rs); } }}
四、使用JDBC處理MySQL的二進制數據
對於MySQL中的BLOB類型,可調用如下方法設定:
PreparedStatement. setBinaryStream(i, inputStream, length);
對MySQL中的BLOB類型,可調用如下方法獲取:
InputStream in = inaryStream(String columnLabel);InputStream in = lob(String columnLabel)inaryStream();
4.1、 測試範例
1、編寫SQL測試腳本
create table testblob( id int primary key auto_increment, image longblob );
2、編寫測試代碼如下:
package ;import ;import InputStream;import OutputStream;import tStream;import ection;import aredStatement;import ltSet;import Utils;import ;/*** @ClassName: JdbcOperaClob* @Description: 使用JDBC操作MySQL的二進制數據(例如圖像、聲音、二進制文)* @author: 孤傲蒼狼* @date: 2014-9-19 下午10:10:04**/ public class JdbcOperaBlob { /** * @Method: add * @Description:向數據庫中插入二進制數據 * @Anthor:孤傲蒼狼 * */ @Test public void add(){ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try{ conn = onnection(); String sql = " into testblob(image) values("; st = areStatement(sql); //這種方式獲取的路徑,其中的空格會被使用“”代替 String path = JdbcOperaBlob.class.getClassLoader().getResource("01.jpg")ath(); //將“”替換會空格 path = aceAll("", " "); File file = new File(path); FileInputStream fis = new FileInputStream(file);//生成的流 inaryStream(1, fis,(int) th()); int num = uteUpdate(); if(num>0){ tln("插入成功!!"); } e(); }catch (Exception e) { tStackTrace(); }finally{ ase(conn, st, rs); } } /** * @Method: read * @Description: 讀取數據庫中的二進制數據 * @Anthor:孤傲蒼狼 * */ @Test public void read() { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = onnection(); String sql = "select image from testblob where id="; st = areStatement(sql); nt(1, 1); rs = uteQuery(); if (()) { //InputStream in = lob("image")inaryStream();//這種方法也可以 InputStream in = inaryStream("image"); int len = 0; byte buffer[] = new byte[1024]; FileOutputStream out = new FileOutputStream("D:1.jpg"); while ((len = (buffer)) > 0) { e(buffer, 0, len); } e(); e(); } } catch (Exception e) { tStackTrace(); } finally { ase(conn, st, rs); } }}