blob存储文件示例程序

建表文件

create   table  A_F2DB
(
  TID     
VARCHAR2 ( 23 not   null ,
  A_FILEC CLOB,
  A_FILEB BLOB
)

WriteBlob.java

package  com.fsm.db;

import  java.sql. * ;
import  java.util.ArrayList;
import  java.io. * ;
import  oracle.sql. * ;

public   class  WriteBlob  {
 
-----------icepoint------------
 /**
  * 
@param strTID
  * 
@param tablename
  * 
@param filenames
  * 
@param blobcolumnname
  * 
@param KeyColumn
  * 
@return
  
*/

 
-----------icepoint------------
 public boolean writeblobtodb(String strTID, String tablename,
   String filenames, String blobcolumnname, String KeyColumn) 
{
  
boolean isok = false;

  
try {
   DriverManager.registerDriver(
new oracle.jdbc.driver.OracleDriver());
   Connection conn 
= DriverManager.getConnection(
     
"jdbc:oracle:thin:@localhost:1521:orcl""gj""gj");
   conn.setAutoCommit(
false);
   ReadPath readPath 
= new ReadPath();
   ArrayList list 
= readPath.okpath();
   File f 
= new File(filenames);
   FileInputStream fin 
= new FileInputStream(f);

   BLOB blob 
= null;
   PreparedStatement pstmt 
= conn.prepareStatement("insert into "
     
+ tablename + " (" + KeyColumn + "," + blobcolumnname
     
+ ") values(?,empty_blob())");
   pstmt.setString(
1, strTID);
   pstmt.executeUpdate();
   pstmt.close();

   pstmt 
= conn.prepareStatement("select " + blobcolumnname + " from "
     
+ tablename + " where " + KeyColumn + "=? for update");
   pstmt.setString(
1, strTID);
   ResultSet rset 
= pstmt.executeQuery();

   
if (rset.next())
    blob 
= (BLOB) rset.getBlob(1);

   pstmt 
= conn.prepareStatement("update " + tablename + " set "
     
+ blobcolumnname + "=? where " + KeyColumn + "='" + strTID
     
+ "'");

   OutputStream out 
= blob.getBinaryOutputStream();

   
int count = -1, total = 0;
   
byte[] data = new byte[(int) fin.available()];
   fin.read(data);
   out.write(data);

   fin.close();
   out.close();

   pstmt.setBlob(
1, blob);
   pstmt.executeUpdate();
   pstmt.close();

   System.out.println(
"插入成功!!!");
   isok 
= true;

   conn.commit();
   conn.close();

  }
 catch (SQLException e) {
   System.err.println(e.getMessage());
   e.printStackTrace();
  }
 catch (IOException e) {
   System.err.println(e.getMessage());
  }

  
return isok;

 }


 
public static void main(String[] args) {
  WriteBlob wb
=new WriteBlob();
  String strTID 
= "2007";
  String tablename 
= "A_F2DB";
  String filenames 
= "E:/chunye.jpg";
  String blobcolumnname 
= "A_FILEB";
  String KeyColumn 
= "TID";
  
  
if(wb.writeblobtodb(strTID,tablename,filenames,blobcolumnname,KeyColumn)){
   System.out.println(
"OK");
  }
else{
   System.out.println(
"FAILD");
  }

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值