不依赖驱动更新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"), 0, 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", new Class[]{});
                OutputStream os = (OutputStream)method.invoke(obj, new Object[]{});
                BufferedOutputStream outStream = new BufferedOutputStream(os);
                outStream.write(tJSONObj.getBytes("GBK"), 0, 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;
    }

猜你喜欢

转载自blog.51cto.com/zhaoanan/2409302