Java jdbc调用Oracle数据库存储过程,jdbcoracle
分享于 点击 47598 次 点评:208
Java jdbc调用Oracle数据库存储过程,jdbcoracle
http://perfectplan.iteye.com/blog/1582331
Java jdbc调用Oracle数据库存储过程
博客分类:- Java EE
Java jdbc调用Oracle数据库存储过程
一、了解CallableStatement接口
1.callablestatement接口提供了两种调用形式
{?= call <procedure-name>[(<arg1>,<arg2>, ...)]} //包含结果参数的调用形式 如:函数(funciton)
{call <procedure-name>[(<arg1>,<arg2>, ...)]} //不包含结果参数的调用形式 如:存储过程(procedure)
2.callablestatement接口提供的方法
Java代码- void registerOutParameter(int parameterIndex, int sqlType)
- throws SQLException; //在调用存储过程的时候设置输出参数的类型,用于接收输出结果
registerOutParameter接口中有四个该方法的重载实现,具体的可以查看源码了解
Java代码
- setXXX(int parameterIndex,XXX x) //主要用于设置过程调用时候需要的输入参数信息 其中XXX代表对应类型
- getXXX(int x) //主要用于获取过程调用后返回的参数的信息
3.callablestatement接口产生的异常提示
如下源码:
Java代码- /*
- * @exception SQLFeatureNotSupportedException if <code>sqlType</code> is
- * a <code>ARRAY</code>, <code>BLOB</code>, <code>CLOB</code>,
- * <code>DATALINK</code>, <code>JAVA_OBJECT</code>, <code>NCHAR</code>,
- * <code>NCLOB</code>, <code>NVARCHAR</code>, <code>LONGNVARCHAR</code>,
- * <code>REF</code>, <code>ROWID</code>, <code>SQLXML</code>
- * or <code>STRUCT</code> data type and the JDBC driver does not support
- * this data type
- * @see Types
- */
- void registerOutParameter(int parameterIndex, int sqlType)
- ows SQLException;
当我们使用registerOutParameter方法设置输出参数类型的时候,需要注意对于某一些类型是不能够
进行设置的如上中所以提到的类型都会引发SQLFeatureNotSupportedException异常,对于能够支持
的类型可以查看java.sql.Types和oracle.jdbc.OracleTypes
如下源码:
Java代码- /*
- * java.sql.SQLException: 不允许的操作: Ordinal binding and Named binding cannot be
- * combined! at
- * oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at
- * oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) at
- * oracle
- * .jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java
- * :4219) at
- * org.viancent.call.procedure.CallProcedure.main(CallProcedure.java:36)
- */
当我们在给过程设置参数信息的时候,不能够同时使用下标和名称来指定对应参数的。
二、具体的Java代码实现
Java代码- package org.viancent.call.procedure;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Types;
- import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
- import oracle.jdbc.OracleTypes;
- /**
- *
- * @author PerfectPlans
- * @date 2012-7-7
- *
- */
- @SuppressWarnings("unused")
- public class CallProcedure {
- private static Connection conn = null; // 数据库连接对象
- private static CallableStatement cs = null;// 存储过程执行对象
- private static ResultSet rs = null;// 结果集对象
- //静态处理块
- static {
- try {
- // 利用java反射技术获取对应驱动类
- Class.forName("oracle.jdbc.driver.OracleDriver");
- // 获取连接对象
- conn = DriverManager.getConnection(
- "jdbc:oracle:thin:@0.0.0.0:1521:orcl", "scott", "tiger");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- *
- * @Discription 执行有参数,无返回值的存储过程
- * @return void
- * @param conn
- * @throws Exception
- */
- /* 对应的存储过程语句
- --有参数无返回值
- create or replace procedure updateName(byNo in number,useName in varchar2)
- as
- begin
- update emp e set e.ename = useName where e.empno = byNo;
- end;
- */
- public void callProcedureY(Connection conn) throws Exception
- {
- //指定调用的存储过程
- cs = conn.prepareCall("{call updateName(?,?)}");
- cs.setInt(1, 7499);//设置存储过程对应的输入参数
- cs.setString(2, "www");//对应下标从1 开始
- //执行存储过程调用
- cs.execute();
- }
- /**
- *
- * @Discription 执行无参数,无返回值的存储过程
- * @return void
- * @param conn
- * @throws Exception
- */
- /*对应的存储过程语句
- --无参数
- create or replace procedure insertLine
- as
- begin
- insert into emp values(7333,'ALLEN','SAL',7698,to_date('2011/11/11','yyyy-MM-dd'),1600,300,30);
- end;
- */
- public void callProcedure(Connection conn) throws Exception
- {
- //指定调用的存储过程
- cs = conn.prepareCall("{call insertLine}");
- //执行存储过程的调用
- cs.execute();
- }
- /**
- *
- * @Discription 执行有参数,有返回值的存储过程
- * @return void
- * @param conn
- * @throws Exception
- */
- /*对应的存储过程语句
- --有参数,有返回值
- create or replace procedure deleteLine(byNo in number,getCount out number)
- as
- begin
- delete from emp e where e.empno = byNo;
- select count(*) into getCount from emp e;
- end;
- */
- public void callProcedureYY(Connection conn) throws Exception
- {
- //指定调用的存储过程
- cs = conn.prepareCall("{call deleteLine(?,?)}");
- //设置参数
- cs.setInt(1, 7839);
- //这里需要配置OUT的参数新型
- cs.registerOutParameter(2, OracleTypes.NUMBER);
- //执行调用
- cs.execute();
- //输入返回值
- System.out.println(cs.getString(2));
- }
- /**
- *
- * @Discription 执行有参数,返回集合的存储过程
- * @return void
- * @param conn
- * @throws Exception
- */
- /*对应的存储过程语句
- --有参数返回一个列表,使用package
- create or replace package someUtils
- as
- type cur_ref is ref cursor;
- procedure selectRows(cur_ref out someUtils.cur_ref);
- end someUtils;
- create or replace package body someUtils
- as
- procedure selectRows(cur_ref out someUtils.cur_ref)
- as
- begin
- open cur_ref for select * from emp e;
- end selectRows;
- end someUtils;
- */
- public void callProcedureYYL(Connection conn) throws Exception
- {
- //执行调用的存储过程
- cs = conn.prepareCall("{call someUtils.selectRows(?)}");
- //设置返回参数
- cs.registerOutParameter(1, OracleTypes.CURSOR);
- //执行调用
- cs.execute();
- // 获取结果集 结果集是一个Object类型,需要进行强制转换 rs = (ResultSet)
- rs = (ResultSet) cs.getObject(1);
- //输出返回值
- while(rs.next())
- {
- System.out.println(rs.getInt(1)+"\t"+rs.getString(2));
- }
- }
- /**
- *
- * @Discription 执行有参数的函数
- * @return void
- * @param conn
- * @throws Exception
- */
- /*对应的存储过程语句
- --创建函数,有参数
- create or replace function useOther(byNo in number)
- return String
- as
- returnValue char(10);
- begin
- select count(*) into returnValue from emp e where e.empno > byNo;
- return returnValue;
- end;
- */
- public void callProcedureFY(Connection conn) throws Exception
- {
- //指定调用的函数
- cs = conn.prepareCall("{? = call useOther(?)}");
- //配置OUT参数信息
- cs.registerOutParameter(1, OracleTypes.CHAR);
- //配置输入参数
- cs.setInt(2, 1111);
- //执行过程调用
- cs.execute();
- //输入返回值
- System.out.println(cs.getString(1));
- }
- /**
- *
- * @Discription 执行相应关闭操作
- * @return void
- * @param conn
- * @param rs
- */
- public void closeConn(Connection conn,ResultSet rs)
- {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- CallProcedure cp = new CallProcedure();
- try {
- //开启事务管理
- conn.setAutoCommit(true);
- cp.callProcedure(conn);
- cp.callProcedureY(conn);
- cp.callProcedureFY(conn);
- cp.callProcedureYY(conn);
- cp.callProcedureYYL(conn);
- conn.commit();
- } catch (Exception e) {
- e.printStackTrace();
- try {
- conn.rollback();
- } catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- // TODO: handle exception
- }finally
- {
- cp.closeConn(conn, rs);
- }
- }
- }
相关文章
- 暂无相关文章
用户点评