表:
create table msx0504( id number(12), filename nvarchar2(20), content blob, primary key(id) );
代码:
package com.hy.lab.blob2; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class ContinuousBlobWriter { //-- 以下为连接Oracle数据库的四大参数 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; private static final String USER = "luna"; private static final String PSWD = "1234"; public static void main(String[] args){ Connection conn = null; PreparedStatement pstmt = null; try { Class.forName(DRIVER); conn = DriverManager.getConnection(URL, USER, PSWD); String insertSql="insert into msx0504(id,filename,content) values(?,?,?)"; pstmt = conn.prepareStatement(insertSql); File dir=new File("C:\\temp1\\msx_src_26"); File[] files=dir.listFiles(); int idx=0; for(File file:files){ InputStream is=new FileInputStream(file.getPath()); pstmt.setLong(1,idx); pstmt.setString(2,file.getName()); pstmt.setBlob(3,is); pstmt.executeUpdate(); System.out.println("写入文件"+file.getName()); idx++; } } catch (Exception e) { e.printStackTrace(); } finally { try { pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
查询结果:
SQL> select id,filename,length(content) as len from msx0504; ID FILENAME LEN ---------- ---------------------------------------- ---------- 0 1.jpg 230161 1 10.jpg 211711 2 11.jpg 310264 3 12.jpg 242226 4 13.jpg 194538 5 14.jpg 225705 6 15.jpg 218547 7 16.jpg 196176 8 17.jpg 227857 9 18.jpg 211692 10 19.jpg 142678 ID FILENAME LEN ---------- ---------------------------------------- ---------- 11 2.jpg 212361 12 20.jpg 195689 13 21.jpg 160843 14 22.jpg 221536 15 23.jpg 187784 16 24.jpg 211107 17 25.jpg 178949 18 26.jpg 166578 19 3.jpg 201039 20 4.jpg 248252 21 5.jpg 189575 ID FILENAME LEN ---------- ---------------------------------------- ---------- 22 6.jpg 279448 23 7.jpg 198189 24 8.jpg 199466 25 9.jpg 211237 已选择26行。
END