I am trying to extract an XMLTYPE column from an Oracle table using JDBC and having some issues

跟風遠走 提交于 2021-02-10 05:53:20

问题


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!


回答1:


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/




回答2:


Have to use to_clob in the query select

to_clob(xmlelement ( "employee", 'Bob' ))  ) as SQLXMLCOL1 from dual 

then use the toString() in resultSet




回答3:


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.



来源:https://stackoverflow.com/questions/25515119/i-am-trying-to-extract-an-xmltype-column-from-an-oracle-table-using-jdbc-and-hav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!