2

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
  • 3,605
  • 14
  • 52
  • 84
  • Possible duplicate of [Most efficient solution for reading CLOB to String, and String to CLOB in Java?](http://stackoverflow.com/questions/2169732/most-efficient-solution-for-reading-clob-to-string-and-string-to-clob-in-java) – CubeJockey Dec 03 '15 at 14:22

5 Answers5

4

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.

PA001
  • 451
  • 3
  • 12
3

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
Raghu Kumar
  • 118
  • 5
0

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();
        }
0

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);
            }
    }
Shine
  • 31
  • 1
0

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();

}

Mohit Singh
  • 5,977
  • 2
  • 24
  • 25