Create and use a stored procedure with Play Framework and JPA

妖精的绣舞 提交于 2019-12-05 15:10:39

I don't know how you should create them. Perhaps the OnApplicationStart method is what you need. In my environment the procedures are already in place. We just use Play to invoke them. To invoke stored procedures, you should take a look at the Work interface. By implementing this you can execute statements in the database.

We've created a basic OracleProcedure 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();
    }

    /**
     * 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);
                }
            }
        }
    }
}

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 then call it like this:

StoredProcedureCall procedure = new StoredProcedureCall("your parameter");
session.doWork(procedure);

If you need to call a procedure and retrieve a return value you can use a 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;
    }
}

Take a look at evolutions (http://www.playframework.com/documentation/1.2.7/evolutions) for creating your stored procedures.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!