package net.qysoft; |
|
import java.io.File; |
import java.io.FileOutputStream; |
import java.sql.Blob; |
import java.sql.Connection; |
import java.sql.SQLException; |
|
|
/** |
*
java 中對Blob數(shù)據(jù)的操作數(shù)據(jù)庫的創(chuàng)建代碼: |
|
* (1)db2 => create table blobTest (
id int not null generated always as identity, image blob ) |
|
*添加用戶java,密碼java |
|
*
(2) db2=>! net |
*
user java java /add java分配權(quán)限 |
|
*(3)db2 => grant
select,insert,update,delete on |
*
table weihuachao.blobTest to user java |
* |
* |
*
@author weihuachao |
*
以下代碼在microsoft 2003 系統(tǒng),DB2 9.0中測試成功. |
* |
*/ |
|
//類的定義開始------------------------------------------------------------ |
|
public class Test { |
|
public static void main(String[] args)
throws Exception { |
|
Test test = new Test(); |
Connection conn = test.createConnection(); |
|
|
// Blob對象的插入的方法: |
try { |
|
|
// 創(chuàng)建插入語句. |
java.sql.PreparedStatement preparedStatement = conn |
.prepareStatement("insert into
weihuachao.blobTest(image)values(?)"); |
|
|
//創(chuàng)建文件對象: |
|
File file=new File("c:/a.jpg"); |
|
|
// 創(chuàng)建流對象: |
java.io.BufferedInputStream imageInput = new
java.io.BufferedInputStream( |
new java.io.FileInputStream(file)); |
|
|
//參數(shù)賦值: |
preparedStatement.setBinaryStream(1, imageInput,(int) file.length()); |
|
|
//執(zhí)行語句 |
preparedStatement.executeUpdate(); |
|
|
|
//------------------------------------------------------------------ |
//Blob的讀取工作: |
|
|
java.sql.Statement st=conn.createStatement(); |
|
|
java.sql.ResultSet rs=st.executeQuery("select image from
weihuachao.blobTest"); |
|
|
while(rs.next()) |
{ |
//讀取Blob對象 |
Blob blob= (Blob) rs.getBlob(1); |
|
|
//Blob對象轉(zhuǎn)化為InputStream流 |
java.io.InputStream inputStream =blob.getBinaryStream(); |
|
|
//要寫入的文件 |
File fileOutput = new File("c:/backa.jpg"); |
|
|
//文件的寫入流的定義 |
FileOutputStream fo = new FileOutputStream(fileOutput); |
|
|
int c; |
//讀取流并寫入到文件中 |
while ((c = inputStream.read()) != -1) |
fo.write(c); |
|
|
//流的關(guān)閉: |
fo.close(); |
|
} |
|
} catch (SQLException e) { |
|
|
// TODO 自動生成 catch 塊 |
e.printStackTrace(); |
}catch(java.io.FileNotFoundException ex) |
|
{ |
|
ex.printStackTrace(); |
}catch(java.io.IOException ex) |
{ |
|
ex.printStackTrace(); |
} |
finally |
{ |
try { |
conn.close(); |
} catch (SQLException e) { |
|
|
// TODO 自動生成 catch 塊 |
e.printStackTrace(); |
} |
|
} |
|
} |
|
/** |
* 定義數(shù)據(jù)庫連接的方法 |
* |
* @return |
*/ |
private Connection createConnection() { |
Connection conn = null; |
try { |
|
|
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver"); |
|
conn =
java.sql.DriverManager.getConnection("jdbc:db2:sample", |
"java", "java"); |
|
|
} catch (SQLException ex1) { |
ex1.printStackTrace(); |
|
|
} catch (ClassNotFoundException ex) { |
ex.printStackTrace(); |
} |
return conn; |
} |
|
}//結(jié)束. |
|
|
|
總結(jié): JAVA
對DB2中的BLOB對象的操作,主要是使用流的技術(shù)實現(xiàn).對BLOB的實現(xiàn)主要步驟有: |
|
(1)定義要寫入的文件 File file=new File("c:/a.jpg"); |
|
(2)定義文件的寫入流 |
|
java.io.BufferedInputStream imageInput = new
java.io.BufferedInputStream( |
new java.io.FileInputStream(file)); |
|
(3)使用函數(shù)寫入: |
|
preparedStatement.setBinaryStream(1, imageInput,(int) file.length()); |
|
(4)執(zhí)行SQL語句: |
|
對BLOB的讀取采取的步驟有: |
|
(1)讀取Blob的對象: Blob blob= (Blob) rs.getBlob(1); |
|
(2)把Blob的對象轉(zhuǎn)化為流: |
|
java.io.InputStream inputStream =blob.getBinaryStream(); |
(3)定義要寫入的文件 |
File fileOutput = new File("c:/backa.jpg"); |
(4)文件的寫入流的定義 |
FileOutputStream fo = new FileOutputStream(fileOutput); |
|
(5)寫入文件(流的寫入技術(shù),就不多講了) |
int c; |
while ((c = inputStream.read()) != -1) |
|
{ |
fo.write(c); |
|
} |
//END |
|