简体   繁体   中英

Get value of plsql procedure from java

I want to get parameters from PLSQL Proceduer.

PROCEDURE BALANCE(requestDate in date, uniqueId in number, field1 out varchar2, field2 out varchar2)
AS 
BEGIN
    field1 := '110';
    field2 := '100';
END BALANCE;

And this is java code:

 javax.persistence.Query query = pm.getEntityManager().createNativeQuery(" declare a number(10);" +
            " b number(10);" +
            " begin BALANCE(:date,:id,a,b); end;");
    query.setParameter("date", transaction.getTransactionId().getRequestDate());
    query.setParameter("id", transaction.getTransactionId().getId());
    query.executeUpdate();

I want to use 'a' and 'b' in java program and I don't know how to get 'a' and 'b'? Thanks.

Actually you don't need hibernate but JDBC. You don't have aany Entity for the out params.

TO call them just register the ut parameters and get them back after calling statement.

String getDBUSERByUserIdSql = "{call getDBUSERByUserId(?,?,?,?)}";
callableStatement = dbConnection.prepareCall(getDBUSERByUserIdSql);
callableStatement.setInt(1, 10);
callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR);
callableStatement.registerOutParameter(3, java.sql.Types.VARCHAR);
callableStatement.registerOutParameter(4, java.sql.Types.DATE);

// execute getDBUSERByUserId store procedure
callableStatement.executeUpdate();

String userName = callableStatement.getString(2);
String createdBy = callableStatement.getString(3);
Date createdDate = callableStatement.getDate(4);

The code from the example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM