1
votes

I have a String of more than 5000 characters, which I want to save as XMLTYPE with MyBatis. For this I do the following:

convert the String to CLOB and the CLOB use it to save it in database with the constructor: XMLTYPE.CREATEXML.

I have a problem when converting the String to CLOB.

If I do it in the first way, the CLOB object that is created is a SerialClob has 3 fields: BUF, LENG and CLOB; but of these three fields only two are filled: BUF (buffer) and LENG but CLOB is null.

But if I do it in the second way, I create the complete CLOB object, with all the String, and I do not have problems saving it in the database. But the first way I create a SerialClob, which does not allow it to be saved in an XMLTYPE.

I can not use the XMLTYPE constructor with VARCHARS because my String has more than 4000 characters.

Is there an alternative without having to open a connection?

 Clob myClobFile;
    String str = "<myString example></end of example>";

First way:

try
{
   myClobFile=  new javax.sql.rowset.serial.SerialClob (str.toCharArray ());
}

Second way:

try
{
    myClobFile= sqlSession.getConnection ().createClob ();
    myClobFile.setString (1, str);
 }
1
Please indicate the database which you are working on. Not all databases have XMLTYPE data type.Ian Lim
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0KikeSP

1 Answers

1
votes

I fund a solution:

    public static Clob stringToClob (SqlSession sqlSession, String str) throws SQLException 
    {
            Clob myRet = null;

            try
            {
                myRet = sqlSession.getConnection ().createClob ();
                myRet.setString (1, str);
            }
            catch (SQLException e)
            {
                ...trows
            }
            return myRet;
    }

I used that solution because when debugging the code, in the section that the array should be (in the clob), it was an empty array. There must be a problem when trying to format an array of bits to clob, and then pass this object to mybatis.