java读取oracle的存储过程

oracle的PLSQL

CREATE OR REPLACE package chapter_13 as <p>TYPErsISREFCURSOR;</p> <p>procedurefounder(oFieldsoutrs);</p> <p>end;</p> <p>CREATEORREPLACEpackagebodychapter_13as</p> <p>PROCEDUREfounder(oFieldsoutrs)IS</p> <p>BEGIN</p> <p> openoFieldsfor</p> <p> select*fromperson;</p> <p>ENDfounder;</p> <p>end;</p> <p>java代码</p> <p>packagejdbc;</p> <p>importjava.io.*;</p> <p>importjava.sql.*;</p> <p>importjava.text.*;</p> <p>importoracle.jdbc.OracleTypes;</p> <p>publicclassTestStoredProcedures{</p> <p>  Connectionconn;</p> <p>  publicTestStoredProcedures(){</p> <p>    try{</p> <p>      DriverManager.registerDriver(neworacle.jdbc.driver.OracleDriver());</p> <p>      conn=DriverManager.getConnection(</p> <p>          "jdbc:oracle:thin:@localhost:1521:orcl","jola","jola");</p> <p>    }catch(SQLExceptione){</p> <p>      System.err.println(e.getMessage());</p> <p>      e.printStackTrace();</p> <p>    }</p> <p>  }</p> <p>  publicstaticvoidmain(String[]args)throwsException{</p> <p>    newTestStoredProcedures().process();</p> <p>  }</p> <p>  publicvoidprocess()throwsSQLException{</p> <p>   </p> <p>    longstart=0;</p> <p>    longend=0;</p> <p>    CallableStatementcstmt=null;</p> <p>    try{</p> <p>      start=System.currentTimeMillis();</p> <p>      //***SQL92escapesyntax***</p> <p>     </p> <p>      cstmt=conn.prepareCall(</p> <p>          "{call chapter_13.founder(?)}");</p> <p>      cstmt.registerOutParameter(1,OracleTypes.CURSOR);</p> <p>      ResultSetrs=null;</p> <p>      cstmt.execute();</p> <p>      rs=(ResultSet)cstmt.getObject(1);</p> <p>      while(rs.next()){</p> <p>        System.out.println(rs.getString("NAME"));</p> <p>      }</p> <p>      rs.close();</p> <p>      end=System.currentTimeMillis();</p> <p>      System.out.println("Averageelapsedtime="+</p> <p>               (end-start)/8+"milliseconds");</p> <p>    }catch(SQLExceptione){</p> <p>      System.err.println("SQLError:"+e.getMessage());</p> <p>    }finally{</p> <p>      if(cstmt!=null){</p> <p>        try{</p> <p>          cstmt.close();</p> <p>        }catch(SQLExceptionignore){}</p> <p>      }</p> <p>    }</p> <p>  }</p> <p>  protectedvoidfinalize()throwsThrowable{</p> <p>    if(conn!=null){</p> <p>      try{</p> <p>        conn.close();</p> <p>      }catch(SQLExceptionignore){}</p> <p>    }</p> <p>    super.finalize();</p> <p>  }</p> }

相关推荐