Example usage for java.sql Clob length

List of usage examples for java.sql Clob length

Introduction

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

Prototype

long length() throws SQLException;

Source Link

Document

Retrieves the number of characters in the CLOB value designated by this Clob object.

Usage

From source file:org.sakaiproject.content.impl.serialize.impl.conversion.Type1BlobResourcesConversionHandler.java

public Object getValidateSource(String id, ResultSet rs) throws SQLException {
    ResultSetMetaData metadata = rs.getMetaData();
    byte[] rv = null;
    switch (metadata.getColumnType(1)) {
    case Types.BLOB:
        Blob blob = rs.getBlob(1);
        if (blob != null) {
            //System.out.println("getValidateSource(" + id + ") blob == " + blob + " blob.length == " + blob.length());
            rv = blob.getBytes(1L, (int) blob.length());
        } else {//from  w  w  w  . j av  a2 s .co m
            System.out.println("getValidateSource(" + id + ") blob is null");
        }
        break;
    case Types.CLOB:
        Clob clob = rs.getClob(1);
        if (clob != null) {
            rv = clob.getSubString(1L, (int) clob.length()).getBytes();
        }
        break;
    case Types.CHAR:
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
        rv = rs.getString(1).getBytes();
        break;
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        rv = rs.getBytes(1);
        break;
    }
    // System.out.println("getValidateSource(" + id + ") \n" + rv + "\n");
    return rv;

    //return rs.getBytes(1);
}

From source file:org.sakaiproject.mailarchive.impl.conversion.ExtractXMLToColumns.java

public Object getSource(String id, ResultSet rs) throws SQLException {
    ResultSetMetaData metadata = rs.getMetaData();
    String rv = null;//  www.  jav a 2  s  . co m
    switch (metadata.getColumnType(1)) {
    case Types.BLOB:
        Blob blob = rs.getBlob(1);
        if (blob != null) {
            rv = new String(blob.getBytes(1L, (int) blob.length()));
        }
        break;
    case Types.CLOB:
        Clob clob = rs.getClob(1);
        if (clob != null) {
            rv = clob.getSubString(1L, (int) clob.length());
        }
        break;
    case Types.CHAR:
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        byte[] bytes = rs.getBytes(1);
        if (bytes != null) {
            rv = new String(bytes);
        }
        break;
    }
    // System.out.println("getSource(" + id + ") \n" + rv + "\n");
    return rv;
}

From source file:org.sakaiproject.mailarchive.impl.conversion.ExtractXMLToColumns.java

public Object getValidateSource(String id, ResultSet rs) throws SQLException {
    ResultSetMetaData metadata = rs.getMetaData();
    byte[] rv = null;
    switch (metadata.getColumnType(1)) {
    case Types.BLOB:
        Blob blob = rs.getBlob(1);
        if (blob != null) {
            rv = blob.getBytes(1L, (int) blob.length());
        } else {//from  ww  w.  ja v a 2 s. c o m
            System.out.println("getValidateSource(" + id + ") blob ==  null");
        }
        break;
    case Types.CLOB:
        Clob clob = rs.getClob(1);
        if (clob != null) {
            rv = clob.getSubString(1L, (int) clob.length()).getBytes();
        }
        break;
    case Types.CHAR:
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
        rv = rs.getString(1).getBytes();
        break;
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        rv = rs.getBytes(1);
        break;
    }
    // System.out.println("getValidateSource(" + id + ") \n" + rv + "\n");
    return rv;
}

From source file:org.springframework.jdbc.support.JdbcUtils.java

/**
 * Retrieve a JDBC column value from a ResultSet, using the most appropriate
 * value type. The returned value should be a detached value object, not having
 * any ties to the active ResultSet: in particular, it should not be a Blob or
 * Clob object but rather a byte array or String representation, respectively.
 * <p>Uses the {@code getObject(index)} method, but includes additional "hacks"
 * to get around Oracle 10g returning a non-standard object for its TIMESTAMP
 * datatype and a {@code java.sql.Date} for DATE columns leaving out the
 * time portion: These columns will explicitly be extracted as standard
 * {@code java.sql.Timestamp} object.//from   w w w .ja v a2 s.  c o  m
 * @param rs is the ResultSet holding the data
 * @param index is the column index
 * @return the value object
 * @throws SQLException if thrown by the JDBC API
 * @see java.sql.Blob
 * @see java.sql.Clob
 * @see java.sql.Timestamp
 */
@Nullable
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
    Object obj = rs.getObject(index);
    String className = null;
    if (obj != null) {
        className = obj.getClass().getName();
    }
    if (obj instanceof Blob) {
        Blob blob = (Blob) obj;
        obj = blob.getBytes(1, (int) blob.length());
    } else if (obj instanceof Clob) {
        Clob clob = (Clob) obj;
        obj = clob.getSubString(1, (int) clob.length());
    } else if ("oracle.sql.TIMESTAMP".equals(className) || "oracle.sql.TIMESTAMPTZ".equals(className)) {
        obj = rs.getTimestamp(index);
    } else if (className != null && className.startsWith("oracle.sql.DATE")) {
        String metaDataClassName = rs.getMetaData().getColumnClassName(index);
        if ("java.sql.Timestamp".equals(metaDataClassName)
                || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) {
            obj = rs.getTimestamp(index);
        } else {
            obj = rs.getDate(index);
        }
    } else if (obj instanceof java.sql.Date) {
        if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) {
            obj = rs.getTimestamp(index);
        }
    }
    return obj;
}

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

@Override
@Nullable// w  ww . j a va 2s. c om
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:org.springframework.jdbc.support.lob.OracleLobHandler.java

@Override
public String getClobAsString(ResultSet rs, int columnIndex) throws SQLException {
    logger.debug("Returning Oracle CLOB as string");
    Clob clob = rs.getClob(columnIndex);
    initializeResourcesBeforeRead(rs.getStatement().getConnection(), clob);
    String retVal = (clob != null ? clob.getSubString(1, (int) clob.length()) : null);
    releaseResourcesAfterRead(rs.getStatement().getConnection(), clob);
    return retVal;
}

From source file:org.zaproxy.zap.extension.sse.db.TableEventStream.java

/**
 * @param rs/*from   w w w.ja v  a2  s .  com*/
 * @param interpretLiteralBytes
 * @param previewLength
 * @return
 * @throws DatabaseException
 */
private List<ServerSentEvent> buildEvents(ResultSet rs, boolean interpretLiteralBytes, int previewLength)
        throws SQLException {
    List<ServerSentEvent> events = new ArrayList<>();
    try {
        while (rs.next()) {
            ServerSentEvent event;

            int streamId = rs.getInt("stream_id");
            //            ServerSentEventStream stream = getStream(streamId);
            event = new ServerSentEvent();
            // TODO should I set stream?
            event.setId(rs.getInt("event_id"));
            event.setEventType(rs.getString("event_type"));
            event.setLastEventId(rs.getString("last_event_id"));
            event.setStreamId(streamId);
            event.setTime(rs.getTimestamp("timestamp"));

            if (previewLength == -1) {
                // load all characters
                event.setData(rs.getString("data"));
            } else {
                Clob clob = rs.getClob("data");
                int length = Math.min(previewLength, (int) clob.length());
                event.setData(clob.getSubString(1, length));
                clob.free();
            }

            Clob clob = rs.getClob("raw_event");
            event.setRawEventLength(clob.length());
            if (previewLength == -1) {
                // load all characters
                event.setRawEvent(rs.getString("raw_event"));
            } else {
                int length = Math.min(previewLength, (int) clob.length());
                event.setRawEvent(clob.getSubString(1, length));
            }
            clob.free();

            events.add(event);
        }
    } finally {
        rs.close();
    }

    return events;
}

From source file:org.zaproxy.zap.extension.websocket.db.TableWebSocket.java

/**
 * Filter out and count messages according to payloadFilter
 *
 * @param criteria/*from  ww w  . j  av  a 2 s . c  o m*/
 * @param opcodes Null when all opcodes should be retrieved.
 * @param inScopeChannelIds
 * @param payloadFilter Null when all payloads should be retrieved.
 * @param payloadLength
 * @return number of message that fulfill given template
 * @throws DatabaseException
 */
private int countMessageWithPayloadFilter(WebSocketMessageDTO criteria, List<Integer> opcodes,
        List<Integer> inScopeChannelIds, WebSocketMessagesPayloadFilter payloadFilter, int payloadLength)
        throws DatabaseException {
    String query = "SELECT m.opcode, m.payload_utf8 FROM websocket_message AS m "
            + "LEFT OUTER JOIN websocket_message_fuzz f "
            + "ON m.message_id = f.message_id AND m.channel_id = f.channel_id " + "<where> ";
    int count = 0;
    try {
        PreparedStatement stmt = buildMessageCriteriaStatement(query, criteria, opcodes, inScopeChannelIds);
        stmt.execute();
        ResultSet resultSet = stmt.getResultSet();
        try {
            while (resultSet.next()) {
                String payload;
                // read payload
                if (resultSet.getInt("opcode") != WebSocketMessage.OPCODE_BINARY) {

                    if (payloadLength == -1) {
                        // load all characters
                        payload = resultSet.getString("payload_utf8");
                    } else {
                        Clob clob = resultSet.getClob("payload_utf8");
                        int length = Math.min(payloadLength, (int) clob.length());
                        payload = clob.getSubString(1, length);
                        clob.free();
                    }
                    if (payloadFilter.isStringValidWithPattern(payload)) {
                        count++;
                    }
                }
            }
        } finally {
            resultSet.close();
            stmt.close();
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }

    return count;
}

From source file:org.zaproxy.zap.extension.websocket.db.TableWebSocket.java

/**
 * @param rs//from www .  ja v a 2s . com
 * @param interpretLiteralBytes
 * @param payloadLength
 * @return
 * @throws HttpMalformedHeaderException
 * @throws SQLException
 * @throws DatabaseException
 */
private List<WebSocketMessageDTO> buildMessageDTOs(ResultSet rs, boolean interpretLiteralBytes,
        int payloadLength) throws SQLException, DatabaseException {
    ArrayList<WebSocketMessageDTO> messages = new ArrayList<>();
    try {
        while (rs.next()) {
            WebSocketMessageDTO message;

            int channelId = rs.getInt("channel_id");
            WebSocketChannelDTO channel = getChannel(channelId);

            if (rs.getInt("fuzz_id") != 0) {
                WebSocketFuzzMessageDTO fuzzMessage = new WebSocketFuzzMessageDTO(channel);
                fuzzMessage.fuzzId = rs.getInt("fuzz_id");
                fuzzMessage.state = WebSocketFuzzMessageDTO.State.valueOf(rs.getString("state"));
                fuzzMessage.fuzz = rs.getString("fuzz");

                message = fuzzMessage;
            } else {
                message = new WebSocketMessageDTO(channel);
            }

            message.id = rs.getInt("message_id");
            message.setTime(rs.getTimestamp("timestamp"));
            message.opcode = rs.getInt("opcode");
            message.readableOpcode = WebSocketMessage.opcode2string(message.opcode);

            // read payload
            if (message.opcode == WebSocketMessage.OPCODE_BINARY) {
                if (payloadLength == -1) {
                    // load all bytes
                    message.payload = rs.getBytes("payload_bytes");
                } else {
                    Blob blob = rs.getBlob("payload_bytes");
                    int length = Math.min(payloadLength, (int) blob.length());
                    message.payload = blob.getBytes(1, length);
                    blob.free();
                }

                if (message.payload == null) {
                    message.payload = new byte[0];
                }
            } else {
                if (payloadLength == -1) {
                    // load all characters
                    message.payload = rs.getString("payload_utf8");
                } else {
                    Clob clob = rs.getClob("payload_utf8");
                    int length = Math.min(payloadLength, (int) clob.length());
                    message.payload = clob.getSubString(1, length);
                    clob.free();
                }

                if (message.payload == null) {
                    message.payload = "";
                }
            }

            message.isOutgoing = rs.getBoolean("is_outgoing");
            message.payloadLength = rs.getInt("payload_length");

            messages.add(message);
        }
    } finally {
        rs.close();
    }

    messages.trimToSize();

    return messages;
}

From source file:uk.ac.ebi.orchem.search.SimilaritySearch.java

/**
 * Java interface to PL/SQL for similarity searching
 * @param userQuery  query structure in some chemical format
 * @param queryType  MOL or SMILES//  w w w  .  j  a v a  2s .  c o m
 * @param cutOff     break out when similarity goes under this cut off
 * @param topN       only find first top N results
 * @param debugYN    debug info back to user Y/N
 * @param idsOnlyYN  only return IDs Y/N
 * @param extraWhereClause an option to add an extra where clause refering to your base compound table
 * @return           array of compound data
 * @throws Exception
 */
public static oracle.sql.ARRAY search(Clob userQuery, String queryType, Float cutOff, Integer topN,
        String debugYN, String idsOnlyYN, String extraWhereClause) throws Exception {
    int clobLen = new Long(userQuery.length()).intValue();
    String query = (userQuery.getSubString(1, clobLen));
    if (queryType.equals(Utils.QUERY_TYPE_MOL))
        return molSearch(query, cutOff, topN, debugYN, idsOnlyYN, extraWhereClause);
    else if (queryType.equals(Utils.QUERY_TYPE_SMILES))
        return smilesSearch(query, cutOff, topN, debugYN, idsOnlyYN, extraWhereClause);
    else
        throw new RuntimeException("Query type not recognized");
}