This section describes how to retrieve CLOB values with the ResultSet.getString() method.
The simplest way to retrieve the character string value from a CLOB column is to use the getString() method on the ResultSet object.
Here is short example program:
/**
* OracleClobGetString.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.sql.*;
public class OracleClobGetString {
public static void main(String [] args) {
Connection con = null;
try {
oracle.jdbc.pool.OracleDataSource ds
= new oracle.jdbc.pool.OracleDataSource();
ds.setDriverType("thin");
ds.setServerName("localhost");
ds.setPortNumber(1521);
ds.setDatabaseName("XE");
ds.setUser("Herong");
ds.setPassword("TopSecret");
con = ds.getConnection();
// Retrieving CLOB value with getString()
Statement sta = con.createStatement();
ResultSet res = sta.executeQuery("SELECT * FROM Article");
int i = 0;
while (res.next() && i<3) {
i++;
System.out.println("Record ID: "+res.getInt("ID"));
System.out.println(" Subject = "+res.getString("Subject"));
String body = res.getString("Body");
if (body.length() > 100) body = body.substring(0,100);
System.out.println(" Body = "+body+"...");
}
res.close();
sta.close();
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
}
The output of the program confirms that CLOB values can be retrieved with getString() method on the ResultSet object:
C:\>java -cp .;\local\lib\ojdbc14.jar OracleClobGetString
Record ID: 1
Subject = Test on INSERT statement
Body = A BLOB (Binary Large OBject) is a large chunk of data
which is stored in a database....
Record ID: 2
Subject = Test of the setString() method
Body = He is wonderful and strange and who knows how old he is,
he thought. Never have I had such a strong...
Record ID: 3
Subject = Test of setCharacterStream() methods
Body = /**
* OracleClobSetCharacterStream.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserv...