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

创新互联建站主营顺义网站建设的网络公司,主营网站建设方案,app软件开发公司,顺义h5微信小程序开发搭建,顺义网站营销推广欢迎顺义等地区企业咨询
- SET NOCOUNT ON added to prevent extra result sets from
 - interfering with SELECT statements.
 
在JDBC的操作数据库的过程中,你可以把Statement理解成为指向ResultSet的指针,如果数据库允许返回记数行的话,Statement将指向该计数行,比如
- SET NOCOUNT ON;
 - update TABLEA SET A='aa';--假设共100条数据被修改
 - SELECT * FROM TABLEA;
 
调用callableStatement.execute();后callableStatement指向受影响的计数行,当你再调用rs = callableStatement.getResultSet(); 的时候,结果集rs 为空。 无法查询出表TABLEA 的数据Statement提供了一个getMoreResults()的方法,该方法能将当前Statement "指针" 移动到下一个结果集。如果callableStatement.getUpdateCount()==-1&&getMoreResults()==true的话表明当前statement对象正指向一个真正的结果集。
For Examle:
- package xx.qq.app;
 - import java.sql.CallableStatement;
 - import java.sql.Connection;
 - import java.sql.ResultSet;
 - import org.springframework.beans.factory.BeanFactory;
 - import org.springframework.context.ApplicationContext;
 - import org.springframework.context.support.ClassPathXmlApplicationContext;
 - import com.mchange.v2.c3p0.ComboPooledDataSource;
 - /**
 - * @author Jack Zhang Email:fish2-2@163.com
 - * @date 2011-08-22
 - */
 - public class AppTest {
 - public static void main(String[] args) throws Exception {
 - ApplicationContext context = new ClassPathXmlApplicationContext(
 - new String[] { "applicationContext.xml" });
 - BeanFactory factory = (BeanFactory) context;
 - ComboPooledDataSource dataSource = (ComboPooledDataSource) factory
 - .getBean("dataSource");
 - Connection con = dataSource.getConnection();
 - CallableStatement callableStatement = con
 - .prepareCall("{call GetBasics(?,?)}");
 - callableStatement.setString(1, "w");
 - callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
 - ResultSet rs=null;
 - // 是否有结果集返回
 - boolean hasResultSet = 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
存储过程
- ALTER PROCEDURE GetBasics(
 - @PERSON_NAME VARCHAR(32),
 - @COUNT INT OUT
 - )
 - AS
 - BEGIN
 - SET NOCOUNT ON;
 - update TABLE_A SET NAME='aa';
 - SELECT @COUNTCOUNT = COUNT(*) FROM TABLE_A;
 - update TABLE_A SET NAME='aa';
 - SELECT * FROM TABLE_A;
 - update TABLE_A SET NAME='aa';
 - update TABLE_A SET NAME='aa';
 - SELECT * FROM ORGS;
 - update TABLE_A SET NAME='aa';
 - END
 - GO
 
以上就是JDBC更新计数行及调用存储过程返回多个结果集的过程的详细解释及实例说明,本文就介绍到这里了,希望本次的介绍能够对您有所收获!
【编辑推荐】
                新闻名称:JDBC更新计数行及调用存储过程返回多个结果集详解
                
                新闻来源:http://www.csdahua.cn/qtweb/news16/178116.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网