`

bboss persistent 1.0.2中方便地实现大字段(clob,blob)的处理

阅读更多


bboss项目下载列表 在sourceforge访问地址为:
https://sourceforge.net/project/showfiles.php?group_id=238653

bboss-persistent 1.0.2 下载地址
http://sourceforge.net/project/platformdownload.php?group_id=238653


bboss persistent框架提供了对大字段处理的封装,本文详细介绍具体的用法:
1.获取与blob和clob字段相关的流对象
2.直接获取操作lob字段包括增删改查4种操作,其中有通用的操作(适用于所有的数据库),针对oracle的操作方法。
下面详细介绍
 

 

获取与blob和clob字段相关的流对象
    package com.frameworkset.common;

import java.sql.SQLException;

import com.frameworkset.common.poolman.DBUtil;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.handle.ValueExchange;

/**
 * 测试如下方法是否正常工作
 * dbUtil.getBinaryStream
 * dbUtil.getUnicodeStream(
 * dbUtil.getAsciiStream(
 * getCharacterStream(
 * <p>Title: TestStream.java</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 * @Date Dec 5, 2008 3:12:22 PM
 * @author biaoping.yin
 * @version 1.0.1
 */
public class TestStream {
        public static void testClob()
        {
                DBUtil dbUtil = new DBUtil();
                try {
                        dbUtil.executeSelect("select clobname from test where id='0'");
                        System.out.println("BinaryStream:" + ValueExchange.getStringFromStream(dbUtil.getBinaryStream(0, 0)));
                        System.out.println("UnicodeStream:" + ValueExchange.getStringFromStream(dbUtil.getUnicodeStream(0, 0)));
                        System.out.println("AsciiStream:" + ValueExchange.getStringFromStream(dbUtil.getAsciiStream(0, 0)));
                        System.out.println("Reader:" + ValueExchange.getStringFromReader(dbUtil.getCharacterStream(0, 0)));
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
               
        }
       
        public static void testBlob()
        {
                PreparedDBUtil pdb = new PreparedDBUtil();
               
               
                DBUtil dbUtil = new DBUtil();
                try {
                        pdb.preparedUpdate("update test set blobname = ? where id = '0'");
                        pdb.setBlob(1, "blob".getBytes());
                        pdb.executePrepared();
                        dbUtil.executeSelect("select blobname from test where id='0'");
                        System.out.println("BinaryStream:" + ValueExchange.getStringFromStream(dbUtil.getBinaryStream(0, 0)));
                        System.out.println("UnicodeStream:" + ValueExchange.getStringFromStream(dbUtil.getUnicodeStream(0, 0)));
                        System.out.println("AsciiStream:" + ValueExchange.getStringFromStream(dbUtil.getAsciiStream(0, 0)));
                        /**
                         * blob类型的字段不能直接转换为Reader对象
                         */
                        System.out.println("Reader:" + ValueExchange.getStringFromReader(dbUtil.getCharacterStream(0, 0)));
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
               
        }
       
       
       
        public static void main(String[] args)
        {
                testBlob();
        }

}

 

直接获取操作lob字段包括增删改查4种操作,其中有通用的操作(适用于所有的数据库)和针对oracle的操作方法


/*
 *  Copyright 2008 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.frameworkset.common;

import javax.transaction.RollbackException;

import oracle.sql.BLOB;
import oracle.sql.CLOB;

import com.frameworkset.common.poolman.DBUtil;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.orm.transaction.TransactionManager;

/**
 *
 * <p>Title: TestLob.java</p>
 *
 * <p>
 * Description:运行本类时必须下载 bboss persistent 框架的version 1.0.2 下载的地址如下:
 * https://sourceforge.net/project/showfiles.php?group_id=238653
 * </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 * @Date Dec 24, 2008 10:05:04 PM
 * @author biaoping.yin
 * @version 1.0
 */
public class TestLob {
       
        /**
     * 第一种插入blob字段的方法,通用的处理模式,适用于所有类型的数据库
         */
        public static void testBlobWrite(String id)
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                try {
                        dbUtil.preparedInsert( "insert into test(id,blobname) values(?,?)");
                       
                        dbUtil.setString(1, id);
                        dbUtil.setBlob(2, new java.io.File("d:/内容管理系统升级步骤.txt"));//直接将文件存储到大字段中
                       
                        dbUtil.executePrepared();
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally
                {
                       
                }
               
        }
       
       
       
       
        /**
         * 针对oracle Blob字段的插入操作
         */
        public static void testBigBlobWrite()
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                TransactionManager tm = new TransactionManager();
                try {
                        //启动事务
                        tm.begin();
                        //先插入一条记录,blob字段初始化为empty_lob
                        dbUtil.preparedInsert( "insert into test(id,blobname) values(?,?)");
                        String id = DBUtil.getNextStringPrimaryKey("test");
                        dbUtil.setString(1, id);
                        dbUtil.setBlob(2,BLOB.empty_lob());//先设置空的blob字段
                       
                       
                        dbUtil.executePrepared();
                       
                        //查找刚才的插入的记录,修改blob字段的值为一个文件
                        dbUtil = new PreparedDBUtil();
                        dbUtil.preparedSelect("select blobname from test where id = ?");
                        dbUtil.setString(1, id);
                        dbUtil.executePrepared();
                       
                        BLOB blob = (BLOB)dbUtil.getBlob(0, "blobname");
                        if(blob != null)
                        {                               
                                DBUtil.updateBLOB(blob, new java.io.File("d:/wls_docs92.chm"));
                        }
                        tm.commit();
                       
                       
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        try {
                                tm.rollback();
                        } catch (RollbackException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
                finally
                {
                        tm = null;
                        dbUtil = null;
                }
               
        }
       
        /**
     * 第一种update blob字段的方法,通用的处理模式,适用于所有类型的数据库
         */
        public static void testBlobUpdate()
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                try {
                        dbUtil.preparedUpdate("update test set blobname= ? where id= ?");
                       
                        dbUtil.setString(2, "12");
                        dbUtil.setBlob(1, new java.io.File("d:/readme.txt"));//直接将文件存储到大字段中
                       
                        dbUtil.executePrepared();
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally
                {
                        dbUtil = null;
                }
               
        }
        /**
         * 针对oracle Blob字段的update操作
         */
        public static void testBigBlobUpdate()
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                TransactionManager tm = new TransactionManager();
                try {
                        //启动事务
                        tm.begin();
                        //先查询id=12一条记录,以乐观锁的方式来处理这条记录,当有其他程序正在修改id=12的记录时,将抛出异常直接终止改修改操作
                        dbUtil.preparedSelect( "select id,blobname from test where id = ? for update nowait");
                        String id = "12";
                        dbUtil.setString(1, id);                       
                        dbUtil.executePrepared();
                       
                        BLOB blob = (BLOB)dbUtil.getBlob(0, "blobname");
                        if(blob != null)
                        {               
                                //更新blob字段的值
                                DBUtil.updateBLOB(blob, new java.io.File("d:/readme.txt"));
                        }
                        //提交事务
                        tm.commit();
                       
                       
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        try {
                                //回滚事务
                                tm.rollback();
                        } catch (RollbackException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
                finally
                {
                        tm = null;
                        dbUtil = null;
                }
               
        }
       
       
        /**
         * 大字段的读取
         */
        public static void testBlobRead()
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                try {
                        //查询大字段内容并且将大字段存放到文件中
                        dbUtil.preparedSelect( "select id,blobname from test");
                        dbUtil.executePrepared();
                       
                        for(int i = 0; i < dbUtil.size(); i ++)
                        {
                               
                                dbUtil.getFile(i, "blobname", new java.io.File("d:/wls_docs921.chm"));//将blob字段的值转换为文件
//                                Blob blob = dbUtil.getBlob(i, "blobname");//获取blob字段的值到blob变量中。
                        }
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally
                {
                       
                }
               
        }
       
        /**
         * clob字段的写入
         */
        public static void testClobWrite(String id)
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                try {
                        dbUtil.preparedInsert( "insert into test(id,clobname) values(?,?)");
                       
//                        dbUtil.setString(1, DBUtil.getNextStringPrimaryKey("test"));
                        dbUtil.setString(1, id);
                        dbUtil.setClob(2,"clobvalue");//直接将字符串存储到clob字段中,也可以直接使用dbUtil.setString(2,"clobvalue")来设置clob字段的值
                        dbUtil.executePrepared();
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally
                {
                       
                }
               
        }
       
        /**
         * 针对oracle Clob字段的插入操作
         */
        public static void testBigClobWrite()
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                TransactionManager tm = new TransactionManager();
                try {
                        //启动事务
                        tm.begin();
                        //先插入一条记录,blob字段初始化为empty_lob
                        dbUtil.preparedInsert( "insert into test(id,clobname) values(?,?)");
                        String id = DBUtil.getNextStringPrimaryKey("test");
                        dbUtil.setString(1, id);
                        dbUtil.setClob(2,CLOB.empty_lob());//先设置空的blob字段
                       
                       
                        dbUtil.executePrepared();
                       
                        //查找刚才的插入的记录,修改blob字段的值为一个文件
                        dbUtil = new PreparedDBUtil();
                        dbUtil.preparedSelect("select clobname from test where id = ?");
                        dbUtil.setString(1, id);
                        dbUtil.executePrepared();
                       
                        CLOB clob = (CLOB)dbUtil.getClob(0, "clobname");
                        if(clob != null)
                        {                               
                                DBUtil.updateCLOB(clob, new java.io.File("d:\\route.txt"));
                        }
                        tm.commit();
                       
                       
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        try {
                                tm.rollback();
                        } catch (RollbackException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
                finally
                {
                        tm = null;
                        dbUtil = null;
                }
               
        }
       
        /**
         * clob字段的更新
         */
        public static void testClobUpdate()
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                try {
                        dbUtil.preparedUpdate( "update test set clobname=? where id='12'");
                       
                       
                        dbUtil.setClob(1,"clobvalue11111");//直接将字符串存储到clob字段中,也可以直接使用dbUtil.setString(2,"clobvalue")来设置clob字段的值
                        dbUtil.executePrepared();
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally
                {
                       
                }
               
        }
       
        /**
         * 针对oracle Clob字段的修改操作
         */
        public static void testBigClobUpdate()
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                TransactionManager tm = new TransactionManager();
                try {
                        //启动事务
                        tm.begin();
                        //先插入一条记录,blob字段初始化为empty_lob
                        dbUtil.preparedSelect( "select id,clobname from test where id='12' for update nowait");
                       
                      dbUtil.executePrepared();
                       
                       
                       
                        CLOB clob = (CLOB)dbUtil.getClob(0, "clobname");
                        if(clob != null)
                        {                               
                                DBUtil.updateCLOB(clob, new java.io.File("d:\\readme.txt"));
                        }
                        tm.commit();
                       
                       
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        try {
                                tm.rollback();
                        } catch (RollbackException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
                finally
                {
                        tm = null;
                        dbUtil = null;
                }
               
        }
       
        /**
         * clob字段的读取
         */
        public static void testClobRead()
        {
                PreparedDBUtil dbUtil = new PreparedDBUtil();
                try {
                        //查询大字段内容并且将大字段存放到文件中
                        dbUtil.preparedSelect( "select id,clobname from test");
                        dbUtil.executePrepared();
                       
                        for(int i = 0; i < dbUtil.size(); i ++)
                        {
                               
                                dbUtil.getFile(i, "clobname", new java.io.File("d:/route" + i + ".txt")); //读取clob字段到文件中
//                                String clobvalue = dbUtil.getString(i, "clobname");//获取clob字段到字符串变量中
//                                Clob clob = dbUtil.getClob(i, "clobname");//获取clob字段值到clob类型变量中
                        }
                       
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                finally
                {
                        dbUtil = null;
                }
               
        }
       
        public static void main(String[] args)
        {
                System.out.print("start.........");
                DBUtil.debugMemory();
                for(int i = 0; i < 1; i ++)
                {
//                        String id = DBUtil.getNextStringPrimaryKey("test");
                        String id = "12";
//                        testBlobWrite(id);
                        testClobWrite( id);
//                        testBigBlobWrite();
//                        testClobWrite();
//                        testBigClobWrite();
                       
                }
//                TestLob.testClobUpdate();
                testBigClobUpdate();
//                TestLob.testBlobUpdate();
//                TestLob.testBigBlobUpdate();
//                testBlobWrite();
//                testBigBlobWrite();
//                DBUtil.debugMemory();
//                testBlobRead();
//                testClobRead();
                System.out.println("end.........");
                DBUtil.debugMemory();
        }
       
       

}


运行上述的测试示例,需要在数据库中执行以下脚本,脚本已经在oracle 10上测试通过:
 

drop table test;
CREATE TABLE TEST
(
  NAME  VARCHAR2(4000),
  ID    VARCHAR2(100)
);
drop table test1;
CREATE TABLE TEST1
(
  A  VARCHAR2(12)
);

DROP SEQUENCE SEQ_TEST;

CREATE SEQUENCE SEQ_TEST
  START WITH 160
  MAXVALUE 999999999999999999999999999
  MINVALUE 0
  NOCYCLE
  CACHE 20
  NOORDER;

drop table TABLEINFO cascade constraints
/
CREATE TABLE TABLEINFO
(
  TABLE_NAME          VARCHAR2(255)        NOT NULL,
  TABLE_ID_NAME       VARCHAR2(255),
  TABLE_ID_INCREMENT  NUMBER(5)                 DEFAULT 1,
  TABLE_ID_VALUE      NUMBER(20)                DEFAULT 0,
  TABLE_ID_GENERATOR  VARCHAR2(255),
  TABLE_ID_TYPE       VARCHAR2(255),
  TABLE_ID_PREFIX     VARCHAR2(255)
);

COMMENT ON TABLE TABLEINFO IS '表信息维护对象';

COMMENT ON COLUMN TABLEINFO.TABLE_NAME IS '表名称';


COMMENT ON COLUMN TABLEINFO.TABLE_ID_NAME IS '表的主键名称';

COMMENT ON COLUMN TABLEINFO.TABLE_ID_INCREMENT IS '表的主键递增量
缺省为1';

COMMENT ON COLUMN TABLEINFO.TABLE_ID_VALUE IS '主键当前值:缺省为0';

COMMENT ON COLUMN TABLEINFO.TABLE_ID_GENERATOR IS '自定义表主键生成机制
必需从
com.frameworkset.common.poolman.sql.PrimaryKey集成';

由于脚本太长,请下载附件:

1
0
分享到:
评论

相关推荐

    由 bboss 开源的数据采集&流批一体化工具,提供数据采集、数据清洗转换处理和数据入库以及数据指标统计计算流批一体化处理功能

    通过内置的适配器,用户可以方便地将各种来源的数据高效地抽取到处理管道中。 ### 数据清洗与转换 在数据处理流程中,数据清洗是至关重要的一步。bboss-datatran 提供了丰富的数据转换规则,包括数据类型转换、字段...

    bboss mvcdemo 下载地址

    BBoss MVCDemo提供了灵活的控制器机制,可以方便地处理HTTP请求,进行参数绑定、校验以及异常处理。 4. **AOP(面向切面编程)**:BBoss MVCDemo支持AOP,可以实现如事务管理、日志记录、权限控制等功能,提高代码...

    bboss会话共享培训文档

    它还支持跨多个主流应用服务器(如Tomcat、WebLogic、WebSphere、Jetty等)部署,并且没有会话粘滞性的约束,允许客户端请求均匀地分派给集群中的各个节点。 安全性方面,bboss会话共享通过在客户端基于cookie机制...

    bboss elasticsearch-5.7.8.rar

    3. `bboss-util-5.5.0.jar`和`bboss-persistent-5.5.0.jar`:这是BBoss框架的核心组件,提供了许多实用工具类和持久化操作支持,如数据库连接、事务管理等,为Elasticsearch的数据导入提供底层支持。 4. `...

    bboss mvc 通过jsonp实现跨站跨域远程访问

    标题中的“bboss mvc 通过jsonp实现跨站跨域远程访问”指的是使用bboss MVC框架来处理JSONP(JSON with Padding)请求,以克服浏览器的同源策略限制,实现跨域数据交互。bboss MVC是一个Java Web开发框架,它提供了...

    企业级J2EE开源框架bboss

    BBoss(全称为bbossgroups)是一个专为企业级J2EE应用设计的开源框架,它为Java开发者提供了一系列强大的工具和服务,以简化Web应用程序的开发过程。该框架旨在提高开发效率,降低维护成本,同时保持高度的灵活性和...

    bboss ioc配置文件中使用外部属性文件介绍

    本文将详细介绍如何在BBoss的IOC配置文件中引用外部属性文件,以便更好地管理和动态配置应用。 首先,BBoss的IOC配置文件通常是一个XML文件,比如`bboss-ioc.xml`,在这个文件中我们可以声明并配置各种bean。当需要...

    bboss-elasticsearch开发环境搭建和开发入门视频教程.

    **Elasticsearch 开发环境搭建与 BBoss 入门教程** ...通过提供的视频教程和源码示例,你可以逐步学习到环境配置、基本操作以及一些进阶技能,从而在实际项目中更有效地利用 Elasticsearch 和 BBoss。

    springboot整合bboss es增删改查测试demo代码

    本实例是一个基于bboss es spring boot starter的demo maven工程,可供spring boot项目集成bboss elasticsearch rest client参考 展示了通过spring boot管理单集群功能和管理多集群功能 单集群测试用例:...

    bboss+es基本操作示例.zip

    结合bboss和Elasticsearch,开发者可以轻松实现日志实时分析、用户行为追踪、商品推荐等业务场景,通过DSL实现复杂查询和聚合分析,提升应用的数据处理能力。 总之,bboss+es基本操作示例是学习和实践Elastic...

    bboss-db-elasticsearch-tool-master_java_

    在Java中,传统的JDBC编程方式往往需要编写大量重复的SQL代码,而ORM框架如BBoss-DB-Elasticsearch-Tool则通过元数据(如注解或XML配置)将Java对象与数据库表映射起来,实现了数据的透明访问。这极大地提高了开发...

    bboss 安全认证过滤器功能介绍

    BBoss安全认证过滤器是Java Web开发中一种用于实现用户身份验证和权限控制的重要组件。在Web应用程序中,过滤器(Filter)是Servlet规范的一部分,它允许开发者在请求到达目标Servlet或JSP之前进行预处理,以及在...

    由 bboss 开源的数据采集同步ETL工具,提供数据采集、数据清洗转换处理和数据入库以及数据指标统计计算流批一体化处理功能

    bboss-datatran 由 bboss 开源的数据采集&流批一体化工具,提供数据采集、数据清洗转换处理和数据入库以及数据指标统计计算流批一体化处理功能。 同时也是一个高性能Elasticsearch orm 客户端,100%兼容es各个版本 ...

    基于Java的bboss-datatran数据采集与流批一体化处理工具设计源码

    本bboss-datatran项目基于Java开发,包含591个文件,包括Java源代码、...系统实现了数据采集、数据清洗转换处理、数据入库以及数据指标统计计算的流批一体化处理功能,界面友好,功能完善,适合用于数据采集与处理。

Global site tag (gtag.js) - Google Analytics