Convert Clob to String

19,722

Solution 1

Assuming you're using standard JDBC, once you have a ResultSet object you should be able to call ResultSet#getString("clob_field_name") to retrieve your CLOB data.

Solution 2

I know I'm late to this party!. Here is the one liner i used from hibernate library. If hibernate is already integrated to project then we can use annotations to convert clob to java String. In my case i had custom result transformer which read data from multiple tables after costly join. In the resultSetTransformer the below line does the job.

ClobType.INSTANCE.toString((Clob) tuple[2])
// org.hibernate.type.ClobType
Share:
19,722
Tony
Author by

Tony

Java ape.

Updated on June 14, 2022

Comments

  • Tony
    Tony almost 2 years

    How can I get String out of Clob. I did google it, but

    myClob.getSubString(0, (int) info.length()));
    

    is the only thing I get. Console says:

    java.sql.SQLException: Invalid argument(s) in call at oracle.sql.CLOB.getSubString(CLOB.java:278) at ru.tenet.es09.dao.CompanyDAOImpl.get(CompanyDAOImpl.java:72) at ru.tenet.es09.dao.CompanyDAOImpl.getList(CompanyDAOImpl.java:132) at ru.tenet.es09.dao.AddressDAOImpl.getList(AddressDAOImpl.java:59) at ru.tenet.es09.Test.main(Test.java:11)

    It points on getSubString() method. What is wrong?

  • Tony
    Tony over 8 years
    It will be a cast exception
  • PA001
    PA001 over 8 years
    If you cast it to oracle.sql.CLOB then yes, if you do String clobData = resultSet.getString("clob_field") you'll be fine.
  • Alireza Jamali
    Alireza Jamali over 4 years
    god bless u my friend
  • Don Smith
    Don Smith about 3 years
    This worked for me: if (obj!=null && obj instanceof oracle.sql.CLOB) {obj = rs.getString(i);}. `