Java中取得Oracle存储过程输出参数中的值

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class OracleProcedureWithRetVal {

 /**
  * Oracle带有输出参数的存储过程
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
   //加载驱动
   Class.forName("oracle.jdbc.driver.OracleDriver"); 
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  Connection con = null;
  CallableStatement cs = null;
  try {
   //得到连接
   con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
   //创建CallableStatement对象
   cs = con.prepareCall("{call fly_pro5(?,?)}");
   //对参数赋值
   cs.setInt(1, 7369);
   cs.registerOutParameter(2,oracle.jdbc.OracleTypes.VARCHAR);
   //执行存储过程调用
   cs.execute();
   String ename = cs.getString(2);
   System.out.println("7369的姓名是" + ename);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   //关闭资源
   try {
    cs.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    con.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

}

相关推荐