Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Returning XMLType Data from StoredProcedure, Usage of ojdbc6.jar xdb6.jar xmlparserv2.jar for Java to PLSQL interaction

I am following the below tutorial to retrieve XMLType data from a stored procedure

http://docs.oracle.com/cd/B19306_01/appdev.102/b14259/xdb11jav.htm#g1039140 Example 13-4 XMLType Java: Returning XMLType Data

When I run the code, I get this error,

Exception in thread "main" java.lang.NoClassDefFoundError: oracle/xml/parser/v2/XMLParseException
    at oracle.xdb.XMLTypeFactory.create(XMLTypeFactory.java:67)
    at oracle.sql.OPAQUE.toClass(OPAQUE.java:328)
    at oracle.sql.OPAQUE.toJdbc(OPAQUE.java:278)
    at oracle.sql.OPAQUE.toJdbc(OPAQUE.java:259)
    at oracle.jdbc.driver.NamedTypeAccessor.getObject(NamedTypeAccessor.java:190)
    at oracle.jdbc.driver.NamedTypeAccessor.getObject(NamedTypeAccessor.java:117)
    at oracle.jdbc.driver.OracleCallableStatement.getObject(OracleCallableStatement.java:1579)
    at oracle.jdbc.driver.OracleCallableStatementWrapper.getObject(OracleCallableStatementWrapper.java:815)
    at com.example.TestXMLDAO.main(TestXMLDAO.java:48)
Caused by: java.lang.ClassNotFoundException: oracle.xml.parser.v2.XMLParseException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 9 more

Java code snippet:

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import oracle.jdbc.OracleTypes;
import oracle.xdb.XMLType;

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

            // Prepare to call the stored procedure get_group_details.

            String sql = "{call mypackage.get_records(?,?,?,?,?,?,?,?)}";

            callableStatement = conn.prepareCall(sql);

            callableStatement.setString(1, "");
            callableStatement.setString(2, "");
            callableStatement.setString(3, "");
            callableStatement.setString(4, "");
            callableStatement.setString(5, "abb%");
            callableStatement.setString(6, "F");
            callableStatement.setString(7, "F");
            callableStatement.registerOutParameter(8, OracleTypes.OPAQUE,
                    "SYS.XMLTYPE");

            // execute get_group_details
            // callableStatement.executeQuery();

            callableStatement.executeQuery();

            callableStatement.getObject(8);

            // get cursor and cast it to ResultSet
            xml = (XMLType) callableStatement.getObject(8);

            System.out.println(xml.getStringVal());

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // Close the statement
            callableStatement.close();
            // Close the connection
            conn.close();
        }

PLSQL Procedure snippet

PROCEDURE get_records
   (
      grp1     IN a.name%TYPE DEFAULT NULL
     ,grp2 IN a.name%TYPE DEFAULT NULL
     ,grp3        IN a.name%TYPE DEFAULT NULL
     ,grp4          IN a.name%TYPE DEFAULT NULL
     ,grp5               IN a.name%TYPE DEFAULT NULL
     ,flag1     IN a.flag%TYPE DEFAULT 'F'
     ,flag2       IN a.flag%TYPE DEFAULT 'F'
     ,xml_type_out        OUT xmltype
   );

Jars included: ojdbc6.jar, xdb.jar

Any idea what I am missing?

This issue is resolved. But when deployed in JBoss server, another issue - please refer here if you have any ideas - Xmlparserv2 error while application deployed in jboss, Installing Oracle ojdbc module in JBoss for Java web application

like image 828
spiderman Avatar asked Nov 29 '22 00:11

spiderman


1 Answers

Issue fixed by adding the required jars

Needed the following three jars in the classpath, [I had these jars still, I was getting error due to wrong versions of the jar]

ojdbc6.jar, xdb6.jar, xmlparserv2-11.1.1.jar

Even if you have these jars, you might get error like I was getting when I was including a wrong version of xmlparserv2.jar.

So Important thing to take care is the version and compatibility of these jars.

The ones that worked for me [My Oracle DB version : 11.2.0.4.0]

ojdbc6.jar; xdb6.jar:

http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

xmlparserv2-11.1.1.jar available here: ( SQL Developer ) (in sqldeveloper\modules\oracle.xdk_12.1.2)

[removed third party reference link as advised by @Lukas Eder, Thanks for that]

like image 159
spiderman Avatar answered Dec 04 '22 13:12

spiderman