JDBC調(diào)用帶輸出參數(shù)的存儲(chǔ)過程的實(shí)例解析
作者:myxx520
本文我們主要介紹了JDBC調(diào)用帶輸出參數(shù)的存儲(chǔ)過程來完成統(tǒng)計(jì)分頁(yè)數(shù)據(jù)總數(shù)的功能的代碼實(shí)例,通過這個(gè)實(shí)例,讓我們一起來了解一下JDBC調(diào)用帶輸出參數(shù)的存儲(chǔ)過程的方法吧,希望能夠?qū)δ兴鶐椭?/div>
在用JDBC調(diào)用存儲(chǔ)過程來實(shí)現(xiàn)分頁(yè)的時(shí)候,因?yàn)橐y(tǒng)計(jì)分頁(yè)數(shù)據(jù)的總數(shù),在存儲(chǔ)過程中想到了使用一個(gè)輸出參數(shù)來完成這樣的功能,于是就用JDBC調(diào)用帶輸出參數(shù)的存儲(chǔ)過程來實(shí)現(xiàn)這一功能。剛開始還出了點(diǎn)問題,如下:
- 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"));
- }
如果先調(diào)用 int out = callableStatement.getInt(2);的話,結(jié)果集就會(huì)被關(guān)閉,并拋出
- Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: 結(jié)果集已關(guān)閉。
- 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)
就出現(xiàn)了上面的異?,F(xiàn)象。
解決方法是將其改為:
- ResultSet rs = callableStatement.executeQuery();
- while (rs.next()) {
- System.out.println(rs.getObject("PERSON_NAME"));
- }
- int out = callableStatement.getInt(2);
這樣就OK了。
附上簡(jiǎn)單的存儲(chǔ)過程及源碼:
- 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 = 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)); // } // } // });
- *
- */
存儲(chǔ)過程
- ALTER PROCEDURE GetBasics(
- @PERSON_NAME VARCHAR(32),
- @COUNT INT OUT
- )
- AS
- BEGIN
- SELECT @COUNTCOUNT = COUNT(*) FROM P_BASIC;
- SELECT * FROM P_BASIC
- END
- GO
以上就是JDBC調(diào)用帶輸出參數(shù)的存儲(chǔ)過程來完成分頁(yè)并統(tǒng)計(jì)分頁(yè)總數(shù)的全部過程,本文就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!
【編輯推薦】
責(zé)任編輯:趙鵬
來源:
CSDN博客


相關(guān)推薦




