Tuesday, July 5, 2011

JDBC: retrieve values from BLOB or CLOB

There are four pairs of methods in the java.sql.ResultSet class that deal with BLOBs/CLOBs:
  1. Reader getCharacterStream(int columnIndex)   /
    Reader getCharacterStream(String columnName)
  2. InputStream getBinaryStream(int columnIndex)  /
    InputStream getBinaryStream(String columnName)
  3. Blob getBlob(int i)/Blob getBlob(String colName)
  4. Clob getClob(int i)/Clob getClob(String colName)


Example code:
PreparedStatement pstmt = conn.prepareStatement("SELECT image FROM photos where id = ?");
pstmt.setInt(100);
ResultSet rs= pstmt.executeQuery();
while(rs.next()) {
    InputStream in = rs.getBinaryStream(1);
    ...
} 
rs.close();

No comments:

Post a Comment

Chitika