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;
        }

    相关:

  • 相关阅读:
    git rebase命令
    java中HashSet对象内的元素的hashCode值不能变化
    Spring中WebApplicationInitializer的理解
    mysql判断表字段或索引是否存在,然后修改
    mysql存储过程
    判断地图上的点是否在圆形,多边形,区域内
    计算任意多边形的面积、中心、重心
    判断点是否在任意多边形内
    springMvc将对象json返回时自动忽略掉对象中的特定属性的注解方式
    String.format()详细用法
  • 原文地址:https://www.cnblogs.com/ding0910/p/1756324.html
Copyright © 2011-2022 走看看