Because there is write Java code to call the database, the sense should be the Java call stored procedures also write, so I add the article!
Package testspring;
Import java.sql.CallableStatement;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import oracle.jdbc.OracleCallableStatement;
Import Oracle.jdbc.OracleTypes;
public class Test1 {
public static void Main (string[] args) {
Connection Con=null;
PreparedStatement Pre=null;
ResultSet Res=null;
CallableStatement Call=null;
try {
Class.forName ("Oracle.jdbc.driver.OracleDriver");
String url= "JDBC:ORACLE:THIN:@192.168.224.87:1523:ORCL";
String username= "Gb_report";
String password= "gb_report123$";
SYSTEM.OUT.PRINTLN ("Connection database succeeded! ");
Con=drivermanager.getconnection (Url,username,password);
Call=con.preparecall ("{Call P_common. Query_province_page (?,?,?,?,?,?,?,?,?,?,?)} "); /p_common is the package name, Query_province_page is the name of the stored procedure,? Delegate parameters
Call.registeroutparameter (4, oracletypes.number);//The type of the parameter subscript 4 and the return value
Call.registeroutparameter (5, Oracletypes.number);
Call.registeroutparameter (7, Oracletypes.number);
Call.registeroutparameter (8, Oracletypes.integer);
Call.registeroutparameter (9, Oracletypes.varchar);
Call.registeroutparameter (10,oracletypes.varchar);
Call.registeroutparameter (11,oracletypes.cursor);//Note Here The return value type is a cursor
Call.setstring (1, "");//parameter subscript 1, input parameter type varchar (java corresponding string)
Call.setstring (2, "");
Call.setstring (3, "");
Call.setstring (6, "");
Perform
Call.execute ();
res = (ResultSet) call.getobject (11);//The return value labeled 11 for the Accept parameter is the cursor type, with the ResultSet
while (Res.next ()) {
SYSTEM.OUT.PRINTLN ("Administrative ID:" +res.getstring ("ITEMID") + "; Province name:" +res.getstring ("itemname") + ";");
}
} catch (Exception e) {
E.printstacktrace ();
}finally{
try {
if (res!=null) {
Res.close ();
}else if (pre!=null) {
Pre.close ();
}else if (con!=null) {
Con.close ();
}
System.out.println ("Close the connection! ");
} catch (Exception E2) {
E2.printstacktrace ();
}
}
}
}
Java code calls a database stored procedure