Reputation: 21
I am trying to extract an XMLTYPE column from an Oracle table using JDBC.
I have the query:
select "XML_FILE" FROM "TABLE_NAME"
and when I run the query in the Oracle SQL Developer, it returns back all the XMLTYPE rows completely fine.
But, when I run the following Java code, and run the same query, I always get "null" returned for every column. I'm not sure what could be going wrong and I've tried doing many different things, but nothing has been working. Important note - the XMLTYPE fields each have an XML file with at most 60,000 lines of XML.
The Java code I'm using:
String query = "select \"XML_FILE\" FROM \"TABLE_NAME\"";
//creating PreparedStatement object to execute query
PreparedStatement preStatement = null;
try {
System.out.println("QUERY: "+query);
preStatement = con.prepareStatement(query); //con is the Connection obj
} catch (SQLException e) {
e.printStackTrace();
}
ResultSet result = null;
try {
result = preStatement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("RESULT SET: "+result.toString());
String dataResult = "";
while(result.next()){
for(int i=1; i<=numCols; i++){
dataResult += result.getObject(i)+"\t";
//dataResult += result.getString(i)+"\t";
}
dataResult+="\n";
System.out.println("RESULT: \'"+dataResult+"\'");
}
Any help would be greatly appreciated. Thanks!
Upvotes: 2
Views: 8231
Reputation: 11
Have to use to_clob
in the query
select
to_clob(xmlelement ( "employee", 'Bob' )) ) as SQLXMLCOL1 from dual
then use the toString()
in resultSet
Upvotes: 1
Reputation: 126
Do you mean your call to result.toString() is returning null? If so, that's not how you get the data from the result set anyway. I tried a couple of different test, using the DataDirect Oracle JDBC driver and only got null from result.toString(). If I call rs.getString() for the XMLType column, I would get the XML data I inserted; Calling getObject() returned to LOB locator (I think, didn't double check) that references the data. Printing that did not give me the data, just the ID for the locator.
Upvotes: 0
Reputation: 2597
You can't retrieve this as string (varchar), because of limitations of varchar lenght (4000bytes). Select it as CLOB:
String query = "SELECT TBL.XMLTYPECOLUMN.GETCLOBVAL() FROM TABLE TBL";
rs = stmt.executeQuery(query);
xmlClob = (Clob) rs.getClob(1);
http://kodehelp.com/how-to-read-xmltype-column-from-database-using-jdbc/
Upvotes: 1