Kbase P156490: How to read a clob field using JDBC
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  25/11/2009 |
|
Status: Unverified
GOAL:
How to read a CLOB field using JDBC
GOAL:
How to set a character set to read a CLOB field.
FACT(s) (Environment):
OpenEdge 10.x
All Supported Operating Systems
FIX:
JDBC provides a method in the resultset object that loads the CLOB into the client JVM. After doing this, the client application can get the data from it using either a character stream or a Ascii stream. However, the Ascii stream provides more control to the client, once it allows to define what character will be used. This value has to match the one specified in the field definition ( that probably is UTF-8, that can accommodate a wider range of characters).
The example below, show how to read the field F1 from the customer table.import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.sql.*;
public class ReadDatabase {
public static void main(String[] s) throws SQLException, IOException,
ClassNotFoundException {
String url;
Class.forName("com.ddtek.jdbc.openedge.OpenEdgeDriver");
url = new String(
"jdbc:datadirect:openedge://<server>:<port>;databaseName=<database>;user=<user>;password = <password>");
Connection con = DriverManager.getConnection(url);
ResultSet rs = con.createStatement().executeQuery(
"select * from pub.customer");
while (rs.next()) {
InputStreamReader st = new InputStreamReader(rs.getClob("F1").getAsciiStream(),Charset.forName("UTF-8"));
while (true) {
int i = (int) st.read();
if (i != -1) {
System.out.print((char)i);
} else
break;
}
}
}
}