Example usage for java.sql ResultSet getClob

List of usage examples for java.sql ResultSet getClob

Introduction

In this page you can find the example usage for java.sql ResultSet getClob.

Prototype

Clob getClob(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a Clob object in the Java programming language.

Usage

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:nz.co.gregs.dbvolution.datatypes.DBJavaObject.java

@SuppressWarnings("unchecked")
private O getFromCLOB(ResultSet resultSet, String fullColumnName) throws SQLException {
    O returnValue = null;//from w ww. j av  a  2 s.  co m
    Clob clob = resultSet.getClob(fullColumnName);
    if (resultSet.wasNull() || clob == null) {
        this.setToNull();
    } else {
        try {
            BufferedReader input = new BufferedReader(clob.getCharacterStream());
            try {
                List<byte[]> byteArrays = new ArrayList<byte[]>();

                int totalBytesRead = 0;
                try {
                    char[] resultSetBytes;
                    resultSetBytes = new char[100000];
                    int bytesRead = input.read(resultSetBytes);
                    while (bytesRead > 0) {
                        totalBytesRead += bytesRead;
                        byteArrays.add(String.valueOf(resultSetBytes).getBytes());
                        resultSetBytes = new char[100000];
                        bytesRead = input.read(resultSetBytes);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(DBByteArray.class.getName()).log(Level.SEVERE, null, ex);
                }
                byte[] bytes = new byte[totalBytesRead];
                int bytesAdded = 0;
                for (byte[] someBytes : byteArrays) {
                    System.arraycopy(someBytes, 0, bytes, bytesAdded,
                            Math.min(someBytes.length, bytes.length - bytesAdded));
                    bytesAdded += someBytes.length;
                }
                ObjectInputStream objectInput = new ObjectInputStream(new ByteArrayInputStream(bytes));
                //            this.setValue(objectInput.readObject());
                returnValue = (O) objectInput.readObject();
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(DBJavaObject.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBJavaObject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return returnValue;
}

From source file:org.apache.tika.parser.jdbc.JDBCTableReader.java

protected void handleClob(String tableName, String columnName, int rowNum, ResultSet resultSet, int columnIndex,
        ContentHandler handler, ParseContext context) throws SQLException, IOException, SAXException {
    Clob clob = resultSet.getClob(columnIndex);
    if (resultSet.wasNull()) {
        return;//from w w w . j a v a 2s  .  c om
    }
    boolean truncated = clob.length() > Integer.MAX_VALUE || clob.length() > maxClobLength;

    int readSize = (clob.length() < maxClobLength ? (int) clob.length() : maxClobLength);
    Metadata m = new Metadata();
    m.set(Database.TABLE_NAME, tableName);
    m.set(Database.COLUMN_NAME, columnName);
    m.set(Database.PREFIX + "ROW_NUM", Integer.toString(rowNum));
    m.set(Database.PREFIX + "IS_CLOB", "true");
    m.set(Database.PREFIX + "CLOB_LENGTH", Long.toString(clob.length()));
    m.set(Database.PREFIX + "IS_CLOB_TRUNCATED", Boolean.toString(truncated));
    m.set(Metadata.CONTENT_TYPE, "text/plain; charset=UTF-8");
    m.set(Metadata.CONTENT_LENGTH, Integer.toString(readSize));
    m.set(TikaMetadataKeys.RESOURCE_NAME_KEY,
            //just in case something screwy is going on with the column name
            FilenameUtils.normalize(FilenameUtils.getName(columnName + "_" + rowNum + ".txt")));

    //is there a more efficient way to go from a Reader to an InputStream?
    String s = clob.getSubString(0, readSize);
    if (embeddedDocumentUtil.shouldParseEmbedded(m)) {
        embeddedDocumentUtil.parseEmbedded(new ByteArrayInputStream(s.getBytes(UTF_8)), handler, m, true);
    }
}

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

@Override
@Nullable//from   w  w  w  .j  av a2s  . c  o m
public String getClobAsString(ResultSet rs, int columnIndex) throws SQLException {
    logger.debug("Returning CLOB as string");
    if (this.wrapAsLob) {
        Clob clob = rs.getClob(columnIndex);
        return clob.getSubString(1, (int) clob.length());
    } else {
        return rs.getString(columnIndex);
    }
}

From source file:nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport.java

public Object getClobUpdateHandle(ResultSet rs, int column) throws SQLException, JdbcException {
    Clob clob = rs.getClob(column);
    if (clob == null) {
        throw new JdbcException("no clob found in column [" + column + "]");
    }/* w  ww .  j a  v a 2  s  .  co  m*/
    return clob;
}

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

@Override
public InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException {
    logger.debug("Returning CLOB as ASCII stream");
    if (this.wrapAsLob) {
        Clob clob = rs.getClob(columnIndex);
        return clob.getAsciiStream();
    } else {/*w  w  w  .j  a v  a 2s  .  c  om*/
        return rs.getAsciiStream(columnIndex);
    }
}

From source file:nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport.java

public Object getClobUpdateHandle(ResultSet rs, String column) throws SQLException, JdbcException {
    Clob clob = rs.getClob(column);
    if (clob == null) {
        throw new JdbcException("no clob found in column [" + column + "]");
    }/* w  w w  .  j  a  v  a 2s  .  c  o  m*/
    return clob;
}

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  .ja v  a  2s  . c o m*/
        return rs.getCharacterStream(columnIndex);
    }
}

From source file:org.apache.sqoop.lib.LargeObjectLoader.java

/**
 * Actually read a ClobRef instance from the ResultSet and materialize
 * the data either inline or to a file./*from w  w  w  . j  a va 2s .  c  om*/
 *
 * @param colNum the column of the ResultSet's current row to read.
 * @param r the ResultSet to read from.
 * @return a ClobRef encapsulating the data in this field.
 * @throws IOException if an error occurs writing to the FileSystem.
 * @throws SQLException if an error occurs reading from the database.
 */
public com.cloudera.sqoop.lib.ClobRef readClobRef(int colNum, ResultSet r)
        throws IOException, InterruptedException, SQLException {

    long maxInlineLobLen = conf.getLong(MAX_INLINE_LOB_LEN_KEY, DEFAULT_MAX_LOB_LENGTH);

    Clob c = r.getClob(colNum);
    if (null == c) {
        return null;
    } else if (c.length() > maxInlineLobLen) {
        // Deserialize large CLOB into separate file.
        long len = c.length();
        LobFile.Writer lobWriter = getClobWriter();

        long recordOffset = lobWriter.tell();
        Reader reader = null;
        Writer w = lobWriter.writeClobRecord(len);
        try {
            reader = c.getCharacterStream();
            copyAll(reader, w);
        } finally {
            if (null != w) {
                w.close();
            }

            if (null != reader) {
                reader.close();
            }

            // Mark the record as finished.
            lobWriter.finishRecord();
        }

        return new com.cloudera.sqoop.lib.ClobRef(getRelativePath(lobWriter), recordOffset, len);
    } else {
        // This is a 1-based array.
        return new com.cloudera.sqoop.lib.ClobRef(c.getSubString(1, (int) c.length()));
    }
}

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
 *///  w  w  w . j a  v a  2  s .c om
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);
    }
}