How to call a stored procedure in Java

Source: Internet
Author: User

The previous development is to write directly to the SQL statements, the recent project leader required to interact with the database to use stored procedures, at the beginning also met some difficulties, but still finished, now in this my results tell you, and we share, there is a good saying ah, a person music that is not called music, everyone le that is really happy, Now let's tell you how to call a stored procedure in a Java class, I use an Oracle database, build a stored procedure, and then build a class

Package com.rj.service;

Import java.sql.*;
/**
* Invoke stored Procedure test
* @author Administrator
*
*/
public class Test {
Public Test () {
}
public void TT () {


}
public static void Main (string[] args) {
String Driver = "Oracle.jdbc.driver.OracleDriver";
String strURL = "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORA9";
Connection conn = null;
CallableStatement cstmt = null;

try {
Class.forName (driver);
conn = Drivermanager.getconnection (strURL, "Scott", "Tiger");
callablestatement proc = null;
Call stored procedure UpdateUser is procedure name,? Represents the number of parameters
String sql = "{call UpdateUser (?)}";
proc = conn.preparecall (sql);
Proc.setstring (1, "001");
Proc.execute ();
}
catch (SQLException ex2) {
Ex2.printstacktrace ();
}
catch (Exception ex2) {
Ex2.printstacktrace ();
}
Finally
{
try {
if (conn!=null)
{
Conn.close ();
}
}
catch (SQLException Ex1) {
Ex1.printstacktrace ();
}
}
}
}

If there is not clear to ask, of course, I am not saying that I have many bad, I just want to let me have the same experience of people do not detours, can help everyone, nothing more.

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.