java - how to return value in anonymous pl sql using eclipselink -
i need guidance on how retrieve value of variable in plsql anonymous using eclipselink. below leave sample query. know use functions , stored procedures, there restrictions on database.
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(" "); plsql.append(" key_field = "); plsql.append(key); plsql.append(" "); plsql.append(" returning "); plsql.append(" number_field "); plsql.append(" "); 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);
thanks help. however, i've found solution problem.
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(" "); plsql.append(" number_field = "); plsql.append(numerfield); plsql.append(" "); plsql.append(" returning "); plsql.append(" number_field "); plsql.append(" "); 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; } }
Comments
Post a Comment