我不知道你应该如何创建它们。也许 OnApplicationStart 方法是您所需要的。在我的环境中,程序已经到位。我们只是使用 Play 来调用它们。要调用存储过程,您应该看一下Work
接口。通过实现这一点,您可以在数据库中执行语句。
我们创建了一个基本的 OracleProcedure 类:
public class CallOracleProcedure implements Work {
private String anonymousPLSQL;
private String[] parameters;
public CallOracleProcedure(String anonymousPLSQL, String[] parameters) {
this.anonymousPLSQL = anonymousPLSQL;
this.parameters = parameters.clone();
}
/**
* Create a JDBC PreparedStatement and then execute the anonymous
* PL/SQL procedure.
*/
@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);
}
}
}
}
}
对于每个特定的存储过程,您可以扩展此类并通过以下方式将名称和参数传递给构造函数super()
:
public class StoredProcedureCall extends CallOracleProcedure {
public StoredProcedureCall(String param) {
super("package.storedprocedure(?)", new String[] { orgname });
}
}
在您的代码中,您可以这样调用它:
StoredProcedureCall procedure = new StoredProcedureCall("your parameter");
session.doWork(procedure);
如果需要调用过程并检索返回值,可以CallableStatement
在execute()
方法中使用 a:
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;
}
}