read Clob - Java java.sql

Java examples for java.sql:Clob

Description

read Clob

Demo Code


//package com.java2s;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

public class Main {
    /**/* w  w w  . jav a  2 s  .com*/
     * by Lawrence Wong
     * 
     * @param clob
     * @return
     * @throws SQLException
     * @throws java.io.IOException
     */
    public static String readClob(Clob clob) throws SQLException,
            java.io.IOException {
        if (clob == null)
            return null;
        // Open a stream to read Clob data
        Reader clobStream = clob.getCharacterStream();
        // Holds the Clob data when the Clob stream is being read
        // Read from the Clob stream and write to the stringbuffer
        int nchars = 0;

        // Number of characters read
        char[] buffer = new char[1024];
        StringBuffer result = new StringBuffer();

        // Buffer holding characters being transferred
        while ((nchars = clobStream.read(buffer)) != -1) {
            // Read from Clob
            result.append(buffer, 0, nchars);
            // Write to StringBuffer
        }
        clobStream.close();
        // Close the Clob input stream
        return result.toString();
    }
}

Related Tutorials