Example usage for java.sql Clob getCharacterStream

List of usage examples for java.sql Clob getCharacterStream

Introduction

In this page you can find the example usage for java.sql Clob getCharacterStream.

Prototype

java.io.Reader getCharacterStream() throws SQLException;

Source Link

Document

Retrieves the CLOB value designated by this Clob object as a java.io.Reader object (or as a stream of characters).

Usage

From source file:nl.nn.adapterframework.util.JdbcUtil.java

public static Reader getClobReader(Clob clob) throws SQLException, JdbcException {
    return clob.getCharacterStream();
}

From source file:dk.netarkivet.harvester.datamodel.TemplateDBDAO.java

/**
 * Read an XML order file for the named order XML.
 *
 * @param orderXmlName The name of the order.xml document
 * @return The contents of this order.xml document
 *//*www.j  av a2 s  .  com*/
public synchronized HeritrixTemplate read(String orderXmlName) {
    ArgumentNotValid.checkNotNullOrEmpty(orderXmlName, "String orderXmlName");
    Connection c = HarvestDBConnection.get();
    PreparedStatement s = null;
    try {
        s = c.prepareStatement("SELECT orderxml FROM ordertemplates WHERE name = ?");
        s.setString(1, orderXmlName);
        ResultSet res = s.executeQuery();
        if (!res.next()) {
            throw new UnknownID("Can't find template " + orderXmlName);
        }
        Reader orderTemplateReader = null;
        if (DBSpecifics.getInstance().supportsClob()) {
            Clob clob = res.getClob(1);
            orderTemplateReader = clob.getCharacterStream();
        } else {
            orderTemplateReader = new StringReader(res.getString(1));
        }
        SAXReader reader = new SAXReader();
        // TODO Check what happens on non-ascii
        Document orderXMLdoc = reader.read(orderTemplateReader);
        return new HeritrixTemplate(orderXMLdoc);
    } catch (SQLException e) {
        final String message = "SQL error finding order.xml for " + orderXmlName + "\n"
                + ExceptionUtils.getSQLExceptionCause(e);
        log.warn(message, e);
        throw new IOFailure(message, e);
    } catch (DocumentException e) {
        final String message = "Error parsing order.xml string for " + orderXmlName;
        log.warn(message, e);
        throw new IOFailure(message, e);
    } finally {
        DBUtils.closeStatementIfOpen(s);
        HarvestDBConnection.release(c);
    }
}

From source file:com.squid.core.jdbc.formatter.DefaultJDBCDataFormatter.java

protected String read(Clob c) throws SQLException, IOException {
    StringBuffer sb = new StringBuffer((int) c.length());
    Reader r = c.getCharacterStream();
    char[] cbuf = new char[2048];
    int n = 0;/*from  w  w  w .j a  v a 2s  .  c o m*/
    while ((n = r.read(cbuf, 0, cbuf.length)) != -1) {
        if (n > 0) {
            sb.append(cbuf, 0, n);
        }
    }
    return sb.toString();
}

From source file:sk.uniza.fri.pds.spotreba.energie.service.SeHistoriaService.java

public List<String> createLastYearReport(ReportParams params) {
    try (Connection connection = OracleJDBCConnector.getConnection();) {
        CallableStatement stmnt = connection.prepareCall("select get_xml_odberatela(?) as xml from dual");
        stmnt.setInt(1, params.getIdOdberatela());
        ResultSet result = stmnt.executeQuery();

        List<String> output = new LinkedList<>();
        while (result.next()) {
            Clob clob = result.getClob("XML");
            Reader reader = clob.getCharacterStream();
            String o = IOUtils.toString(reader);
            output.add(o);/*from w w w .  j a  va 2  s. c  o  m*/
            File subor = params.getSubor();
            if (subor != null) {
                try (BufferedWriter w = new BufferedWriter(new FileWriter(subor))) {
                    IOUtils.write(o, w);
                }
            }
        }
        return output;
    } catch (SQLException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.unitime.commons.hibernate.blob.XmlClobType.java

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws SQLException {
    Clob clob = rs.getClob(names[0]);
    if (clob == null)
        return null;
    try {//  w  w  w .j  av a 2 s  . c o  m
        SAXReader reader = new SAXReader();
        Document document = reader.read(clob.getCharacterStream());
        return document;
    } catch (DocumentException e) {
        throw new HibernateException(e.getMessage(), e);
    }
}

From source file:com.serphacker.serposcope.db.base.ExportDB.java

protected String clobToStringEscaped(java.sql.Clob data) throws Exception {
    final StringBuilder builder = new StringBuilder();

    try (final Reader reader = data.getCharacterStream();
            final BufferedReader br = new BufferedReader(reader);) {
        int b;//from www  .  j ava  2 s .  c  om
        while (-1 != (b = br.read())) {
            sbEscape(builder, (char) b);
        }
    }
    return builder.toString();
}

From source file:org.executequery.gui.resultset.TableCellData.java

private String readClob() {

    if (StringUtils.isNotBlank(valueAsString) || value == null) {

        return valueAsString;
    }//w w w .java 2 s  .  c  om

    Clob clob = (Clob) value;

    Writer writer = new StringWriter();
    Reader reader;
    try {

        reader = clob.getCharacterStream();

    } catch (SQLException e) {

        if (Log.isDebugEnabled()) {

            Log.debug("Error reading CLOB data", e);
        }

        return e.getMessage();
    }

    char[] buffer = new char[DEFAULT_BUFFER_SIZE];

    try {

        while (true) {

            int amountRead;
            amountRead = reader.read(buffer);

            if (amountRead == -1) {

                break;
            }

            writer.write(buffer);
        }

        writer.flush();

    } catch (IOException e) {

        if (Log.isDebugEnabled()) {

            Log.debug("Error reading CLOB data", e);
        }

        return e.getMessage();
    }

    valueAsString = writer.toString();
    return valueAsString;
}

From source file:nl.b3p.catalog.arcgis.ArcSDE10JDBCHelper.java

@Override
public String getMetadata(ArcSDEJDBCDataset dataset) throws NamingException, SQLException, IOException {
    Connection c = getConnection();
    try {/*  ww  w .java  2  s.c o  m*/
        String sql = "select documentation from " + getTableName(TABLE_ITEMS) + " where objectid = ?";
        Clob xml = (Clob) new QueryRunner().query(c, sql, new ScalarHandler(), dataset.getObjectID());
        if (xml == null) {
            return DocumentHelper.EMPTY_METADATA;
        }
        return IOUtils.toString(xml.getCharacterStream());
    } finally {
        DbUtils.closeQuietly(c);
    }
}

From source file:com.opencsv.ResultSetHelperService.java

private String getColumnValue(ResultSet rs, int colType, int colIndex, boolean trim, String dateFormatString,
        String timestampFormatString) throws SQLException, IOException {

    String value = "";

    switch (colType) {
    case Types.BIT:
    case Types.JAVA_OBJECT:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getObject(colIndex), "");
        value = ObjectUtils.toString(rs.getObject(colIndex), "");
        break;//from   ww w  . j a  v a  2 s  .c  o  m
    case Types.BOOLEAN:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getBoolean(colIndex));
        value = ObjectUtils.toString(rs.getBoolean(colIndex));
        break;
    case Types.NCLOB: // todo : use rs.getNClob
    case Types.CLOB:
        Clob c = rs.getClob(colIndex);
        if (c != null) {
            StrBuilder sb = new StrBuilder();
            sb.readFrom(c.getCharacterStream());
            value = sb.toString();
        }
        break;
    case Types.BIGINT:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getLong(colIndex));
        value = ObjectUtils.toString(rs.getLong(colIndex));
        break;
    case Types.DECIMAL:
    case Types.REAL:
    case Types.NUMERIC:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getBigDecimal(colIndex), "");
        value = ObjectUtils.toString(rs.getBigDecimal(colIndex), "");
        break;
    case Types.DOUBLE:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getDouble(colIndex));
        value = ObjectUtils.toString(rs.getDouble(colIndex));
        break;
    case Types.FLOAT:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getFloat(colIndex));
        value = ObjectUtils.toString(rs.getFloat(colIndex));
        break;
    case Types.INTEGER:
    case Types.TINYINT:
    case Types.SMALLINT:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getInt(colIndex));
        value = ObjectUtils.toString(rs.getInt(colIndex));
        break;
    case Types.DATE:
        java.sql.Date date = rs.getDate(colIndex);
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(dateFormatString);
            value = df.format(date);
        }
        break;
    case Types.TIME:
        // Once Java 7 is the minimum supported version.
        //            value = Objects.toString(rs.getTime(colIndex), "");
        value = ObjectUtils.toString(rs.getTime(colIndex), "");
        break;
    case Types.TIMESTAMP:
        value = handleTimestamp(rs.getTimestamp(colIndex), timestampFormatString);
        break;
    case Types.NVARCHAR: // todo : use rs.getNString
    case Types.NCHAR: // todo : use rs.getNString
    case Types.LONGNVARCHAR: // todo : use rs.getNString
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
    case Types.CHAR:
        String columnValue = rs.getString(colIndex);
        if (trim && columnValue != null) {
            value = columnValue.trim();
        } else {
            value = columnValue;
        }
        break;
    default:
        value = "";
    }

    if (rs.wasNull() || value == null) {
        value = "";
    }

    return value;
}

From source file:org.springframework.jdbc.support.lob.DefaultLobHandler.java

@Override
public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException {
    logger.debug("Returning CLOB as character stream");
    if (this.wrapAsLob) {
        Clob clob = rs.getClob(columnIndex);
        return clob.getCharacterStream();
    } else {/*  w  w w  . j  av a  2s  . c o  m*/
        return rs.getCharacterStream(columnIndex);
    }
}