Java jdbc调用Oracle数据库存储过程 Java jdbc调用Oracle数据库存储过程

http://perfectplan.iteye.com/blog/1582331

Java jdbc调用Oracle数据库存储过程

 

 一、了解CallableStatement接口

1.callablestatement接口提供了两种调用形式

{?= call <procedure-name>[(<arg1>,<arg2>, ...)]}  //包含结果参数的调用形式   如:函数(funciton)
{call <procedure-name>[(<arg1>,<arg2>, ...)]}  //不包含结果参数的调用形式     如:存储过程(procedure)

 

2.callablestatement接口提供的方法

Java代码    收藏代码
  1. void registerOutParameter(int parameterIndex, int sqlType)  
  2.         throws SQLException; //在调用存储过程的时候设置输出参数的类型,用于接收输出结果  

   registerOutParameter接口中有四个该方法的重载实现,具体的可以查看源码了解

    

Java代码    收藏代码
  1. setXXX(int parameterIndex,XXX x) //主要用于设置过程调用时候需要的输入参数信息  其中XXX代表对应类型  
Java代码    收藏代码
  1. getXXX(int x) //主要用于获取过程调用后返回的参数的信息  

 

3.callablestatement接口产生的异常提示

如下源码:

扫描二维码关注公众号,回复: 512028 查看本文章
Java代码    收藏代码
  1. /* 
  2.  * @exception SQLFeatureNotSupportedException if <code>sqlType</code> is 
  3.  * a <code>ARRAY</code>, <code>BLOB</code>, <code>CLOB</code>,  
  4.  * <code>DATALINK</code>, <code>JAVA_OBJECT</code>, <code>NCHAR</code>,  
  5.  * <code>NCLOB</code>, <code>NVARCHAR</code>, <code>LONGNVARCHAR</code>, 
  6.  *  <code>REF</code>, <code>ROWID</code>, <code>SQLXML</code> 
  7.  * or  <code>STRUCT</code> data type and the JDBC driver does not support 
  8.  * this data type 
  9.  * @see Types  
  10.  */  
  11. void registerOutParameter(int parameterIndex, int sqlType)  
  12. ows SQLException;  

   当我们使用registerOutParameter方法设置输出参数类型的时候,需要注意对于某一些类型是不能够

        进行设置的如上中所以提到的类型都会引发SQLFeatureNotSupportedException异常,对于能够支持

        的类型可以查看java.sql.Types和oracle.jdbc.OracleTypes

 

如下源码:

Java代码    收藏代码
  1. /* 
  2.  * java.sql.SQLException: 不允许的操作: Ordinal binding and Named binding cannot be 
  3.  * combined! at 
  4.  * oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at 
  5.  * oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) at 
  6.  * oracle 
  7.  * .jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java 
  8.  * :4219) at 
  9.  * org.viancent.call.procedure.CallProcedure.main(CallProcedure.java:36) 
  10.  */  

  当我们在给过程设置参数信息的时候,不能够同时使用下标和名称来指定对应参数的。

二、具体的Java代码实现

Java代码    收藏代码
  1. package org.viancent.call.procedure;  
  2.   
  3. import java.sql.CallableStatement;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Types;  
  9. import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;  
  10. import oracle.jdbc.OracleTypes;  
  11.   
  12. /** 
  13.  *  
  14.  * @author PerfectPlans 
  15.  * @date 2012-7-7 
  16.  *  
  17.  */  
  18. @SuppressWarnings("unused")  
  19. public class CallProcedure {  
  20.     private static Connection conn = null// 数据库连接对象  
  21.     private static CallableStatement cs = null;// 存储过程执行对象  
  22.     private static ResultSet rs = null;// 结果集对象  
  23.     //静态处理块  
  24.     static {  
  25.         try {  
  26.             // 利用java反射技术获取对应驱动类  
  27.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  28.             // 获取连接对象  
  29.             conn = DriverManager.getConnection(  
  30.                     "jdbc:oracle:thin:@0.0.0.0:1521:orcl""scott""tiger");  
  31.         } catch (Exception e) {  
  32.             // TODO Auto-generated catch block  
  33.             e.printStackTrace();  
  34.         }  
  35.     }  
  36.   
  37.     /** 
  38.      *  
  39.      *  @Discription 执行有参数,无返回值的存储过程 
  40.      *  @return         void 
  41.      *  @param conn 
  42.      *  @throws Exception 
  43.      */  
  44.     /* 对应的存储过程语句 
  45.     --有参数无返回值 
  46.     create or replace procedure updateName(byNo in number,useName in varchar2) 
  47.     as 
  48.     begin 
  49.            update emp e set e.ename = useName where e.empno = byNo; 
  50.     end; 
  51.     */  
  52.     public void callProcedureY(Connection conn) throws Exception  
  53.     {  
  54.         //指定调用的存储过程  
  55.         cs = conn.prepareCall("{call updateName(?,?)}");  
  56.         cs.setInt(17499);//设置存储过程对应的输入参数  
  57.         cs.setString(2"www");//对应下标从1 开始  
  58.         //执行存储过程调用  
  59.         cs.execute();  
  60.     }  
  61.   
  62.     /** 
  63.      *  
  64.      *  @Discription 执行无参数,无返回值的存储过程 
  65.      *  @return         void 
  66.      *  @param conn 
  67.      * @throws Exception  
  68.      */  
  69.     /*对应的存储过程语句 
  70.     --无参数 
  71.     create or replace procedure insertLine 
  72.     as 
  73.     begin 
  74.           insert into emp values(7333,'ALLEN','SAL',7698,to_date('2011/11/11','yyyy-MM-dd'),1600,300,30); 
  75.     end; 
  76.     */  
  77.     public void callProcedure(Connection conn) throws Exception  
  78.     {  
  79.         //指定调用的存储过程  
  80.         cs = conn.prepareCall("{call insertLine}");  
  81.         //执行存储过程的调用  
  82.         cs.execute();  
  83.     }  
  84.       
  85.     /** 
  86.      *  
  87.      *  @Discription 执行有参数,有返回值的存储过程 
  88.      *  @return         void 
  89.      *  @param conn 
  90.      * @throws Exception  
  91.      */  
  92.     /*对应的存储过程语句 
  93.      --有参数,有返回值 
  94.     create or replace procedure deleteLine(byNo in number,getCount out number) 
  95.     as 
  96.     begin 
  97.            delete from emp e where e.empno = byNo; 
  98.            select count(*) into getCount from emp e; 
  99.     end; 
  100.      */  
  101.     public void callProcedureYY(Connection conn) throws Exception  
  102.     {  
  103.         //指定调用的存储过程  
  104.         cs = conn.prepareCall("{call deleteLine(?,?)}");  
  105.         //设置参数  
  106.         cs.setInt(17839);  
  107.         //这里需要配置OUT的参数新型  
  108.         cs.registerOutParameter(2, OracleTypes.NUMBER);  
  109.         //执行调用  
  110.         cs.execute();  
  111.         //输入返回值  
  112.         System.out.println(cs.getString(2));  
  113.     }  
  114.       
  115.     /** 
  116.      *  
  117.      *  @Discription 执行有参数,返回集合的存储过程 
  118.      *  @return         void 
  119.      *  @param conn 
  120.      * @throws Exception  
  121.      */  
  122.     /*对应的存储过程语句 
  123.      --有参数返回一个列表,使用package 
  124.         create or replace package someUtils 
  125.         as 
  126.                type cur_ref is ref cursor; 
  127.                procedure selectRows(cur_ref out someUtils.cur_ref); 
  128.           end someUtils; 
  129.            
  130.         create or replace package body someUtils 
  131.         as  
  132.                procedure selectRows(cur_ref out someUtils.cur_ref) 
  133.                  as 
  134.                    begin 
  135.                          open cur_ref for select * from emp e; 
  136.                    end selectRows; 
  137.         end someUtils; 
  138.      */  
  139.     public void callProcedureYYL(Connection conn) throws Exception  
  140.     {  
  141.         //执行调用的存储过程  
  142.         cs = conn.prepareCall("{call someUtils.selectRows(?)}");  
  143.         //设置返回参数  
  144.         cs.registerOutParameter(1, OracleTypes.CURSOR);  
  145.         //执行调用  
  146.         cs.execute();  
  147.         // 获取结果集 结果集是一个Object类型,需要进行强制转换 rs = (ResultSet)  
  148.         rs = (ResultSet) cs.getObject(1);  
  149.         //输出返回值  
  150.         while(rs.next())  
  151.         {  
  152.             System.out.println(rs.getInt(1)+"\t"+rs.getString(2));  
  153.         }  
  154.     }  
  155.       
  156.     /** 
  157.      *  
  158.      *  @Discription 执行有参数的函数 
  159.      *  @return         void 
  160.      *  @param conn 
  161.      *  @throws Exception  
  162.      */  
  163.     /*对应的存储过程语句 
  164.      --创建函数,有参数 
  165.     create or replace function useOther(byNo in number) 
  166.     return String 
  167.     as  
  168.         returnValue char(10); 
  169.            begin 
  170.               select count(*) into returnValue from emp e where e.empno > byNo; 
  171.               return returnValue; 
  172.            end; 
  173.      */  
  174.     public void callProcedureFY(Connection conn) throws Exception  
  175.     {  
  176.         //指定调用的函数  
  177.         cs = conn.prepareCall("{? = call useOther(?)}");  
  178.         //配置OUT参数信息  
  179.         cs.registerOutParameter(1, OracleTypes.CHAR);  
  180.         //配置输入参数  
  181.         cs.setInt(21111);  
  182.         //执行过程调用  
  183.         cs.execute();  
  184.         //输入返回值  
  185.         System.out.println(cs.getString(1));  
  186.     }  
  187.       
  188.     /** 
  189.      *  
  190.      *  @Discription 执行相应关闭操作 
  191.      *  @return         void 
  192.      *  @param conn 
  193.      *  @param rs 
  194.      */  
  195.     public void closeConn(Connection conn,ResultSet rs)  
  196.     {  
  197.         if (rs != null) {  
  198.             try {  
  199.                 rs.close();  
  200.             } catch (SQLException e) {  
  201.                 // TODO Auto-generated catch block  
  202.                 e.printStackTrace();  
  203.             }  
  204.         }  
  205.         if (conn != null) {  
  206.             try {  
  207.                 conn.close();  
  208.             } catch (SQLException e) {  
  209.                 // TODO Auto-generated catch block  
  210.                 e.printStackTrace();  
  211.             }  
  212.         }  
  213.     }  
  214.       
  215.       
  216.     public static void main(String[] args) {  
  217.         CallProcedure cp = new CallProcedure();  
  218.         try {  
  219.             //开启事务管理  
  220.             conn.setAutoCommit(true);  
  221.             cp.callProcedure(conn);  
  222.             cp.callProcedureY(conn);  
  223.             cp.callProcedureFY(conn);  
  224.             cp.callProcedureYY(conn);  
  225.             cp.callProcedureYYL(conn);  
  226.             conn.commit();  
  227.         } catch (Exception e) {  
  228.             e.printStackTrace();  
  229.             try {  
  230.                 conn.rollback();  
  231.             } catch (Exception e1) {  
  232.                 // TODO Auto-generated catch block  
  233.                 e1.printStackTrace();  
  234.             }  
  235.             // TODO: handle exception  
  236.         }finally  
  237.         {  
  238.             cp.closeConn(conn, rs);  
  239.         }  
  240.     }  
  241. }  
 

 

 

猜你喜欢

转载自ychg2009.iteye.com/blog/2149630