Reputation: 3805
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?
Upvotes: 2
Views: 28454
Reputation: 6167
I have created a java method which can create string from a CLOB object:
public String clobToString(Clob data) {
StringBuilder sb = new StringBuilder();
try {
Reader reader = data.getCharacterStream();
BufferedReader br = new BufferedReader(reader);
String line;
while(null != (line = br.readLine())) {
sb.append(line);
}
br.close();
} catch (SQLException e) {
// handle this exception
} catch (IOException e) {
// handle this exception
}
return sb.toString();
}
Upvotes: 0
Reputation: 118
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
Upvotes: 3
Reputation: 31
Converting String to CLOB
SOobject.setLongStringField( new SerialClob(entityString.toCharArray()));//Converting String to CLOB
Convert Clob to String
public String getLongStringField() {
Reader reader = null;
BufferedReader bufferedReader = null;
try {
reader = longStringField.getCharacterStream();
bufferedReader = new BufferedReader(reader);
return IOUtils.toString(bufferedReader);
} catch (Exception e) {
throw new RuntimeException("Error while reading String from CLOB", e);
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(bufferedReader);
}
}
Upvotes: 0
Reputation: 1
this my way (sorry my english)
res = ps.executeQuery();
try {
while (res.next()) {
System.out.println(res.getClob(1));//confirm data
sRet = res.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps.close();
}
Upvotes: 0
Reputation: 451
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.
Upvotes: 4