JAVA筆記:Java資料庫編程(四):處理大資料對象

來源:互聯網
上載者:User

標籤:

大資料對象主要指的是CLOB和BLOB兩種類型的欄位,在CLOB中可以儲存海量文字,例如儲存一本小說。在BLOB中可以儲存二進位檔案,如電影,圖片等,如果在程式中要處理大的資料對象,則必須使用PreparedStatement完成,所有的內容要通過IO流的方式從大欄位文本中儲存和讀取。



寫入大資料對象的主要方法:


讀取大資料對象的主要方法:



處理CLOB資料

CLOB表示大文本資料(Character Large Object),在MySQL中提供了LONGTEXT欄位表示大文本資料,這個欄位的最大儲存容量問4G。

可以通過IO流的方式讀取檔案內容:

import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.PreparedStatement ;import java.sql.ResultSet ;import java.io.File ;import java.io.FileInputStream ;import java.io.InputStream ;import java.util.Scanner ;public class ClobDemo02{// 定義MySQL的資料庫驅動程式public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;// 定義MySQL資料庫的串連地址public static final String DBURL = "jdbc:mysql://localhost:3306/root" ;// MySQL資料庫的串連使用者名稱public static final String DBUSER = "root" ;// MySQL資料庫的串連密碼public static final String DBPASS = "mysqladmin" ;public static void main(String args[]) throws Exception{// 所有異常拋出Connection conn = null ;// 資料庫連接PreparedStatement pstmt = null ;ResultSet rs = null ;int id = 1 ;// 讀取的編號String sql = "SELECT name,note FROM userclob WHERE id=? " ;Class.forName(DBDRIVER) ;// 載入驅動程式conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;pstmt = conn.prepareStatement(sql) ;// 建立PreapredStatement對象pstmt.setInt(1,id) ;rs = pstmt.executeQuery() ;if(rs.next()){String name = rs.getString(1) ;StringBuffer note = new StringBuffer() ;System.out.println("姓名:" + name) ;InputStream input = rs.getAsciiStream(2) ;Scanner scan = new Scanner(input) ;// 使用Scanner類讀取內容scan.useDelimiter("\r\n") ;// 將檔案換行作為分割符while(scan.hasNext()){note.append(scan.next()).append("\n") ;}System.out.println("內容:" + note) ;input.close() ;}rs.close() ;pstmt.close() ;conn.close() ;// 資料庫關閉}};


CLOB類:

也可以使用ResultSet中的getClob()方法,將全部的內容變為Clob對象的內容,直接使用Clob可以很方便的取得大資料的文本,也可以對文本資料進行簡單的操作。

import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.PreparedStatement ;import java.sql.Clob ;import java.sql.ResultSet ;public class ClobDemo03{// 定義MySQL的資料庫驅動程式public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;// 定義MySQL資料庫的串連地址public static final String DBURL = "jdbc:mysql://localhost:3306/root" ;// MySQL資料庫的串連使用者名稱public static final String DBUSER = "root" ;// MySQL資料庫的串連密碼public static final String DBPASS = "mysqladmin" ;public static void main(String args[]) throws Exception{// 所有異常拋出Connection conn = null ;// 資料庫連接PreparedStatement pstmt = null ;ResultSet rs = null ;int id = 1 ;// 讀取的編號String sql = "SELECT name,note FROM userclob WHERE id=? " ;Class.forName(DBDRIVER) ;// 載入驅動程式conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;pstmt = conn.prepareStatement(sql) ;// 建立PreapredStatement對象pstmt.setInt(1,id) ;rs = pstmt.executeQuery() ;if(rs.next()){String name = rs.getString(1) ;System.out.println("姓名:" + name) ;Clob c = rs.getClob(2) ;String note = c.getSubString(1,(int)c.length()) ;System.out.println("內容:" + note ) ;c.truncate(100) ;// 只能讀100個內容System.out.println("部分讀取內容:" + c.getSubString(1,(int)c.length())) ;}rs.close() ;pstmt.close() ;conn.close() ;// 資料庫關閉}};

處理BLOB資料BLOB資料和CLOB資料很相似,專門用來存放位元據,在MySQL中使用LONGBLOB來聲明,最高可以儲存4G大小的內容。

IO流讀取:

import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.PreparedStatement ;import java.io.File ;import java.io.FileOutputStream ;import java.sql.ResultSet ;import java.io.InputStream ;import java.io.InputStream ;import java.io.OutputStream ;public class BlobDemo02{// 定義MySQL的資料庫驅動程式public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;// 定義MySQL資料庫的串連地址public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;// MySQL資料庫的串連使用者名稱public static final String DBUSER = "root" ;// MySQL資料庫的串連密碼public static final String DBPASS = "mysqladmin" ;public static void main(String args[]) throws Exception{// 所有異常拋出Connection conn = null ;// 資料庫連接PreparedStatement pstmt = null ;ResultSet rs = null ;int id = 1 ;String sql = "SELECT name,photo FROM userblob WHERE id=?" ;Class.forName(DBDRIVER) ;// 載入驅動程式conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;pstmt = conn.prepareStatement(sql) ;pstmt.setInt(1,id) ;rs = pstmt.executeQuery() ;// 執行查詢if(rs.next()){String name = rs.getString(1) ;System.out.println("姓名:" + name) ;InputStream input = rs.getBinaryStream(2) ;File f = new File("d:" + File.separator + "loadmldn.gif") ;// 圖片檔案OutputStream out = null ;out = new FileOutputStream(f) ;int temp = 0 ;while((temp=input.read())!=-1){// 邊讀邊寫out.write(temp) ;}input.close() ;out.close() ;}pstmt.close() ;conn.close() ;// 資料庫關閉}};

也可以通過BLOB類進行操作:

import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.PreparedStatement ;import java.sql.Blob ;import java.sql.ResultSet ;import java.io.File ;import java.io.FileOutputStream ;import java.io.InputStream ;import java.io.InputStream ;import java.io.OutputStream ;public class BlobDemo03{// 定義MySQL的資料庫驅動程式public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;// 定義MySQL資料庫的串連地址public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;// MySQL資料庫的串連使用者名稱public static final String DBUSER = "root" ;// MySQL資料庫的串連密碼public static final String DBPASS = "mysqladmin" ;public static void main(String args[]) throws Exception{// 所有異常拋出Connection conn = null ;// 資料庫連接PreparedStatement pstmt = null ;ResultSet rs = null ;int id = 1 ;String sql = "SELECT name,photo FROM userblob WHERE id=?" ;Class.forName(DBDRIVER) ;// 載入驅動程式conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;pstmt = conn.prepareStatement(sql) ;pstmt.setInt(1,id) ;rs = pstmt.executeQuery() ;// 執行查詢if(rs.next()){String name = rs.getString(1) ;System.out.println("姓名:" + name) ;Blob b = rs.getBlob(2) ;File f = new File("d:" + File.separator + "loadmldn.gif") ;// 圖片檔案OutputStream out = null ;out = new FileOutputStream(f) ;out.write(b.getBytes(1,(int)b.length())) ;out.close() ;}pstmt.close() ;conn.close() ;// 資料庫關閉}};


這篇主要總結了下Java資料庫中對大資料對象的操作,但是從實際使用和開發的過程來看,把太大的檔案放在資料庫中是一個不明智的選擇,資料過大時往往是採用映射路徑的方式寫入的。





JAVA筆記:Java資料庫編程(四):處理大資料對象

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.