使用jdbc调用oracle存储过程(返回cursor)的错误及解决方法- -

时间:2021-11-11 15:50:34

copyright (c) written by bigfou on 200504

--接上回书说:
 
2.使用Jdbc调用存储过程
 2.1 一个问题:
  使用下面的代码访问oracle的存储过程(代码如上)
  Class.forName("oracle.jdbc.driver.OracleDriver");
  conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","...");
  conn.setAutoCommit(false);
  CallableStatement cstat = conn.prepareCall("{ ? = call bigfou.DemoReturnCursor(?) }");
  cstat.registerOutParameter(1,Types.OTHER);  
  cstat.setInt(2,1);
  
  //cstat.setObject(2,rs);
  cstat.execute();
  out.println(cstat.getObject(1));
  
  报错:java.sql.SQLException: 无效的列类型
  why???{xp + jdk142 + oracle817 + tomcat50}
  
  
 2.2 解决!!!
    
  {不是jdbc驱动版本的问题,看下面的java代码你就明白了:).好辛苦啊,
   尝试下载了N个版本的jdbc驱动都不行,
  在google上搜索了好几好几分钟,终于over了}
  Class.forName("oracle.jdbc.driver.OracleDriver");
  conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","...");
  conn.setAutoCommit(false);
  CallableStatement cstat = conn.prepareCall("{ ? = call bigfou.DemoReturnCursor(?) }");
  cstat.registerOutParameter(1,oracle.jdbc.driver.OracleTypes.CURSOR);//Types.OTHER); 
  cstat.setInt(2,1);
  cstat.execute();  
  OracleCallableStatement ocstat = (OracleCallableStatement)cstat;  
  ResultSet rs = null;
  rs = ocstat.getCursor(1);
  while(rs.next())
  {   
   out.println(rs.getInt(1));
  }
  rs.close();
  cstat.close();