You are on page 1of 2

Example

The following code creates a CallableStatement fron Connection and


passes in the name of the stored procedure.
The parameters for the stored procedure are all marked with question marks.
Then the parameters is set to a value with setXXX method
from CallableStatement.
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
//from ww w. j a v a 2 s . c o m
public class Main {
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_CONNECTION =
"jdbc:oracle:thin:@localhost:1521:YourDatabase";
private static final String DB_USER = "user";
private static final String DB_PASSWORD = "password";

public static void main(String[] argv) throws Exception {


Class.forName(DB_DRIVER);
Connection dbConnection = DriverManager.getConnection(DB_CONNECTION,
DB_USER,
DB_PASSWORD);

CallableStatement callableStatement = null;


String insertStoreProc = "{call insertPERSON(?,?,?,?)}";

java.util.Date today = new java.util.Date();


callableStatement = dbConnection.prepareCall(insertStoreProc);
callableStatement.setInt(1, 1000);
callableStatement.setString(2, "name");
callableStatement.setString(3, "system");
callableStatement.setDate(4, new java.sql.Date(today.getTime()));

callableStatement.executeUpdate();

System.out.println("Record is inserted into PERSON table!");


callableStatement.close();
dbConnection.close();
}
}

You might also like