Convert Clob to String
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
Comments
-
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 over 8 yearsIt will be a cast exception
-
PA001 over 8 yearsIf you cast it to oracle.sql.CLOB then yes, if you do String clobData = resultSet.getString("clob_field") you'll be fine.
-
Alireza Jamali over 4 yearsgod bless u my friend
-
Don Smith about 3 yearsThis worked for me:
if (obj!=null && obj instanceof oracle.sql.CLOB) {obj = rs.getString(i);}
. `