我不知道你應該如何創建它們。也許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
:
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;
}
}
謝謝!你知道如何從過程中檢索SELECT查詢的結果嗎? – 2013-03-06 12:21:03
我已經添加了另一個返回值的例子。我希望那是你的意思。 – evandongen 2013-03-06 14:17:23
謝謝你的回答。您的示例是否適用於返回多個結果的SELECT? – 2013-03-06 17:56:17