-1

我需要有关如何使用 EclipseLink 检索 plsql 匿名变量值的指导。下面我留下一个示例查询。我知道我可以使用函数和存储过程,但是对数据库有限制。

DataReadQuery query = new DataReadQuery();
SQLCall sqlCall = new SQLCall();
StringBuilder plsql = new StringBuilder();

plsql.append("DECLARE ");
plsql.append("\n");
plsql.append("out_variable "); 
plsql.append("foo.bar");
plsql.append(".");
plsql.append("number_field");
plsql.append("%TYPE;");
plsql.append("\n");
plsql.append("BEGIN ");
plsql.append("\n");
plsql.append("UPDATE ");
plsql.append("number_field");
plsql.append(" SET number_field = (number_field+1)");
plsql.append(" WHERE ");
plsql.append(" key_field = ");
plsql.append(Key);
plsql.append(" ");
plsql.append(" RETURNING ");
plsql.append(" number_field ");
plsql.append(" INTO ");
plsql.append(" out_variable ; ");
plsql.append("\n");
plsql.append("END; \n");

sqlCall.setQueryString(plsql.toString());
sqlCall.setQuery(query);
query.setCall(sqlCall);

Session session = 
JpaHelper.getEntityManager(getEntityManager()).getActiveSession();
Object queryResult = session.executeQuery(query);
4

1 回答 1

0

谢谢您的帮助。但是,我已经找到了问题的解决方案。

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class FetchNumberField {

  public Integer getNext(){

    Integer result = null;      
    Connection connection = null;
    CallableStatement cs = null;
    Context context = null;

    try{

        StringBuilder plsql = new StringBuilder();
        plsql.append("BEGIN ");
        plsql.append("\n");
        plsql.append(" UPDATE ");
        plsql.append(DataBaseUtils.SCHEMA + "." + DataBaseUtils.TABLE_NAME);
        plsql.append(" SET NUMBER_FIELD = (NUMBER_FIELD+1) ");
        plsql.append(" WHERE ");
        plsql.append(" NUMBER_FIELD = ");
        plsql.append(numerField);
        plsql.append(" ");
        plsql.append(" RETURNING ");
        plsql.append(" NUMBER_FIELD ");
        plsql.append(" INTO ");
        plsql.append(" ? ; ");
        plsql.append("\n");
        plsql.append("END; \n");

        context = new InitialContext();
        DataSource dt = (DataSource)context.lookup("java:/jndi/foobar");
        connection =    dt.getConnection(); 
        cs = connection.prepareCall(plsql.toString());  
        cs.registerOutParameter(1,Types.INTEGER);
        cs.execute();
        result = (Integer)cs.getObject(1);

    }catch(Exception ex){
        ex.printStackTrace();

    }finally{
        if( cs != null ){
            try{
                cs.close();
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
        if( connection != null ){
            try{
                connection.close();
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        if( context != null ){
            try{
                context.close();
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

    return result;
 }
}
于 2013-05-28T15:05:17.007 回答