zoukankan      html  css  js  c++  java
  • java读写oracle Blob数据

    代码
    public int setBlob(DBConnection dbConn, String table, String idField, String fieldName, 
                String id, String content, 
    boolean bApeand) {
            
    int ret = -1;
            PreparedStatement pstmt 
    = null;
            ResultSet rs 
    = null;
            
    try {
                content 
    = this.preDisposeString(content);
                
    //java.sql.*
                Connection conn = dbConn.getConnection();
                String sql 
    = "select "+fieldName+" from "+table+" where "+idField+"="+id+" for update";
                pstmt 
    = conn.prepareStatement(sql);
                rs 
    = pstmt.executeQuery();
                Blob blob 
    = null;
                
    if (rs.next()) {
                    
    //获取blob对象,此处的blob是oracle.sql.Blob   
                    blob = (Blob)rs.getBlob(fieldName);
                    
    //执行更新操作   
                    long pos = 0//Blob.length();
                    if(bApeand) {
                        pos 
    = blob.length() + 8;
                        
    if(blob.length() > 0) {
                            content 
    = " \n " + content;
                        }
                    }
                     OutputStream wr 
    = ((BLOB)blob).getBinaryOutputStream();
                     InputStream param 
    = (InputStream) (new ByteArrayInputStream(content.getBytes()));
                     
    for(int temp = param.read(); temp != -1; temp = param.read()) {
                         wr.write(temp);
                     }

                     wr.flush();
                     wr.close();
    //                //
                    ret = 1;
                }
            } 
    catch(SQLException es) {
                es.printStackTrace();
                ret 
    = -1;
            } 
    catch(Exception e) {
                e.printStackTrace();
                ret 
    = -1;
            } 
    finally {
                
    try {
                    
    if(null != rs) {
                        rs.close();
                    }
                    
    if(null != pstmt) {
                        pstmt.close();
                    }
                } 
    catch (SQLException e) {}
            }
            
    return ret;
        }

    相关:

  • 相关阅读:
    javaee 第六周作业
    javaee 第五周作业
    javaee 第四周作业
    第三周作业
    第二周作业xml学习情况
    javaWeb 中http请求 get 与 post的区别
    第八周
    第七周
    第六周
    第五周
  • 原文地址:https://www.cnblogs.com/ding0910/p/1756324.html
Copyright © 2011-2022 走看看