Reputation: 313
I am conncted with oracle database. I need to get infromation from tables to my service class and then send it to controller. One of variable is CLOB data type. How to get it and parse into List of String? Here is my pseudo-code:
public class ModelClass {
private List<String> comments;
public ModelClass(List<String> comments) {
this.comments = comments;
}
public List<String> getComments() {
return comments;
}
}
Then I am trying to execute query into ResultSet and put it into List of ModelClass ResultSet rS = dataSource.getConnection().createStatement().executeQuery(query.toString());
while (rS.next()) {
result.add(new ModelClass((List<String>) rS.getObject(13)));
Obviously it doesn't work. I do not know how to get that Clob. When I am trying to take result I have following error:"oracle.sql.CLOB cannot be cast to java.util.List",
when I change rS.Object(13) into rS.getClob(13) it doesn't work as well. Could you please tell me how to get that Clob object? Actually is a JSON with 4 fields.
Upvotes: 1
Views: 11670
Reputation: 49
Clob clob = rs.getClob(13);
String json = clob.getSubString(1, (int) clob.length());
Now you can convert it into object using library as GSON
Upvotes: 0
Reputation: 4065
The CLOB object you are getting as result of rS.getObject(13)
is a string-like object. If you want to treat it like a list of 4 fields you should convert it to a JSON object using any library as GSON and then asking the JSON object for it's fields.
while (rS.next()) {
CLOB clobOjb = (CLOB) rS.getObject(13);
// convert clobObj (or it's string conversion) to JSON
// extract the fields from JSON object and store them in a list
result.add(new ModelClass( /* the list */ ));
Upvotes: 0