Java code calls a database stored procedure

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.