相关文章推荐

在开始本文的时候,我们先了解SQL SERVER中的一个命令
SET NOCOUNT ON;

执行该命令 表示不返回计数行,什么是计数行了,比如我们执行 DELETE ,UPDATE,INSERT的时候,对多少条数据进行了修改,计数行的值就是多少

-- 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:[email protected]
 * @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
受影响的行数:262
受影响的行:-1 ,此处返回结果集
受影响的行:262
受影响的行:262
受影响的行:-1,此处返回结果集
受影响的行:262
ALTER PROCEDURE GetBasics( @PERSON_NAME VARCHAR(32), @COUNT INT OUT BEGIN SET NOCOUNT ON; update TABLE_A SET NAME='aa'; SELECT @COUNT = 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'; 在开始本文的时候,我们先了解SQL SERVER中的一个命令  SET NOCOUNT ON;执行该命令 表示不返回计数行,什么是计数行了,比如我们执行 DELETE ,UPDATE,INSERT的时候,对多少条数据进行了修改,计数行的值就是多少-- SET NOCO
第一节:CallableStatement 接口的引入 CallableStatement 主要是调用数据库中的存储过程,CallableStatement 也是 Statement 接口的子接口。在使用CallableStatement 时可以接收存储过程返回值。 第二节:使用 CallableStatement 接口调用存储过程 void registerOutParameter(int parameterIndex, int sqlType) 按顺序位置 parameterIndex 将 OU
4. 通过调用CallableStatement对象的setXXX()方法为存储过程传递参数。 5. 调用CallableStatement对象的execute()方法执存储过程。 6. 通过调用CallableStatement对象的getXXX()方法获取存储过程返回的结果。 7. 最后,关闭数据库连接对象和CallableStatement对象。 例如,调用一个名为my_function的存储过程,该过程有两个输入参数和一个输出参数,可以使用以下Java代码: import java.sql.*; public class CallFuncDemo { public static void main(String[] args) throws SQLException { Connection conn = null; CallableStatement stmt = null; try { conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "username", "password"); stmt = conn.prepareCall("{? = call my_function(?, ?)}"); stmt.registerOutParameter(1, Types.INTEGER); stmt.setInt(2, 100); stmt.setInt(3, 200); stmt.execute(); int result = stmt.getInt(1); System.out.println("The result is " + result); } catch(SQLException e) { e.printStackTrace(); } finally { if(stmt != null) { stmt.close(); if(conn != null) { conn.close(); 在这个例子中,我们创建了一个数据库连接对象conn,并使用prepareCall()方法创建一个CallableStatement对象stmt来调用存储过程。接着,我们使用registerOutParameter()方法为输出参数注册类型,并使用setXXX()方法为输入参数设置值。调用execute()方法执存储过程。最后,我们使用getInt()方法获取输出参数的值。注意,在调用完毕后需要关闭数据库连接对象和CallableStatement对象。
 
推荐文章