The approach for Oracle 9iR2 and 10g – based on the supplied UTL_HTTP package .
This article demonstrates a rather generic approach – which I think must be very close to using the UTL_DBWS package. We will take a more pragmatic, to the point line of action – that amounts to the same thing. Another resource that came in handy: Oracle Database 10g – XML & SQL: Design, Build & Manage XML Applications in Java,C, C++ & PL/SQL, Mark Scardina, Ben Chang and Jinyu Wang, Oracle Press – McGrawHill Osborne; 2004; ISBN 0-07-222952-7.

We know what the SOAP messages look like. Now we will create a PL/SQL block that recreates the SOAP message and then invokes the proper UTL_HTTP command:

declare
soap_request varchar2(30000);
soap_respond varchar2(30000);
http_req utl_http.req;
http_resp utl_http.resp;
resp XMLType;
i integer;

begin
soap_request:= '

us
uk

';
http_req:= utl_http.begin_request
( 'http://services.xmethods.net:80/soap'
, 'POST'
, 'HTTP/1.1'
);
utl_http.set_header(http_req, 'Content-Type', 'text/xml'); -- since we are dealing with plain text in XML documents
utl_http.set_header(http_req, 'Content-Length', length(soap_request));
utl_http.set_header(http_req, 'SOAPAction', ''); -- required to specify this is a SOAP communication
utl_http.write_text(http_req, soap_request);
http_resp:= utl_http.get_response(http_req);
utl_http.read_text(http_resp, soap_respond);
utl_http.end_response(http_resp);
resp:= XMLType.createXML(soap_respond);
resp:= resp.extract('/soap:Envelop/soap:Body/child::node()'
, 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"'
);
i:=0;
loop
dbms_output.put_line(substr(soap_respond,1+ i*255,250));
i:= i+1;
if i*250> length(soap_respond)
then
exit;
end if;
end loop;
end;
The output from runnning this piece of PL/SQL in SQL*Plus is this

0.5342
Turning this PL/SQL block into a proper PL/SQL Function that accepts the two countries as input-parameters is straightforward:

function get_conversion_rate
( p_country1 in varchar2 default 'us'
, p_country2 in varchar2 default 'us'
)
return varchar2
as
soap_request varchar2(30000);
soap_respond varchar2(30000);
http_req utl_http.req;
http_resp utl_http.resp;
resp XMLType;
begin
soap_request:= '

'||p_country1||'
'||p_country2||'

';
http_req:= utl_http.begin_request
( 'http://services.xmethods.net:80/soap'
, 'POST'
, 'HTTP/1.1'
);
utl_http.set_header(http_req, 'Content-Type', 'text/xml');
utl_http.set_header(http_req, 'Content-Length', length(soap_request));
utl_http.set_header(http_req, 'SOAPAction', '');
utl_http.write_text(http_req, soap_request);
http_resp:= utl_http.get_response(http_req);
utl_http.read_text(http_resp, soap_respond);
utl_http.end_response(http_resp);
- - Create an XMLType variable containing the Response XML
resp:= XMLType.createXML(soap_respond);
- - extract from the XMLType Resp the child-nodes of the element
resp:= resp.extract('/soap:Envelope/soap:Body/child::node()'
, 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"'
);
- - extract from the XMLType Resp the text() nodes from the n:getRateResponse/Result element
resp:= resp.extract('n:getRateResponse/Result/text()','xmlns:n="urn:xmethods-CurrencyExchange"');
return resp.getClobVal();
end;

We can create this function in any schema that has access to the sys.UTL_HTTP package. Calling this Function can be done from PL/SQL or even SQL:

select get_conversion_rate('uk','us')
from dual
/
GET_CONVERSION_RATE('UK','US')
---------------------------------
1.8713

Posted by: aykutomer | 06/03/2009

How to import an Oracle dump file

First of all place your .dmp file in your admin\XE\dpdump\ folder. If you are running your server locally then location is

C:\oraclexe\app\oracle\admin\XE\dpdump

This location may be different depending on where you installed oracle. I prefer to put my dump files in the dpdump folder so that I don’t have to specify a directory when doing the import.

Next you should open up your command line utility and type the following command

C:\>imp system/password file=c:\database.dmp full=yes

The data pump import utility will begin running and outputting it’s progress to the screen. It will generate your tables, triggers, binds, and fill the data in. Basically it will make an exact replication of the database it was exported from. After this is complete check your errors. If there were none, you’re good to go!

For more info about importing dump file

Read More…

/*
first create a PL/SQL object type and table type
*/
create or replace type SAMPLE_TYPE as object(
id number(34),
email varchar2(64),
info_value varchar2(255)
);
create or replace type SAMPLE_TABLE_TYPE as table of SAMPLE_TYPE;

//then in the java code
LogTrans app = new LogTrans();
int commit = 1;
Vector vector = new Vector();

Object[] attr = new Object[3];
attr[0] = (Object) new BigDecimal(8);
attr[1] = (Object) new String(“TEST@TEST.COM”);
attr[2] = (Object) new String(“TEST DATA”);

try
{
app.connect();

StructDescriptor structdesc = StructDescriptor.createDescriptor(“SAMPLE_TYPE”,app.con);
vector.add((Object)new STRUCT(structdesc, app.con, attr));

ArrayDescriptor arraydesc = ArrayDescriptor.createDescriptor(“SAMPLE_TABLE_TYPE”,app.con);

Object obj_array[] = vector.toArray();
ARRAY array = new ARRAY(arraydesc,app.con,obj_array);

CallableStatement cstm = app.con.prepareCall(“{ call PACKAGE.FUNCTION(?,?) }”);
((OracleCallableStatement)cstm).setARRAY(1, array);
cstm.setInt(2, 1);
cstm.execute();
System.out.println(“Please check database”);

}
catch(Exception e)
{
System.err.println(“dothis method exception: ” + e.getMessage());
}

Posted by: aykutomer | 28/02/2009

Passing Java Arrays to PL/SQL Procedure

in PL/SQL——
create or replace type rectype as object(col1 varchar2(10),col2 varchar2(10));
/
create or replace package ioStruct as
procedure testproc(iorec in out rectype,orec out rectype);
end ioStruct;
/

create or replace package body ioStruct as

procedure testproc(iorec in out rectype,orec out rectype) is
begin
orec := iorec;
iorec.col1 := orec.col2;
iorec.col2 := orec.col1;
end testproc;
end ioStruct;
/

and in java—-

{
// First declare the object arrays that will store the data.
Object [] p1obj = {“First”,”Second”};
Object [] p2obj = {};
// Now Declare a descriptor to associate the host object type with the
// record type in the database.
StructDescriptor desc1=StructDescriptor.createDescriptor(“RECTYPE”,conn);
// Now create the STRUCT objects to associate the host objects
// with the database records.
STRUCT p1struct = new STRUCT(desc1,conn,p1obj);
STRUCT p2struct;
// Declare the callable statement.
// This has to be of type OracleCallableStatement to use:
// setOracleObject(
// and
// registerOutParameter(position,type,oracletype)
OracleCallableStatement ocs =
(OracleCallableStatement)conn.prepareCall(“{call iostruct.testproc(?,?)}”);
// The first parameter is in out so we have to use setOracleObject to
// pass it to the statement.
ocs.setOracleObject(1,p1struct);
// The first parameter is in out so we have to Register the parameter as well.
// Note the reuse of the TYPE.
ocs.registerOutParameter(1,OracleTypes.STRUCT,”RECTYPE”);
// The second parameter is out so that has to be registered too.
// Note the re use of the TYPE.
ocs.registerOutParameter(2,OracleTypes.STRUCT,”RECTYPE”);
// Execute the procedure.
ocs.execute();
// Associate the returned arrays with the ARRAY objects.
p1struct = ocs.getSTRUCT(1);
p2struct = ocs.getSTRUCT(2);
// Get the data back into the data arrays
p1obj = p1struct.getAttributes();
p2obj = p2struct.getAttributes();
// Show the results:
System.out.println(“First Object is now “+p1obj[0]+” and “+p1obj[1]);
System.out.println(“Second Object is now “+p2obj[0]+” and “+p2obj[1]);
}

2009 yaz dönemi staj dönemi için  www.turkcellteknoloji.com.tr web sitesinden “İnsan Kaynakları/Açık Pozisyonlar/Turkcell Teknoloji 2009 Paf Takımı”  (http://www.turkcellteknoloji.com.tr/HumanResources/Paf2009.aspx) altından başvurular alınıyor.

Read More…

Posted by: aykutomer | 25/02/2009

How to return an array from a PL/SQL Stored function

Create a SQL VARRAY type in the database

SQL>CREATE OR REPLACE TYPE EMPARRAY is VARRAY(20) OF VARCHAR2(30)
SQL>/

Then create the following function that returns a VARRAY.

CREATE OR REPLACE FUNCTION getEmpArray RETURN EMPARRAY
AS
  l_data EmpArray := EmpArray();
  CURSOR c_emp IS SELECT ename FROM EMP;
  BEGIN
    FOR emp_rec IN c_emp LOOP
      l_data.extend;
      l_data(l_data.count) := emp_rec.ename;
    END LOOP;
    RETURN l_data;
  END;

Once the function is created in the database, it can be invoked from the java application and get the array data in the application. Below given is the code snippet  to execute the PL/SQL stored function from a Java Application.

public static void main( ) {
.........
.........
  OracleCallableStatement stmt =(OracleCallableStatement)conn.prepareCall
                ( "begin ? := getEMpArray; end;" );

    // The name we use below, EMPARRAY, has to match the name of the
    // type defined in the PL/SQL Stored Function
    stmt.registerOutParameter( 1, OracleTypes.ARRAY,"EMPARRAY" );
    stmt.executeUpdate();

    // Get the ARRAY object and print the meta data assosiated with it
    ARRAY simpleArray = stmt.getARRAY(1);

    System.out.println("Array is of type " +  simpleArray.getSQLTypeName());

    System.out.println("Array element is of type code "+simpleArray.getBaseType());

    System.out.println("Array is of length " + simpleArray.length());

    // Print the contents of the array
    String[] values = (String[])simpleArray.getArray();

    for( int i = 0; i < values.length; i++ )
      System.out.println( "row " + i + " = '" + values[i] +"'" );

...........
...........

Categories

Follow

Get every new post delivered to your Inbox.