JDBC calls the Oracle Stored ProcedureExamples of returning result sets and obtaining output parameters are the main content we will introduce in this article. Next we will introduce this process. Let's take a look at it.
- 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 st = con. prepareCall ("{call package_user.PERSON_QUERY (?,?)} ");
- // St. setString (1 ,"");
- St. registerOutParameter (1, oracle. jdbc. OracleTypes. CURSOR );
- St. registerOutParameter (2, oracle. jdbc. OracleTypes. INTEGER );
- St.exe cute ();
- ResultSet rs = null;
- Rs = (ResultSet) st. getObject (1 );
- While (rs. next ())
- {
- System. out. println (rs. getObject (1) + "" + rs. getObject (2 ));
- }
- Int count = st. getInt (2); // get the output parameter
- System. err. println (count );
- If (rs! = Null)
- Rs. close ();
- If (st! = Null)
- St. close ();
- If (con! = Null)
- Con. close ();
- }
- }
- CREATE OR REPLACE PACKAGE package_user AS
- TYPE MY_RESULTSET_CURSOR IS REF CURSOR;
- PROCEDURE PERSON_QUERY(RS OUT MY_RESULTSET_CURSOR,C OUT INTEGER);
- END;
- create or replace package body package_user AS
- PROCEDURE PERSON_QUERY(RS OUT MY_RESULTSET_CURSOR,C OUT INTEGER)
- AS
- BEGIN
- OPEN RS FOR select * from emp;
- update A set ename='AAA';
- SELECT COUNT(*) INTO C from emp;
- END PERSON_QUERY;
- END;
The above is the whole process of JDBC calling the Oracle stored procedure to return the result set and obtain the output parameters. This article will introduce it here. I hope this introduction will be helpful to you!