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.