• About

On Technology

~ Software Architecture, Integration & Automation

On Technology

Tag Archives: jdbc

Call Oracle PL/SQL procedure with XMLType from Java using JDBC

11 Friday Mar 2011

Posted by Padmarag Lokhande in Database, Integration, Java, Oracle

≈ 2 Comments

Tags

jdbc, oracle, oracleaq, plsql, stored procedure, xmltype

Recently I needed to call a stored procedure which had Oracle’s XMLType as IN and OUT parameters.

The first thing to do is add xdb.jar and xmlparserv2.jar file to your application lib. These contain the required class files for Oracle XML API. the jars can be found under your installation of oracle client lib folders. Also don’t forget to add oracle jdbc driver files – ojdbc6 or ojdbc14.

The code I setup was this –

[sourcecode language=”java”]
XMLType reqInXml;
XMLType reqOutXML;
String atpInStr = "";
OpResponse output = null;

try {
reqInStr = jaxbMarshalRequestToString(input);
System.out.println("Input : " + reqInStr);
} catch (JAXBException ex) {
logger.log(Level.SEVERE, null, ex);
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
}

//input.getHeader().setNotes("THIS VALUE RETURNED BY FACADE : " + XXDS);
try {
Connection con = XXDS.getConnection();
//The IN parameter for stored proc is Oracle XDB XMLType
reqInXml = XMLType.createXML(con, reqInStr);
OracleCallableStatement stmt = (OracleCallableStatement) con.prepareCall("call DEMO_PROC.ProcessXMLRequest(?, ?, ?, ?)");
stmt.setObject(1, reqInXml);

//set out parameters
stmt.registerOutParameter (2, OracleTypes.OPAQUE,"SYS.XMLTYPE");
stmt.registerOutParameter(3, Types.INTEGER);
stmt.registerOutParameter(4, Types.VARCHAR);

stmt.executeQuery();
int resultCode = stmt.getInt(3);
String resultMsg = stmt.getString(4);
System.out.println("result code : " + resultCode);
System.out.println("result msg : " + resultMsg);
if (resultCode == 101 || resultCode == 100){
reqOutXML = XMLType.createXML(stmt.getOPAQUE(2));
System.out.println("Output from ERP :" + reqOutXML.getStringVal());
output = jaxbUnmarshalFromString(reqOutXML.getStringVal());
}
}
….
[/sourcecode]

This gave me “java.lang.ClassCastException: com.sun.gjc.spi.jdbc40.ConnectionHolder40 cannot
be cast to oracle.jdbc.OracleConnection” Exception. The connection was being returned from GlassFish JDBC ConnectionPool and was instance of OracleConnectionPooldataSource.
The FIX proved tricky, but in the end it was simple –
Do this –

[sourcecode language=”java”]
OracleConnection oraCon = con.unwrap(OracleConnection.class);
//The IN parameter for stored proc is Oracle XDB XMLType
atpInXml = XMLType.createXML(oraCon, atpInStr);
[/sourcecode]

Pass the cast instance of OracleConnection to the XMLType API.

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • Print (Opens in new window) Print

Like this:

Like Loading…

Subscribe

  • Entries (RSS)
  • Comments (RSS)

Archives

  • August 2026
  • March 2026
  • April 2024
  • April 2020
  • February 2019
  • April 2018
  • July 2015
  • July 2013
  • October 2012
  • June 2012
  • May 2012
  • September 2011
  • April 2011
  • March 2011
  • December 2010
  • August 2010

Categories

  • Camel
  • Database
  • Devops
    • Amazon AWS
    • Docker
    • Kubernetes
  • Integration
  • Java
  • JMS
  • MuleSoft
  • Oracle
  • Siebel
  • SOA
    • BPEL
    • REST
  • Uncategorized
  • Zapier

Meta

  • Log in

Powered by WordPress.com.

Loading Comments...
%d