どうやって作成すればいいのかわかりません。おそらく、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;
}
}