不依赖驱动更新blob字段

不依赖驱动更新blob字段

/**
     * 此方法用于执行对大对象进行操作的sql语句
     * 传入的参数:blob对象
     */
    public boolean execUpdateBlobSQL(String sql,String tJSONObj){
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        //System.out.println("下面是预编译ExecSQL...");
        System.out.println("预编译ExecSQL执行时间:"+new java.util.Date()+"; ExecSQL : " + sql);
        if (!mflag){
            con = DBConnPool.getConnection();
        }
        try{
            con.setAutoCommit(false);//addbyefeng 关闭自动提交 
            pstmt = con.prepareStatement(StrTool.GBKToUnicode(sql));
            //循环传入的参数数据  将数组内的参数 赋值给 预编译sql
            rs = pstmt.executeQuery(sql);    
            while (rs.next())
               {
                //注:此处很重要。得到oracle.sql.BLOB对象后反射转换为BufferedOutputStream,目的是不依赖驱动
                Object obj = rs.getBlob("TRANSFERDATA");
                Class<? extends Object> clazz = obj.getClass();
                Method method = clazz.getMethod("getBinaryOutputStream",new Class[]{});
                OutputStream os = (OutputStream)method.invoke(obj,new Object[]{});
                BufferedOutputStream outStream = new BufferedOutputStream(os);
                outStream.write(tJSONObj.getBytes("GBK"),tJSONObj.getBytes("GBK").length);
                os.flush();
                outStream.flush();
                os.close();
                outStream.close();
               }
            rs.close();
            pstmt.close();
            if (!mflag){
                con.commit();
                con.close();
            }
        }
        catch (Exception e){
            // @@错误处理
            System.out.println("### Error ExeSQL at execUpdateSQL: " + sql);
            CError.buildErr(this,e.toString(),mErrors);

            try{
                if (pstmt != null){
                    //由于描述的问题,导致执行的sql错误百出,因此pstmt的关闭需要特殊处理
                    try{
                        pstmt.close();
                    }
                    catch (SQLException ex){
                        ex.printStackTrace();
                    }
                    finally{
                        try{
                            System.out.println("Sql‘s bug is very big: " + sql);
                            pstmt.close();
                        }
                        catch (SQLException ex){}
                    }
                }
                if (!mflag){
                    con.rollback();
                    con.close();
                }
            }
            catch (SQLException ex){
                //在这个地方,有可能会没有关闭连接
                ex.printStackTrace();
                return false;
            }
            return false;
        }finally{
            try {
                if (rs != null) {
                rs.close();
                }
                if (pstmt!=null) {
                pstmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
/**
     * 此方法用于执行对大对象进行操作的sql语句
     * 传入的参数:blob对象
     */
    public boolean execSetBlobDataSQL(String sql,String tJSONObj,String tColumn){
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        //System.out.println("下面是预编译ExecSQL...");
        System.out.println("预编译ExecSQL执行时间:"+new java.util.Date()+"; ExecSQL : " + sql);
        if (!mflag){
            con = DBConnPool.getConnection();
        }
        try{
            con.setAutoCommit(false);//addbyefeng 关闭自动提交 
            pstmt = con.prepareStatement(StrTool.GBKToUnicode(sql));
            //循环传入的参数数据  将数组内的参数 赋值给 预编译sql
            rs = pstmt.executeQuery(sql);    
            while (rs.next())
               {
                //注:此处很重要。得到oracle.sql.BLOB对象后反射转换为BufferedOutputStream,目的是不依赖驱动
                Object obj = rs.getBlob(tColumn);
                Class<? extends Object> clazz = obj.getClass();
                Method method = clazz.getMethod("getBinaryOutputStream",mErrors);

            try{
                if (pstmt != null){
                    //由于描述的问题,导致执行的sql错误百出,因此pstmt的关闭需要特殊处理
                    try{
                        pstmt.close();
                    }
                    catch (SQLException ex){
                        ex.printStackTrace();
                    }
                    finally{
                        try{
                            System.out.println("Sql‘s bug is very big: " + sql);
                            pstmt.close();
                        }
                        catch (SQLException ex){}
                    }
                }
                if (!mflag){
                    con.rollback();
                    con.close();
                }
            }
            catch (SQLException ex){
                //在这个地方,有可能会没有关闭连接
                ex.printStackTrace();
                return false;
            }
            return false;
        }finally{
            try {
                if (rs != null) {
                rs.close();
                }
                if (pstmt!=null) {
                pstmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

相关文章

什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
单一职责原则定义(Single Responsibility Principle,SRP)...
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...