oracle BLOB 读写

BLOB
     /**
     * 写文件到BLOB中
     * @param FileName
     */
    public void testSaveFiletoBlob(String FileName){
        try
        {
           
            File file = new File(FileName) ;          
            //java.io.FileReader fr=new java.io.FileReader(file);    
            java.io.FileInputStream fr=new java.io.FileInputStream(file);
           
            oracle.jdbc.OracleConnection  conn=(oracle.jdbc.OracleConnection)getConnection();  
            //0) 先删除
            OraclePreparedStatement  pst=(OraclePreparedStatement)conn.prepareStatement("delete from tKHDZDPRINT where" +
                    " theyear=? and theseason=? and khh=?");
            pst.setInt(1, Integer.valueOf(2007));
            pst.setInt(2, Integer.valueOf(1));
            pst.setString(3, "1094103");
            pst.executeUpdate();           
            conn.commit();
            pst.close();
            //1)插入数据
            pst=(OraclePreparedStatement)conn.prepareStatement("insert into tKHDZDPRINT(theyear,theseason,khh,printfile) values(" +
                    "?,?,?,empty_blob())");
            pst.setInt(1, Integer.valueOf(2007));
            pst.setInt(2, Integer.valueOf(1));
            pst.setString(3, "1094103");
            pst.executeUpdate();           
            conn.commit();
            pst.close();
            //2)锁定和读取内容
            pst=(OraclePreparedStatement)conn.prepareStatement("select printfile from tkhdzdprint where theyear=2007 and " +
                    " theseason=1 and khh='1094103' for update");
            java.sql.ResultSet rs=pst.executeQuery();
            oracle.sql.BLOB c=null;
            if (rs.next()){
                c=(oracle.sql.BLOB)rs.getBlob(1);
            }
            pst.close();
            //3)更新内容           
            java.io.OutputStream writer;    
            byte[] cbuf=new byte[1024];           
            writer=c.setBinaryStream(1L);
           
            Integer offset=Integer.valueOf(0);
            Integer iRead;           
            iRead=fr.read(cbuf,offset,1024);                      
            while (iRead.compareTo(-1)!=0){     
                //writer.write(cbuf);   
                writer.write(cbuf,0, iRead);               
                iRead=fr.read(cbuf,offset,1024);                 
            }                         
            writer.flush();
            writer.close();
            pst=(OraclePreparedStatement)conn.prepareStatement("update tKHDZDPRINT set printfile=? where theyear=2007 and " +
                    " theseason=1 and khh='1094103'");
            pst.setBLOB(1, c);
            pst.executeUpdate();
            conn.commit();
            pst.close();
        }
        catch (java.io.IOException e){
            return;
        }
        catch (SQLException e){
            return;
        }       
        catch (java.lang.ClassNotFoundException e){
            return;
        }
    }
    -------读blob----------------------------------------------------------------------------------------------------------------------
    /**
     * 测试从数据读取blob,并写到一个指定的文件.
     * @param FileName
     */
    public void testReadFileFromBlob(String FileName){
        try{
            File file=new File(FileName);           
            java.io.FileOutputStream fw=new java.io.FileOutputStream(file);
            oracle.jdbc.OracleConnection  conn=(oracle.jdbc.OracleConnection)getConnection();              
            OraclePreparedStatement  pst=(OraclePreparedStatement)conn.prepareStatement("select printfile from tKHDZDPRINT where " +
                    " theyear=? and theseason=? and khh=?");
            pst.setInt(1, Integer.valueOf(2007));
            pst.setInt(2, Integer.valueOf(1));
            pst.setString(3, "1094103");
            java.sql.ResultSet rs=pst.executeQuery();
            if (rs.next()){
                //读取内容
                oracle.sql.BLOB c=(oracle.sql.BLOB)rs.getBlob(1);
                byte[] cbuf=new byte[1024];
                Integer iRead;               
                java.io.InputStream r= c.getBinaryStream(1L);
               
                iRead=r.read(cbuf, 0, 1024);
                while (iRead.compareTo(-1)!=0){
                    fw.write(cbuf,0,iRead);                                       
                    iRead=r.read(cbuf, 0, 1024);
                }
                fw.flush();;
                fw.close();               
            }
            pst.close();           
      }
      catch( java.io.IOException e){
         
      }
      catch( java.lang.ClassNotFoundException e){
         
      }
      catch (java.sql.SQLException e){
         
      }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值