在 Java 中从 Oracle 中提取 XMLType 值时出现异常

Exception while extracting XMLType value from Oracle in Java

我正在尝试从 Oracle table 中提取 XML 数据类型并遇到此异常:

java.lang.NullPointerException: null
at oracle.jdbc.driver.NamedTypeAccessor.getOracleObject(NamedTypeAccessor.java:302) ~[ojdbc6-11.2.0.3.0.jar:11.2.0.3.0]
at oracle.jdbc.driver.NamedTypeAccessor.getSQLXML(NamedTypeAccessor.java:413) ~[ojdbc6-11.2.0.3.0.jar:11.2.0.3.0]
at oracle.jdbc.driver.OracleResultSetImpl.getSQLXML(OracleResultSetImpl.java:1251) ~[ojdbc6-11.2.0.3.0.jar:11.2.0.3.0]
at oracle.jdbc.driver.OracleResultSet.getSQLXML(OracleResultSet.java:488) ~[ojdbc6-11.2.0.3.0.jar:11.2.0.3.0]

代码片段:

SQLXML sqlxml = resultSet.getSQLXML(1);

我仔细检查了正在获取结果集的查询,它按预期工作。此外,我仅针对 XMLType 列面临此问题。

  public static void main(String[] args) throws SQLException, Exception {
        Connection con = ConnectionDefinition.getOracleConnection(); //my oracle connection
        PreparedStatement ps = con.prepareStatement("SELECT a.b FROM xml_test a");       
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
           if( rs.getObject(1) != null ){
                 SQLXML xml = rs.getSQLXML(1);
                 System.err.println(xml.getString());
           } else {
                 System.err.println("xmltype is null");
           }                     
        }
        rs.close();
        con.close();        
    }

和 pom 依赖。推荐使用11.2.0.4版本的oracle驱动

<dependencies>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>xdb</artifactId>
        <version>11.2.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>xmlparserv2</artifactId>
        <version>11.1.2</version>
    </dependency>
</dependencies>