I have a plSql that returns a type of data.
How can I handle these from java, I mean, I make the call to the PL from java, but I do not know how to pick up that type of response to use their fields.
I have a plSql that returns a type of data.
How can I handle these from java, I mean, I make the call to the PL from java, but I do not know how to pick up that type of response to use their fields.
You do not give much information, but if what you are referring to is PLSQL types like STRUCT or Oracle ARRAY, what you need is to use OracleCallableStatement
that provides you with methods to register output parameters of those types.
Example:
Transaction tran = null;
OracleCallableStatement ocs = null;
try {
tran = ...obtener transaccion...
tran.setSql( "CALL PROPIETARIO.PAQUETE.PROCEDIMIENTO(?,?)" );
ocs = (OracleCallableStatement) tran.prepareCall();
ocs.setString(1, "UN_PARAMETRO_DE_ENTRADA");
ocs.registerOutParameter(2, java.sql.Types.STRUCT, "NOMBRE_DEL_TIPO_PLSQL");
ocs.execute();
//la clase padre es oracle.sql.Datum y de ella heredan STRUCT y ARRAY.
oracle.sql.STRUCT struct = ocs.getSTRUCT(2);
Object[] atributos = (Object[])struct.getAttributes();
//El array de atributos esta ordenado tal cual se declaran las propiedades en el plsql, así que si por ejemplo el primer campo es un nombre.
String nombre = (String)atributos[0];
...etc...
}catch( Excetion ex){
//manejar excepciones;
}
The Datum
can also be sent as input parameters, not only output, even both at the same time.
Currently with JPA, we do not usually use this type of mappings much, but in the past, we defined a Bean with the same fields as STRUCT oracle and implemented an interface similar to this:
import java.sql.Connection;
import java.sql.SQLException;
import oracle.sql.Datum;
/**
* Interface que define los métodos necesarios para transformar un objeto
* entre el tipo Java y el Oracle.
* @author Victor Gimeno
* @version 1.0
*/
public interface OracleBdParser {
/**
* Método que transforma el objeto a un tipo Oracle de BBDD.
* @param con Objeto con la conexion a la bbdd para hacer un lookup del objeto.
* @return Supertipo de los objetos Oracle en Java
* @throws SQLException excepcion de base de datos
*/
Datum toSqlOracleType( Connection con ) throws SQLException;
/**
* Método que transforma el objeto a un tipo Oracle de BBDD.
* @param oracleObj Objeto de tipo Oracle que debe transformarse
* @throws SQLException excepcion de base de datos
*/
void fromSqlOracleType( Datum oracleObj ) throws SQLException;
/**
* Método que devuelve el Nombre del tipo de Objeto Oracle declarado en BBDD.
* @return Nombre del Objeto declaro en BBDD.
*/
String getSqlOracleTypeName();
}
So at the time of retrieving it from database we simply invoked:
mibean.fromSqlOracleType(ocs.getSTRUCT(2));
I hope it serves you, Greetings.