`

bboss 持久层框架CallableDBUtil组件执行存储过程和函数实例

阅读更多
本文以两个例子来介绍如何通过bboss 持久层框架的CallableDBUtil组件来执行函数和存储过程。切入正题。

函数执行实例:
package com.frameworkset.common;

import javax.transaction.RollbackException;

import com.frameworkset.common.poolman.CallableDBUtil;
import com.frameworkset.common.poolman.Record;
import com.frameworkset.orm.transaction.TransactionManager;

public class TestCallableFunction {
	public static void testTest_fWithPositionIndex()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();  
		try
		{
			callableDBUtil.prepareCallable("{? = call Test_f(?,?,?)}");
			callableDBUtil.registerOutParameter(1, java.sql.Types.INTEGER);
			callableDBUtil.setInt(2, 10);
			callableDBUtil.registerOutParameter(3, java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter(4, java.sql.Types.VARCHAR);
			callableDBUtil.executeCallable();
			System.out.println("ret:" + callableDBUtil.getInt(1));
			System.out.println("name:" + callableDBUtil.getString(3));
			System.out.println("name1:" + callableDBUtil.getString(4));
						
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * 执行函数时不能通过命名方式绑定变量
	 */
	public static void testTest_fWithNameIndex()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			callableDBUtil.prepareCallable("{? = call Test_f(?,?,?)}");
			callableDBUtil.registerOutParameter(1, java.sql.Types.INTEGER);
			callableDBUtil.setInt(2, 10);
			callableDBUtil.registerOutParameter(3, java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter(4, java.sql.Types.VARCHAR);
			callableDBUtil.executeCallable();
			System.out.println("ret:" + callableDBUtil.getInt(1));
			System.out.println("name:" + callableDBUtil.getString(3));
			System.out.println("name1:" + callableDBUtil.getString(4));
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	public static void testTest_fWithNameIndexForXMLString()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			callableDBUtil.prepareCallable("{? = call Test_f(?,?,?)}");
			callableDBUtil.registerOutParameter(1, java.sql.Types.INTEGER);
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			callableDBUtil.setInt(2, 10);
			callableDBUtil.registerOutParameter(3, java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter(4, java.sql.Types.VARCHAR);
			String xmlString = callableDBUtil.executeCallableForXML();
//			System.out.println("name1:" + callableDBUtil.getString("name"));
//			System.out.println("name2:" + callableDBUtil.getString("name1"));
			System.out.println("xmlString:" + xmlString);
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * 对于函数的调用使能使用顺序位置标识来绑定变量
	 */
	public static void testTest_fWithNameIndexForXMLStringRowHandler()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			callableDBUtil.prepareCallable("{? = call Test_f(?,?,?)}");
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			callableDBUtil.registerOutParameter(1, java.sql.Types.INTEGER);
			callableDBUtil.setInt(2, 10);
			callableDBUtil.registerOutParameter(3, java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter(4, java.sql.Types.VARCHAR);
			String xmlString = callableDBUtil.executeCallableForXML(new com.frameworkset.common.poolman.handle.RowHandler()
			{
				/**
				 * 对已经处理好的行记录进行处理的逻辑
				 * @param rowValue
				 */
				public void handleRow(Object rowValue,Record record)
				{
					StringBuffer objects = (StringBuffer)rowValue;
					objects.append(record);
					System.out.println("objects:" + objects);
					
				}
			});
//			System.out.println("name1:" + callableDBUtil.getString("name"));
//			System.out.println("name2:" + callableDBUtil.getString("name1"));
			System.out.println("xmlString:" + xmlString);
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	public static void testTest_fWithNameIndexForObject()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			
			callableDBUtil.prepareCallable("{? = call Test_f(?,?,?)}");
			callableDBUtil.registerOutParameter(1, java.sql.Types.INTEGER);
			callableDBUtil.setInt(2, 10);
			callableDBUtil.registerOutParameter(3, java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter(4, java.sql.Types.VARCHAR);
			Test_f tets = (Test_f)callableDBUtil.executeCallableForObject(Test_f.class,new com.frameworkset.common.poolman.handle.RowHandler()
			{

				public void handleRow(Object rowValue,Record record) {
					Test_f objects = (Test_f)rowValue;
					try
					{
						objects.setRet(record.getString(1));
						objects.setName(record.getString(3));
						objects.setName1(record.getString(4));
					}
					catch(Exception e)
					{
						
					}
//					origine.put(new Integer(4), "55");
					System.out.println("rowValue:" + rowValue);
					
				}
				
			}
			);
			
			
//			System.out.println("name1:" + callableDBUtil.getString("name"));
//			System.out.println("name2:" + callableDBUtil.getString("name1"));
			System.out.println("Test_f is " + tets);
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}		
	}
	
	/**
	 * 经过测试证明存储过程中如果没有对插入/删除/修改操作做事务提交、回滚时,可以通过poolman
	 * 的事务框架来管理事务;如果存储过程中对插入/删除/修改操作已经做了事务提交或者回滚时,那么应用的事务和存储过程中
	 * 中的事务就是分离的两个事务。 
	 * @param i 为0时回滚事务,1时提交事务
	 */
	public static void testTest_fWithNameIndexForObjectTx(int i)
	{
		
		TransactionManager tm = new TransactionManager();
		try
		{
			tm.begin();
			CallableDBUtil callableDBUtil = new CallableDBUtil();
			callableDBUtil.prepareCallable("{? = call Test_f(?,?,?)}");
			callableDBUtil.registerOutParameter(1, java.sql.Types.INTEGER);
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			
			callableDBUtil.setInt(2, 10);
			callableDBUtil.registerOutParameter(3, java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter(4, java.sql.Types.VARCHAR);
			Test_f tets = (Test_f)callableDBUtil.executeCallableForObject(Test_f.class);
			
			
//			System.out.println("name1:" + callableDBUtil.getString("name"));
//			System.out.println("name2:" + callableDBUtil.getString("name1"));
			System.out.println("Test_f is " + tets);
			callableDBUtil.executeInsert("insert into test(id,name) values('11','name11')");
			if(i == 0)
				tm.rollback();
			else
				tm.commit();
		}
		catch(Exception e)
		{
			try {
				tm.rollback();
			} catch (RollbackException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}		
	}
	
	public static void main(String[] args)
	{
		System.out.println("1------------------------------------------testTest_fWithNameIndexForObject()");
		testTest_fWithNameIndexForObject();
		System.out.println("2------------------------------------------testTest_fWithNameIndexForObjectTx(1)");
		testTest_fWithNameIndexForObjectTx(1);
		System.out.println("3------------------------------------------testTest_fWithPositionIndex()");
		testTest_fWithPositionIndex();
		System.out.println("4------------------------------------------testTest_fWithNameIndexForXMLString()");
		testTest_fWithNameIndexForXMLString();
		System.out.println("5------------------------------------------testTest_fWithNameIndexForXMLStringRowHandler()");
		testTest_fWithNameIndexForXMLStringRowHandler();
		
		CallableDBUtil.debugStatus();
	}
}



存储过程执行实例:

package com.frameworkset.common;

import java.sql.SQLException;

import javax.transaction.RollbackException;

import com.frameworkset.common.poolman.CallableDBUtil;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.Record;
import com.frameworkset.common.poolman.handle.RowHandler;
import com.frameworkset.common.poolman.util.SQLResult;
import com.frameworkset.orm.transaction.TransactionManager;

public class TestCallableDBUtil {
	public static void testTest_pWithPositionIndex()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
//			callableDBUtil.execute("{call test_p(1,'ss',3,4)}");
			callableDBUtil.prepareCallable("{call test_p(?,?,?,?)}");
			callableDBUtil.setInt(1, 10);
			callableDBUtil.registerOutParameter(2, java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter(3, java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter(4, java.sql.Types.INTEGER);
			callableDBUtil.executeCallable();
			System.out.println("name1:" + callableDBUtil.getString(2));
			System.out.println("name2:" + callableDBUtil.getString(3));
			System.out.println("test:" + callableDBUtil.getInt(4));
			
						
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	public static void testTest_pWithNameIndex()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			callableDBUtil.prepareCallable("{call test_p(?,?,?,?)}");
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			callableDBUtil.setInt("id", 10);
			callableDBUtil.registerOutParameter("name", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("name1", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("test", java.sql.Types.INTEGER);
			
			callableDBUtil.executeCallable();
			System.out.println("name1:" + callableDBUtil.getString("name"));
			System.out.println("name2:" + callableDBUtil.getString("name1"));
			System.out.println("test:" + callableDBUtil.getInt("test"));
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	public static void testTest_pWithNameIndexForXMLString()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			callableDBUtil.prepareCallable("{call test_p(?,?,?,?,?)}");
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			callableDBUtil.setInt("id", 10);
			callableDBUtil.registerOutParameter("name", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("name1", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("test", java.sql.Types.INTEGER);
			callableDBUtil.registerOutParameter("nomatch", java.sql.Types.INTEGER);
			
			String xmlString = callableDBUtil.executeCallableForXML();
			System.out.println("xmlString:" + xmlString);
//			System.out.println("name1:" + callableDBUtil.getString("name"));
//			System.out.println("name2:" + callableDBUtil.getString("name1"));
//			System.out.println("test:" + callableDBUtil.getInt("test"));
			
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	public static void testTest_pWithNameIndexForXMLStringRowHandler()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			callableDBUtil.prepareCallable("{call test_p(?,?,?,?)}");
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			callableDBUtil.setInt("id", 10);
			callableDBUtil.registerOutParameter("name", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("name1", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("test", java.sql.Types.INTEGER);
			String xmlString = callableDBUtil.executeCallableForXML(new com.frameworkset.common.poolman.handle.RowHandler()
			{
				/**
				 * 对已经处理好的行记录进行处理的逻辑
				 * @param rowValue
				 */
				public void handleRow(Object rowValue,Record origine)
				{
					Object objects = (Object)rowValue;
					
					System.out.println("objects rowhandler:" + objects);
//					System.out.println("objects【0】:" + objects[0]);
//					System.out.println(objects[1]);
				}
			});
			System.out.println("name1:" + callableDBUtil.getString("name"));
			System.out.println("name2:" + callableDBUtil.getString("name1"));
			System.out.println("xmlString:" + xmlString);
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	public static void testTest_pWithNameIndexForObject()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			callableDBUtil.prepareCallable("{call test_p(?,?,?,?,?)}");
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			callableDBUtil.setInt("id", 10);
			callableDBUtil.registerOutParameter("name", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("name1", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("test", java.sql.Types.INTEGER);
			callableDBUtil.registerOutParameter("nomatch", java.sql.Types.INTEGER);
			Test_p tets = (Test_p)callableDBUtil.executeCallableForObject(Test_p.class);
			
			System.out.println("Test_p is " + tets);
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}		
	}
	
	public static void testoldMethod()
	{
		PreparedDBUtil callableDBUtil = new PreparedDBUtil();
		try {
			SQLResult result = callableDBUtil.execute("select * from tableinfo");
			System.out.println(result.size());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void testTest_pWithNameIndexForObjectWithRowHandler()
	{
		CallableDBUtil callableDBUtil = new CallableDBUtil();
		try
		{
			callableDBUtil.prepareCallable("{call test_p(?,?,?,?)}");
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			callableDBUtil.setInt("id", 10);
			callableDBUtil.registerOutParameter("name", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("name1", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("test", java.sql.Types.INTEGER);
			Test_p tets = (Test_p)callableDBUtil.executeCallableForObject(Test_p.class,new RowHandler(){

				public void handleRow(Object rowValue, Record record) {
					Test_p test_p = (Test_p)rowValue;
					try {
						test_p.setTest(record.getString("test")+"天马");
						test_p.setName1(record.getString("name1"));
						test_p.setName(record.getString("name"));
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				
			});
			
//			System.out.println("name1:" + callableDBUtil.getString("name"));
//			System.out.println("name2:" + callableDBUtil.getString("name1"));
			
			System.out.println("Test_p is " + tets);
					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}		
	}
	
	/**
	 * 经过测试证明存储过程中如果没有对插入/删除/修改操作做事务提交、回滚时,可以通过poolman
	 * 的事务框架来管理事务;如果存储过程中对插入/删除/修改操作已经做了事务提交或者回滚时,那么应用的事务和存储过程中
	 * 中的事务就是分离的两个事务。 
	 * @param i 为0时回滚事务,1时提交事务
	 */
	public static void testTest_pWithNameIndexForObjectTx(int i)
	{
		
		TransactionManager tm = new TransactionManager();
		try
		{
			tm.begin();
			CallableDBUtil callableDBUtil = new CallableDBUtil();
			callableDBUtil.prepareCallable("{call test_p(?,?,?,?)}");
			//不允许的操作: Ordinal binding and Named binding cannot be combined!
			callableDBUtil.setInt("id", 10);
			callableDBUtil.registerOutParameter("name", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("name1", java.sql.Types.VARCHAR);
			callableDBUtil.registerOutParameter("test", java.sql.Types.INTEGER);
			Test_p tets = (Test_p)callableDBUtil.executeCallableForObject(Test_p.class);
			
			
			System.out.println("name1:" + callableDBUtil.getString("name"));
			System.out.println("name2:" + callableDBUtil.getString("name1"));
			System.out.println("Test_p is " + tets);
			callableDBUtil.executeInsert("insert into test(id,name) values('11','name11')");
			if(i == 0)
				tm.rollback();
			else
				tm.commit();
		}
		catch(Exception e)
		{
			try {
				tm.rollback();
			} catch (RollbackException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}		
	}
	
	
	
	
	
	public static void main(String[] args)
	{
//		System.out.println("1---------------------------------------------------testTest_pWithPositionIndex()");
//		testTest_pWithPositionIndex();
//		System.out.println("2---------------------------------------------------testTest_pWithNameIndex()");
//		testTest_pWithNameIndex();
//		System.out.println("3---------------------------------------------------testTest_pWithNameIndexForObject()");
//		testTest_pWithNameIndexForObject();
//		System.out.println("4---------------------------------------------------testTest_pWithNameIndexForObjectTx()");
//		testTest_pWithNameIndexForObjectTx(0);
//		System.out.println("5---------------------------------------------------testTest_pWithNameIndexForXMLString()");
//		testTest_pWithNameIndexForXMLString();
//		System.out.println("6---------------------------------------------------testTest_pWithNameIndexForXMLStringRowHandler()");
//		testTest_pWithNameIndexForXMLStringRowHandler();
//		System.out.println("7---------------------------------------------------testTest_pWithNameIndexForObjectWithRowHandler()");
//		testTest_pWithNameIndexForObjectWithRowHandler();
//		CallableDBUtil.debugStatus();	
//		testoldMethod();
		
	    testTest_pWithNameIndexForXMLString();
	}

}

分享到:
评论

相关推荐

    企业级J2EE开源框架bboss

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

    基于Java的bboss开源框架设计源码

    bboss框架集成了AOP/iOC、MVC、持久化标签库、RPC、事件处理、Bean-XML序列化等多种功能。本项目基于bboss框架构建了数据采集ETL工具、流批一体化Stream工具、Elasticsearch客户端工具和WebSession共享框架等应用。

    bboss mvcdemo 下载地址

    BBoss MVCDemo 是一个基于Java的企业级应用框架,它主要为开发者提供了一种高效、灵活的MVC(Model-View-Controller)开发模式。这个框架的目的是简化企业级Web应用的开发流程,提高开发效率,并且具备良好的可扩展...

    bboss elasticsearch-5.7.8.rar

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

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

    2. **bboss elasticsearch开发入门教程.wmv**: 通过实例演示,讲解如何使用BBoss进行索引操作、数据插入、查询和更新,帮助初学者快速上手。 **六、资料阅读** "说明.txt" 文件可能包含了关于这些教程的详细步骤和...

    bboss会话共享培训文档

    bboss会话共享是一种分布式会话管理技术,它针对在集群环境下应用部署时如何解决会话数据丢失和单点登录问题提供了专门的解决方案。根据提供的文档内容,我们可以详细探讨bboss会话共享涉及的关键知识点。 首先,...

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

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

    bboss 插件工程,持久层hibernate/hibernate4插件

    bboss-plugins project.包含kafka、log4j、spring、dubbo、activemq、word转pdf、pdf转swf项目,持久层hibernate/hibernate4插件

    J2EE企业级开源框架bboss v5.0.6.8

    bboss功能涵盖ioc,mvc,jsp自定义标签库,持久层,全局事务托管,安全认证,SSO,web会话共享,cxfwebservice服务发布和管理,hessian服务发布和管理等功能。另外还提供了符合中国式自由流的bboss activiti工作流...

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

    BBoss Persistent是一个轻量级的持久层框架,它提供了方便的方式来处理这些大字段。本文将深入探讨在BBoss Persistent 1.0.2中如何高效地管理CLOB和BLOB数据。 首先,CLOB用于存储大量的文本数据,如长篇文章或XML...

    bboss+es基本操作示例.zip

    bboss是针对企业级应用开发的轻量级框架,其对Elasticsearch的封装简化了开发过程,提供了简单易用的API接口。bboss支持JDBC式的操作方式,使得开发者可以像操作数据库一样操作Elasticsearch,提高了开发效率。 3....

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

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

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

    完成清洗和转换后,bboss-datatran 可以将处理后的数据加载到不同的存储系统,如Hadoop HDFS、Hive、HBase、Elasticsearch、Greenplum、Oracle等。这为用户提供了灵活的数据存储选择,并且支持数据分片、分区策略,...

    基于bboss框架的全面设计源码自动生成工具

    该工具是一款基于bboss...此工具能够自动生成bboss MVC、IOC、持久化、JSP、i18n、SQL配置文件、Web服务、Hessian服务等相关源代码,适用于快速开发和项目迭代。详细文档请参考:http://yin-bp.iteye.com/blog/2256948

    bboss-db-elasticsearch-tool-master_java_

    【bboss-db-elasticsearch-tool-master_java_】是一个Java ORM(对象关系映射)框架,它在功能上超越了MyBatis,提供了对多种数据库的广泛支持,包括MySQL、Oracle、PostgreSQL、SQLServer、DB2、DM以及MongoDB。...

    bboss+easyui帮助文档比较详细

    BBoss,全称为“Business Basic Operation Support”,是一个基于Java的企业级开发框架,它提供了丰富的组件和工具,使得开发者能够快速地进行业务逻辑的构建。而EasyUI则是一个基于jQuery的前端UI库,它为开发者...

    基于Java的bboss插件集设计源码,涵盖文档转换及持久层技术

    该项目为bboss插件集的Java设计源码,总计277个文件,涵盖163个Java源文件、22个XML配置文件、14个Gradle构建脚本、13个Git忽略规则、13个属性文件以及少量的JavaScript、HTML、CSS和Shell脚本,主要用于文档转换和...

    bboss-http:bboss http 服务组件

    负载均衡组件特点: 1.服务负载均衡(目前提供RoundRobin负载算法) 2.服务健康检查 3.服务容灾故障恢复 4.服务自动发现(zk,etcd,consul,eureka,db,其他第三方注册中心) 5.路由规则动态切换 5.分组服务管理 ...

Global site tag (gtag.js) - Google Analytics