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鏁版嵁鐨勬搷浣滄暟鎹簱鐨勫垱寤轟唬鐮? |
|
* (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鍒嗛厤鏉冮檺 |
|
*(3)db2 => grant
select,insert,update,delete on |
*
table weihuachao.blobTest to user java |
* |
* |
*
@author weihuachao |
*
浠ヤ笅浠g爜鍦╩icrosoft 2003 緋葷粺,DB2 9.0涓祴璇曟垚鍔? |
* |
*/ |
|
//綾葷殑瀹氫箟寮濮?----------------------------------------------------------- |
|
public class Test { |
|
public static void main(String[] args)
throws Exception { |
|
Test test = new Test(); |
Connection conn = test.createConnection(); |
|
|
// Blob瀵硅薄鐨勬彃鍏ョ殑鏂規(guī)硶: |
try { |
|
|
// 鍒涘緩鎻掑叆璇彞. |
java.sql.PreparedStatement preparedStatement = conn |
.prepareStatement("insert into
weihuachao.blobTest(image)values(?)"); |
|
|
//鍒涘緩鏂囦歡瀵硅薄: |
|
File file=new File("c:/a.jpg"); |
|
|
// 鍒涘緩嫻佸璞? |
java.io.BufferedInputStream imageInput = new
java.io.BufferedInputStream( |
new java.io.FileInputStream(file)); |
|
|
//鍙傛暟璧嬪? |
preparedStatement.setBinaryStream(1, imageInput,(int) file.length()); |
|
|
//鎵ц璇彞 |
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瀵硅薄杞寲涓篒nputStream嫻?/td>
|
java.io.InputStream inputStream =blob.getBinaryStream(); |
|
|
//瑕佸啓鍏ョ殑鏂囦歡 |
File fileOutput = new File("c:/backa.jpg"); |
|
|
//鏂囦歡鐨勫啓鍏ユ祦鐨勫畾涔?/td>
|
FileOutputStream fo = new FileOutputStream(fileOutput); |
|
|
int c; |
//璇誨彇嫻佸茍鍐欏叆鍒版枃浠朵腑 |
while ((c = inputStream.read()) != -1) |
fo.write(c); |
|
|
//嫻佺殑鍏抽棴: |
fo.close(); |
|
} |
|
} catch (SQLException e) { |
|
|
// TODO 鑷姩鐢熸垚 catch 鍧?/td>
|
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 鍧?/td>
|
e.printStackTrace(); |
} |
|
} |
|
} |
|
/** |
* 瀹氫箟鏁版嵁搴撹繛鎺ョ殑鏂規(guī)硶 |
* |
* @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; |
} |
|
}//緇撴潫. |
|
|
|
鎬葷粨: JAVA
瀵笵B2涓殑BLOB瀵硅薄鐨勬搷浣?涓昏鏄嬌鐢ㄦ祦鐨勬妧鏈疄鐜?瀵笲LOB鐨勫疄鐜頒富瑕佹楠ゆ湁: |
|
(1)瀹氫箟瑕佸啓鍏ョ殑鏂囦歡 File file=new File("c:/a.jpg"); |
|
(2)瀹氫箟鏂囦歡鐨勫啓鍏ユ祦 |
|
java.io.BufferedInputStream imageInput = new
java.io.BufferedInputStream( |
new java.io.FileInputStream(file)); |
|
(3)浣跨敤鍑芥暟鍐欏叆: |
|
preparedStatement.setBinaryStream(1, imageInput,(int) file.length()); |
|
(4)鎵цSQL璇彞: |
|
瀵笲LOB鐨勮鍙栭噰鍙栫殑姝ラ鏈? |
|
(1)璇誨彇Blob鐨勫璞? Blob blob= (Blob) rs.getBlob(1); |
|
(2)鎶夿lob鐨勫璞¤漿鍖栦負(fù)嫻? |
|
java.io.InputStream inputStream =blob.getBinaryStream(); |
(3)瀹氫箟瑕佸啓鍏ョ殑鏂囦歡 |
File fileOutput = new File("c:/backa.jpg"); |
(4)鏂囦歡鐨勫啓鍏ユ祦鐨勫畾涔?/td>
|
FileOutputStream fo = new FileOutputStream(fileOutput); |
|
(5)鍐欏叆鏂囦歡(嫻佺殑鍐欏叆鎶鏈?灝變笉澶氳浜? |
int c; |
while ((c = inputStream.read()) != -1) |
|
{ |
fo.write(c); |
|
} |
//END |
|