I do not know how you should create them. Perhaps the OnApplicationStart method is what you need. Procedures already exist in my environment. We just use Play to call them. To call stored procedures, you should take a look at the Work interface. By doing this, you can follow the instructions in the database.
We created the OracleProcedure base class:
public class CallOracleProcedure implements Work { private String anonymousPLSQL; private String[] parameters; public CallOracleProcedure(String anonymousPLSQL, String[] parameters) { this.anonymousPLSQL = anonymousPLSQL; this.parameters = parameters.clone(); } @Override public void execute(Connection connection) { PreparedStatement statement = null; try { statement = connection.prepareStatement("begin " + anonymousPLSQL + "; end;"); if (parameters != null) { int i = 1; for (String param : parameters) { statement.setString(i++, param); } } statement.executeUpdate(); } catch (SQLException e) { Logger.error("Error performing anonymous pl/sql statement: '%s', with parameters: '%s' - catched error '%s'", anonymousPLSQL, parameters, e); } finally { if (statement != null) { try { statement.close(); } catch (Exception e) { Logger.error("Error closing statement: %s", e); } } } } }
For each specific stored procedure, you can extend this class and pass the name and parameters to the constructor via super() :
public class StoredProcedureCall extends CallOracleProcedure { public StoredProcedureCall(String param) { super("package.storedprocedure(?)", new String[] { orgname }); } }
In your code, you can call it like this:
StoredProcedureCall procedure = new StoredProcedureCall("your parameter"); session.doWork(procedure);
If you need to call a procedure and get the return value, you can use CallableStatement in the execute() method:
public class ProcedureWithReturnValue implements Work { private final String parameter; private String returnValue = null; public ProcedureWithReturnValue (final String parameter) { this.parameter = parameter; } @Override public void execute(Connection connection) { CallableStatement statement = null; try { statement = connection.prepareCall("begin ? := package.procedure(?); end;"); statement.registerOutParameter(1, OracleTypes.VARCHAR); statement.setString(2, parameter); statement.execute(); returnValue = statement.getString(1); } catch (SQLException e) { Logger.error("Error getting return value - catched error '%s'", e); } } public String getReturnValue() { return returnValue; } }
source share