JDBC更新计数行调用存储过程返回多个结果集是本文我们主要要介绍的内容,在开始本文的时候,我们先了解SQL Server中的一个命令:SET NOCOUNT ON;执行该命令,表示不返回计数行,什么是计数行了,比如我们执行DELETE ,UPDATE,INSERT的时候,对多少条数据进行了修改,计数行的值就是多少?

    SETNOCOUNTONaddedtopreventextraresultsetsfrom interferingwithSELECTstatements.

在JDBC的操作数据库的过程中,你可以把Statement理解成为指向ResultSet的指针,如果数据库允许返回记数行的话,Statement将指向该计数行,比如

    SETNOCOUNTON; updateTABLEASETA='aa';--假设共100条数据被修改 SELECT*FROMTABLEA;

调用callableStatement.execute();后callableStatement指向受影响的计数行,当你再调用rs=callableStatement.getResultSet();的时候,结果集rs 为空。 无法查询出表TABLEA的数据Statement提供了一个getMoreResults()的方法,该方法能将当前Statement “指针” 移动到下一个结果集。如果callableStatement.getUpdateCount()==-1&&getMoreResults()==true的话表明当前statement对象正指向一个真正的结果集。

For Examle:

    packagexx.qq.app; importjava.sql.CallableStatement; importjava.sql.Connection; importjava.sql.ResultSet; importorg.springframework.beans.factory.BeanFactory; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; importcom.mchange.v2.c3p0.ComboPooledDataSource; /** *@authorJackZhangEmail:[email protected] *@date2011-08-22 */ publicclassAppTest{ publicstaticvoidmain(String[]args)throwsException{ ApplicationContextcontext=newClassPathXmlApplicationContext( newString[]{"applicationContext.xml"}); BeanFactoryfactory=(BeanFactory)context; ComboPooledDataSourcedataSource=(ComboPooledDataSource)factory .getBean("dataSource"); Connectioncon=dataSource.getConnection(); CallableStatementcallableStatement=con .prepareCall("{callGetBasics(?,?)}"); callableStatement.setString(1,"w"); callableStatement.registerOutParameter(2,java.sql.Types.INTEGER); ResultSetrs=null; //是否有结果集返回 booleanhasResultSet=callableStatement.execute(); //callableStatement--------->update System.out.println("执行存储过程后Statement是否指向真正的结果集:"+hasResultSet); System.out.println("受影响的行数:"+callableStatement.getUpdateCount()); callableStatement.getMoreResults();//------->select rs=callableStatement.getResultSet(); System.out.println("受影响的行:"+callableStatement.getUpdateCount()); while(rs.next()){ //System.out.println(rs.getObject(1)); } callableStatement.getMoreResults();//-------->update System.out.println("受影响的行:"+callableStatement.getUpdateCount()); callableStatement.getMoreResults();//-------->update System.out.println("受影响的行:"+callableStatement.getUpdateCount()); callableStatement.getMoreResults();//-------->select System.out.println("受影响的行:"+callableStatement.getUpdateCount()); rs=callableStatement.getResultSet();//获取到真实的结果集 while(rs.next()){ //System.out.println(rs.getObject(1)); } callableStatement.getMoreResults();//--------->update System.out.println("受影响的行:"+callableStatement.getUpdateCount()); if(rs!=null) rs.close(); if(callableStatement!=null) callableStatement.close(); if(con!=null) con.close(); } }

输出:

执行存储过程后是否返回结果集:false

    受影响的行数:262受影响的行:-1,此处返回结果集受影响的行:262受影响的行:262受影响的行:-1,此处返回结果集受影响的行:262

存储过程

    ALTERPROCEDUREGetBasics( @PERSON_NAMEVARCHAR(32), @COUNTINTOUT ) AS BEGIN SETNOCOUNTON; updateTABLE_ASETNAME='aa'; SELECT@COUNTCOUNT=COUNT(*)FROMTABLE_A; updateTABLE_ASETNAME='aa'; SELECT*FROMTABLE_A; updateTABLE_ASETNAME='aa'; updateTABLE_ASETNAME='aa'; SELECT*FROMORGS; updateTABLE_ASETNAME='aa'; END GO

以上就是JDBC更新计数行及调用存储过程返回多个结果集的过程的详细解释及实例说明,本文就介绍到这里了,希望本次的介绍能够对您有所收获!

【编辑推荐】

    Oracle 11g数据库审计功能应用实例解析SQL Server数据库复制失败的原因及解决方案SQL Server 2005数据库游标调用函数实例解析SQL Server数据库中FOR XML AUTO的使用详解SQL Server数据库用视图来处理复杂的数据查询关系