Today the Oracle in the package and the cursor learning, no nonsense, some of the online code is wrong, copied to copy, on their own practice under, make a record. Direct, on the code
Create your own package with Plsql, which is divided into Baotou and the package body.
1. Baotou is as follows:
1 CREATE OR REPLACE Package Javalinktest 2 is 3 is CURSOR; -- Defining Cursors 4 PROCEDURE test_cursor (INPUT STRING, cursor_back out cursor_type); 5 6 END Javalinktest;
2. Package body as follows:
1 CREATE OR REPLACEPackage BODY Javalinktest is2 3 PROCEDUREtest_cursor (INPUT STRING, cursor_back out Cursor_type)4 is5 BEGIN6 IFINPUT= 'Material' Then7 OPENCursor_back for SELECT * fromT_bd_materialWHERECfissync= 1;8 ELSE9 OPENCursor_back for SELECT * fromT_bd_customerWHERECfissync= 1;Ten One END IF; A - ENDTest_cursor; - ENDJavalinktest;
3.java environment, when using the Eclispe,oracle 11g,java code as follows, the need for the small partners to introduce themselves to the JDBC package it
1 Packagecursortest;2 3 Importjava.sql.CallableStatement;4 Importjava.sql.Connection;5 ImportJava.sql.DriverManager;6 ImportJava.sql.ResultSet;7 Importjava.sql.SQLException;8 Importjava.sql.Statement;9 Importoracle.jdbc.OracleTypes;Ten ImportOracle.jdbc.oracore.OracleType; One A Public classCursoruse { - StaticResultSet rs =NULL; - StaticStatement stmt =NULL; the StaticConnection conn =NULL; - Staticcallablestatement proc =NULL; - Static inti; - + Public Static voidMain (string[] args) { - + Try { A //Load Driver atClass.forName ("Oracle.jdbc.driver.OracleDriver"); - //establish a physical connection to the database -conn =Drivermanager.getconnection ( -"Jdbc:oracle:thin:@127.0.0.1:1521:orcl", "Easdb", "Easdb"); - //Call Cursor -proc = Conn.preparecall ("Call Javalinktest. Test_cursor (?,?) "); inProc.setstring (1, "hhe"); -Proc.registeroutparameter (2, oracletypes.cursor); to Proc.execute (); +rs = (ResultSet) proc.getobject (2); - the while(Rs.next ()) { *I+=1; $System.out.println ("FID:" + rs.getstring (1) +Panax Notoginseng"Code:" +rs.getstring ("Fnumber") + "Name:" +rs.getstring ("Fname_l2")); - the } +SYSTEM.OUT.PRINTLN ("Return result set: Total" +i+ "Row"); A}Catch(ClassNotFoundException e) { the //TODO auto-generated Catch block + e.printstacktrace (); -}Catch(SQLException e) { $ //TODO auto-generated Catch block $ e.printstacktrace (); -}finally { - if(rs! =NULL) { the Try { - rs.close ();Wuyi}Catch(SQLException e) { the //TODO auto-generated Catch block - e.printstacktrace (); Wu } - } About if(stmt! =NULL) { $ Try { - stmt.close (); -}Catch(SQLException e) { - //TODO auto-generated Catch block A e.printstacktrace (); + } the } - if(Conn! =NULL) { $ Try { the conn.close (); the}Catch(SQLException e) { the //TODO auto-generated Catch block the e.printstacktrace (); - } in } the } the About } the the}
The results of the operation are as follows:
The Java implementation invokes cursors and packages in Oracle