Skip to Main Content

Database Software

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem inserting clob value into xmltype column

757177Mar 2 2010 — edited Mar 2 2010
Hi all,

I have created a table in XML DB using as:
CREATE TABLE TransDetailstblCLOB ( id number, data_xml XMLType) XmlType data_xml STORE AS CLOB;

I am trying to insert large xml data into the data_xml column which is of type XMLTYPE.

I followed this link (http://www.oracle.com/technology/sample_code/tech/java/codesnippet/xmldb/HowToLoadLargeXML.html) to create a clob object and insert into xml.

I am getting the following error:

ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00229: input source is empty
Error at line 0
ORA-06512: at "SYS.XMLTYPE", line 254
ORA-06512: at line 1


My code snippet:

private static CLOB getCLOB(String xmlData, Connection conn) throws SQLException{
CLOB tempClob = null;


if(conn==null){
log.debug("Connection object is null");
throw new SQLException("Connection object is null");
}
try{
// If the temporary CLOB has not yet been created, create new
tempClob = CLOB.createTemporary(conn, true, CLOB.MODE_READWRITE);

// Open the temporary CLOB in readwrite mode to enable writing
tempClob.open(CLOB.MODE_READWRITE);

// Get the output stream to write
writer = tempClob.getCharacterOutputStream();
writer.write(xmlData);




} catch(SQLException sqlexp){
tempClob.freeTemporary();
sqlexp.printStackTrace();
} catch(Exception exp){
tempClob.freeTemporary();
exp.printStackTrace();
}
return tempClob;
}

public static void insertXML(String xmlData, Connection conn){
CLOB clob = null;
String query;
log.debug("Inside insertXML" +xmlData);
try{
query = "INSERT INTO TransDetailstbl1(data) VALUES (XMLType(?)) ";// Changed prev TransDetailstbl

// Get the statement Object
pstmt = conn.prepareStatement(query);

// xmlData is the string that contains the XML Data.
// Get the CLOB object using the getCLOB method.
clob = getCLOB(xmlData, conn);

// Bind this CLOB with the prepared Statement
pstmt.setObject(1, clob);

int i =pstmt.executeUpdate();
log.debug("pstmt.executeUpdate () status ::: "+i);

// Execute the Prepared Statement
if (i == 1) {
log.debug("Record Successfully inserted!");
}

} catch(SQLException sqlexp){
sqlexp.printStackTrace();
} catch(Exception exp){
exp.printStackTrace();
}

finally{
try{
pstmt.close();
// Flush and close the stream
writer.flush();
writer.close();

// Close the temporary CLOB
tempClob.close();
}
catch(Exception e)
{
log.debug("Cant close prepared statement.");
e.printStackTrace();
}
}
}

Can anyone help me out?

Please let me know if any other info is required.

Regards,
Robina
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 30 2010
Added on Mar 2 2010
2 comments
2,567 views