加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

不依赖驱动更新blob字段

发布时间:2020-12-14 04:50:06 所属栏目:百科 来源:网络整理
导读:不依赖驱动更新blob字段 /** * 此方法用于执行对大对象进行操作的sql语句 * 传入的参数:blob对象 */ public boolean execUpdateBlobSQL(String sql,String tJSONObj){ PreparedStatement pstmt = null; ResultSet rs = null; //System.out.println("下面是
不依赖驱动更新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;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读