Arrays de entrada en un Procedimiento?
Estamos desarrollando una aplicacion JAVA .
El problema consiste en que debemos proporcionar un ARRAY de ids de usuario a un procedimiento almacenado.
Este procedimiento debe eliminar esos usuarios de la BB.DD y devolver un array a JAVA con los usuarios que se han eliminado correctamente.
Podemos devolver el array sin problemas, pero es posible suministrar un array de entrada a un Procedimiento PL/SQL de Oracle?
Un saludo y gracias de antemano...
El problema consiste en que debemos proporcionar un ARRAY de ids de usuario a un procedimiento almacenado.
Este procedimiento debe eliminar esos usuarios de la BB.DD y devolver un array a JAVA con los usuarios que se han eliminado correctamente.
Podemos devolver el array sin problemas, pero es posible suministrar un array de entrada a un Procedimiento PL/SQL de Oracle?
Un saludo y gracias de antemano...
En principio no se puede, pero lo puedes simular con parametros de tipo tabla.
Te mando un ejemplo:
import java.sql.*;
import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import oracle.sql.*;
import oracle.jdbc.driver.*;
public class callInOutArray extends Object {
/*
This program demonstrates how to emulate calling
a stored procedure
with PL/SQL table
parameters via JDBC. You cannot call a PL/SQL table
parameter directly,
but you can use an Object of Type Table.
The Script used
to create the procedure used in this example is as
follows:
create or replace
type tabtype as table of varchar2(30);
/
create or replace
package ioArray as
procedure testproc(iotab
in out tabtype,otab out tabtype);
end ioArray;
/
create or replace
package body ioArray as
procedure testproc(iotab
in out tabtype,otab out tabtype) is
begin
otab := iotab;
if iotab.count = 0 then
iotab.extend;
iotab(1) :=
'Record 1';
else
iotab.extend;
iotab(iotab.count)
:= 'Second'||iotab(1);
end if;
end testproc;
end ioArray;
/
*/
public static void main(String[] args) throws
SQLException {
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());
// Connect to the database
Connection conn =
DriverManager.getConnection
("jdbc:oracle:oci8:@S692815.WORLD",
"scott", "tiger");
//
First, declare the data arrays that will store the data.
String [] p1obj
= {"First"};
String [] p2obj = {};
//
Now, declare a descriptor to associate the host array type with the
//
array type in the database.
ArrayDescriptor
desc1=ArrayDescriptor.createDescriptor("TABTYPE",conn);
//
Create the ARRAY objects to associate the host array
//
with the database array.
ARRAY p1arr =
new ARRAY(desc1,conn,p1obj);
ARRAY p2arr =
new ARRAY(desc1,conn,p2obj);
//
Declare the callable statement.
//
This must be of type OracleCallableStatement to use:
//
setArray(ARRAY)
//
and
//
registerOutParameter(position,type,oracletype)
OracleCallableStatement
ocs =
(OracleCallableStatement)conn.prepareCall("{call
ioarray.testproc(?,?)}");
//
The first parameter is in out, so you must use setArray to
//
pass it to the statement.
ocs.setArray(1,p1arr);
//
The first parameter is in out, so you must Register the parameter as
//
well.
//
Note the reuse of the TYPE.
ocs.registerOutParameter(1,OracleTypes.ARRAY,"TABTYPE");
//
The second parameter is out, so that must be registered too.
//
Note the reuse of the TYPE.
ocs.registerOutParameter(2,OracleTypes.ARRAY,"TABTYPE");
//
Execute the procedure
ocs.execute();
//
Associate the returned arrays with the ARRAY objects.
p1arr = (ARRAY)ocs.getArray(1);
p2arr = (ARRAY)ocs.getArray(2);
//
Get the data back into the data arrays.
p1obj = (String[])p1arr.getArray();
p2obj = (String[])p2arr.getArray();
//
Show the results:
System.out.println("p1obj[p1obj.length-1]
is " + p1obj[p1obj.length-1]);
System.out.println("p2obj[0]
is " + p2obj[0]);
}
}
Te mando un ejemplo:
import java.sql.*;
import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import oracle.sql.*;
import oracle.jdbc.driver.*;
public class callInOutArray extends Object {
/*
This program demonstrates how to emulate calling
a stored procedure
with PL/SQL table
parameters via JDBC. You cannot call a PL/SQL table
parameter directly,
but you can use an Object of Type Table.
The Script used
to create the procedure used in this example is as
follows:
create or replace
type tabtype as table of varchar2(30);
/
create or replace
package ioArray as
procedure testproc(iotab
in out tabtype,otab out tabtype);
end ioArray;
/
create or replace
package body ioArray as
procedure testproc(iotab
in out tabtype,otab out tabtype) is
begin
otab := iotab;
if iotab.count = 0 then
iotab.extend;
iotab(1) :=
'Record 1';
else
iotab.extend;
iotab(iotab.count)
:= 'Second'||iotab(1);
end if;
end testproc;
end ioArray;
/
*/
public static void main(String[] args) throws
SQLException {
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());
// Connect to the database
Connection conn =
DriverManager.getConnection
("jdbc:oracle:oci8:@S692815.WORLD",
"scott", "tiger");
//
First, declare the data arrays that will store the data.
String [] p1obj
= {"First"};
String [] p2obj = {};
//
Now, declare a descriptor to associate the host array type with the
//
array type in the database.
ArrayDescriptor
desc1=ArrayDescriptor.createDescriptor("TABTYPE",conn);
//
Create the ARRAY objects to associate the host array
//
with the database array.
ARRAY p1arr =
new ARRAY(desc1,conn,p1obj);
ARRAY p2arr =
new ARRAY(desc1,conn,p2obj);
//
Declare the callable statement.
//
This must be of type OracleCallableStatement to use:
//
setArray(ARRAY)
//
and
//
registerOutParameter(position,type,oracletype)
OracleCallableStatement
ocs =
(OracleCallableStatement)conn.prepareCall("{call
ioarray.testproc(?,?)}");
//
The first parameter is in out, so you must use setArray to
//
pass it to the statement.
ocs.setArray(1,p1arr);
//
The first parameter is in out, so you must Register the parameter as
//
well.
//
Note the reuse of the TYPE.
ocs.registerOutParameter(1,OracleTypes.ARRAY,"TABTYPE");
//
The second parameter is out, so that must be registered too.
//
Note the reuse of the TYPE.
ocs.registerOutParameter(2,OracleTypes.ARRAY,"TABTYPE");
//
Execute the procedure
ocs.execute();
//
Associate the returned arrays with the ARRAY objects.
p1arr = (ARRAY)ocs.getArray(1);
p2arr = (ARRAY)ocs.getArray(2);
//
Get the data back into the data arrays.
p1obj = (String[])p1arr.getArray();
p2obj = (String[])p2arr.getArray();
//
Show the results:
System.out.println("p1obj[p1obj.length-1]
is " + p1obj[p1obj.length-1]);
System.out.println("p2obj[0]
is " + p2obj[0]);
}
}
yo tengo un problema mas fácil, tengo que leer el conjunto idc (contestaciones) que ha habido a una encuesta, para ello utilizo estas líneas de código:
rs = s.executeQuery("SELECT idc FROM respuesta WHERE ide = \' "+ encuesta +"\' ");
if (rs.next())
Array aidc= rs.getArray("idc");
Si utilizo el getInt solo obtengo el primero que encuentra... pero yo lo quiero todos, por eso he decidido utilizar la función getArray(),
El problema que tengo es que sala una excepción "com.mysql.jdbc.NotImplemented: Feature not implemented", Ósea que no esta implementado esta función.
Me he bajado los últimos driver para conectarse a mysql : mysql-connector-java-3.0.15 , pero nada, sigue diciéndome que no esta implementado.
¿sabes como puedo leer todos los idc?
Gracias
rs = s.executeQuery("SELECT idc FROM respuesta WHERE ide = \' "+ encuesta +"\' ");
if (rs.next())
Array aidc= rs.getArray("idc");
Si utilizo el getInt solo obtengo el primero que encuentra... pero yo lo quiero todos, por eso he decidido utilizar la función getArray(),
El problema que tengo es que sala una excepción "com.mysql.jdbc.NotImplemented: Feature not implemented", Ósea que no esta implementado esta función.
Me he bajado los últimos driver para conectarse a mysql : mysql-connector-java-3.0.15 , pero nada, sigue diciéndome que no esta implementado.
¿sabes como puedo leer todos los idc?
Gracias
Es exactamente lo que buscaba, muchisimas gracias por la ayuda compañero ;)
