JDBC調用帶輸出參數的存儲過程

 

 前段時間項目中遇到存儲過程分頁的問題,因爲分頁的時候要統計分頁數據的總數,在存儲過程中想到了使用一個輸出參數,但剛開是出現了點小問題
callableStatement.setString(1, "w");
		callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
		ResultSet rs = callableStatement.executeQuery();
		int out = callableStatement.getInt(2);
		while (rs.next()) {
			System.out.println(rs.getObject("PERSON_NAME"));
		}

如果先調用 int out = callableStatement.getInt(2);的話,結果集就會被關閉,並拋出
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: 結果集已關閉。
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerResultSet.checkClosed(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(Unknown Source)
	at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:2859)
	at xx.qq.app.AppTest.main(AppTest.java:24)


異常

 

改爲

ResultSet rs = callableStatement.executeQuery();
  
  while (rs.next()) {
   System.out.println(rs.getObject("PERSON_NAME"));
  }
  int out = callableStatement.getInt(2);


 

這樣就OK了

 

附上簡單的存儲過程及源碼

 

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 = callableStatement.executeQuery();
		
		while (rs.next()) {
			System.out.println(rs.getObject("PERSON_NAME"));
		}
		int out = callableStatement.getInt(2);
		//int out = callableStatement.getInt(2);
		System.out.println(out);
		if (rs != null)
			rs.close();
		if (callableStatement != null)
			callableStatement.close();
		if (con != null)
			con.close();
	}
}
/**
 * 
 * QueryTemplate queryTemplate =(QueryTemplate)factory.getBean("queryTemplate"); //
 * queryTemplate.query(new Query(){ // public void executeQuery(Connection con,
 * Statement st, ResultSet rs) throws Exception { // String sql ="SELECT * FROM
 * P_BASIC"; // rs = st.executeQuery(sql); // while(rs.next()) // { //
 * System.out.println(rs.getObject(5)); // } // } // });
 * 
 */

存儲過程

 



ALTER PROCEDURE GetBasics(
	@PERSON_NAME VARCHAR(32),
	@COUNT INT OUT
	)
AS
BEGIN
	SELECT @COUNT = COUNT(*) FROM P_BASIC;
	SELECT * FROM P_BASIC
END
GO

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章