Insert XML with more than 4000 characters into a Oracle XMLTYPE column

前端 未结 3 1072
借酒劲吻你
借酒劲吻你 2021-01-07 06:01

I have an oracle table with a column from type \"SYS.XMLTYPE\" and a storage procudure which is doing the insert:

(Short version):

PROCE         


        
相关标签:
3条回答
  • 2021-01-07 06:09

    Check the Oracle docs about XMLType

    Also, I believe the datatype should be a CLOB (Character Large Object).

    0 讨论(0)
  • 2021-01-07 06:19

    You need to convert xml string for more than 4000 charcaters into SQLXML type first.

    Environment: jpa 2.1.0, eclipselink 2.5.2, oracle db 11gr2

    SQL:

    CREATE TABLE "XMLTEST"
    ( "ID" NUMBER(10,0) NOT NULL ENABLE, 
      "DESCRIPTION" VARCHAR2(50 CHAR) NOT NULL ENABLE, 
      "XML_TXT" "XMLTYPE" NOT NULL ENABLE
    );
    
    INSERT INTO XMLTEST (ID, DESCRIPTION, XML_TXT) VALUES (101, 'XML DATA', '<data>TEST</data>');
    COMMIT;
    
    DROP TABLE "XMLTEST";
    

    Java Code

    String sql = "INSERT INTO XMLTEST (ID, DESCRIPTION, XML_TXT) VALUES (?, ?, ?)";
    String xmlDataStr = "<data>test...</data>"; // a long xml string with length > 4000 characters
    Connection con = getEntityManager().unwrap(Connection.class);
    SQLXML sqlXml = con.createSQLXML();
    sqlXml.setString(xmlDataStr);
    

    Java code - use PreparedStatement

    PreparedStatement pstmt = con.prepareStatement(sql);
    pstmt.setLong(1, 201);
    pstmt.setLong(2, "Long XML Data");
    pstmt.setSQLXML(3, sqlXml);
    pstmt.execute();
    

    Java code - use native query instead of PreparedStatement

    Query query = getEntityManager().createNativeQuery(sql);
    query.setParameter(1, 301);
    query.setParameter(2, "Long XML Data");
    query.setParameter(3, sqlXml);
    query.executeUpdate();
    
    0 讨论(0)
  • 2021-01-07 06:23

    According to Stuart Campbell

    • I inserted a XMLTYPE into a CLOB
    • but the statement itself had to be wrapped in a transaction.

    Without transaction, it did not work....

    0 讨论(0)
提交回复
热议问题