Example usage for javax.sql.rowset.serial SerialClob SerialClob

List of usage examples for javax.sql.rowset.serial SerialClob SerialClob

Introduction

In this page you can find the example usage for javax.sql.rowset.serial SerialClob SerialClob.

Prototype

public SerialClob(Clob clob) throws SerialException, SQLException 

Source Link

Document

Constructs a SerialClob object that is a serialized version of the given Clob object.

Usage

From source file:com.adaptris.core.services.jdbc.types.ClobColumnTranslatorTest.java

@Test
public void testClobToString() throws Exception {
    Clob clob = new SerialClob("SomeData".toCharArray());
    JdbcResultRow row = new JdbcResultRow();
    row.setFieldValue("testField", clob, Types.CLOB);

    String translated = translator.translate(row, 0);

    assertEquals("SomeData", translated);
}

From source file:com.adaptris.core.services.jdbc.types.ClobColumnTranslatorTest.java

@Test
public void testClobToStringColumnName() throws Exception {
    Clob clob = new SerialClob("SomeData".toCharArray());
    JdbcResultRow row = new JdbcResultRow();
    row.setFieldValue("testField", clob, Types.CLOB);

    String translated = translator.translate(row, "testField");

    assertEquals("SomeData", translated);
}

From source file:lasige.steeldb.jdbc.BFTRowSet.java

/**
 * Sets the designated column in either the current row or the insert
 * row of this <code>CachedRowSetImpl</code> object with the given
 * <code>double</code> value.
 *
 * This method updates a column value in either the current row or
 * the insert row of this rowset, but it does not update the
 * database.  If the cursor is on a row in the rowset, the
 * method {@link #updateRow} must be called to update the database.
 * If the cursor is on the insert row, the method {@link #insertRow}
 * must be called, which will insert the new row into both this rowset
 * and the database. Both of these methods must be called before the
 * cursor moves to another row.//from  ww  w.ja va 2 s .  co m
 *
 * @param columnIndex the first column is <code>1</code>, the second
 *        is <code>2</code>, and so on; must be <code>1</code> or larger
 *        and equal to or less than the number of columns in this rowset
 * @param c the new column <code>Clob</code> value
 * @throws SQLException if (1) the given column index is out of bounds,
 *        (2) the cursor is not on one of this rowset's rows or its
 *        insert row, or (3) this rowset is
 *        <code>ResultSet.CONCUR_READ_ONLY</code>
 */
public void updateClob(int columnIndex, Clob c) throws SQLException {
    // sanity check.
    checkIndex(columnIndex);
    // make sure the cursor is on a valid row
    checkCursor();

    // SerialClob will help in getting the byte array and storing it.
    // We need to be checking DatabaseMetaData.locatorsUpdatorCopy()
    // or through RowSetMetaData.locatorsUpdatorCopy()

    if (dbmslocatorsUpdateCopy) {
        getCurrentRow().setColumnObject(columnIndex, new SerialClob(c));
    } else {
        throw new SQLException(resBundle.handleGetObject("cachedrowsetimpl.opnotsupp").toString());
    }
}

From source file:org.batoo.jpa.jdbc.AbstractColumn.java

/**
 * {@inheritDoc}//from  w ww  . j av  a  2  s. c om
 * 
 */
@Override
public Object convertValue(Connection connection, final Object value) {
    if (value == null) {
        return null;
    }

    if (this.temporalType != null) {
        switch (this.temporalType) {
        case DATE:
            if (value instanceof java.sql.Date) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Date(((Date) value).getTime());
            }

            return new java.sql.Date(((Calendar) value).getTimeInMillis());
        case TIME:
            if (value instanceof java.sql.Time) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Time(((Date) value).getTime());
            }

            return new java.sql.Time(((Calendar) value).getTimeInMillis());
        case TIMESTAMP:
            if (value instanceof java.sql.Timestamp) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Timestamp(((Date) value).getTime());
            }

            return new java.sql.Timestamp(((Calendar) value).getTimeInMillis());
        }
    }

    if (this.numberType != null) {
        return ReflectHelper.convertNumber((Number) value, this.numberType);
    }

    if (this.enumType != null) {
        final Enum<?> enumValue = (Enum<?>) value;
        if (this.enumType == EnumType.ORDINAL) {
            return enumValue.ordinal();
        }

        return enumValue.name();
    }

    if (this.lob) {
        try {
            if (this.javaType == String.class) {
                return new SerialClob(((String) value).toCharArray());
            } else if (this.javaType == char[].class) {
                return new SerialClob((char[]) value);
            } else if (this.javaType == byte[].class) {
                return new SerialBlob((byte[]) value);
            } else {
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                final ObjectOutputStream oos = new ObjectOutputStream(os);
                try {
                    oos.writeObject(value);
                } finally {
                    oos.close();
                }

                return new SerialBlob(os.toByteArray());
            }
        } catch (final Exception e) {
            throw new PersistenceException("Cannot set parameter", e);
        }
    } else {
        return value;
    }
}

From source file:org.batoo.jpa.jdbc.ValueConverter.java

/**
 * convert java objects to jdbc friendly
 * //w  w w .  j  ava2 s. c o  m
 * @param value
 *            jdbc raw value
 * @param javaType
 *            type of value
 * @param temporalType
 *            temporal type
 * @param enumType
 *            enum type
 * @param lob
 *            is Lob
 * @return jdbc friendly value
 * @since 2.0.1
 */
public static Object toJdbc(Object value, Class<?> javaType, TemporalType temporalType, EnumType enumType,
        boolean lob) {
    if (value == null) {
        return null;
    }

    if (temporalType != null) {
        switch (temporalType) {
        case DATE:
            if (value instanceof java.sql.Date) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Date(((Date) value).getTime());
            }

            return new java.sql.Date(((Calendar) value).getTimeInMillis());
        case TIME:
            if (value instanceof java.sql.Time) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Time(((Date) value).getTime());
            }

            return new java.sql.Time(((Calendar) value).getTimeInMillis());
        case TIMESTAMP:
            if (value instanceof java.sql.Timestamp) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Timestamp(((Date) value).getTime());
            }

            return new java.sql.Timestamp(((Calendar) value).getTimeInMillis());
        }
    }

    if (Number.class.isAssignableFrom(javaType)) {
        return ReflectHelper.convertNumber((Number) value, javaType);
    }

    if (enumType != null) {
        final Enum<?> enumValue = (Enum<?>) value;
        if (enumType == EnumType.ORDINAL) {
            return enumValue.ordinal();
        }

        return enumValue.name();
    }

    if (lob) {
        try {
            if (javaType == String.class) {
                return new SerialClob(((String) value).toCharArray());
            } else if (javaType == char[].class) {
                return new SerialClob((char[]) value);
            } else if (javaType == byte[].class) {
                return new SerialBlob((byte[]) value);
            } else {
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                final ObjectOutputStream oos = new ObjectOutputStream(os);
                try {
                    oos.writeObject(value);
                } finally {
                    oos.close();
                }

                return new SerialBlob(os.toByteArray());
            }
        } catch (final Exception e) {
            throw new PersistenceException("Cannot set parameter", e);
        }
    } else {
        return value;
    }

}

From source file:org.moqui.impl.entity.EntityJavaUtil.java

public static Object getResultSetValue(ResultSet rs, int index, FieldInfo fi, EntityFacade efi)
        throws EntityException {
    if (fi.typeValue == -1)
        throw new EntityException("No typeValue found for " + fi.entityName + "." + fi.name);

    Object value = null;//www. j a v  a 2s.  c o  m
    try {
        switch (fi.typeValue) {
        case 1:
            // getMetaData and the column type are somewhat slow (based on profiling), and String values are VERY
            //     common, so only do for text-very-long
            if (fi.isTextVeryLong) {
                ResultSetMetaData rsmd = rs.getMetaData();
                if (Types.CLOB == rsmd.getColumnType(index)) {
                    // if the String is empty, try to get a text input stream, this is required for some databases
                    // for larger fields, like CLOBs
                    Clob valueClob = rs.getClob(index);
                    Reader valueReader = null;
                    if (valueClob != null)
                        valueReader = valueClob.getCharacterStream();
                    if (valueReader != null) {
                        // read up to 4096 at a time
                        char[] inCharBuffer = new char[4096];
                        StringBuilder strBuf = new StringBuilder();
                        try {
                            int charsRead;
                            while ((charsRead = valueReader.read(inCharBuffer, 0, 4096)) > 0) {
                                strBuf.append(inCharBuffer, 0, charsRead);
                            }
                            valueReader.close();
                        } catch (IOException e) {
                            throw new EntityException("Error reading long character stream for field ["
                                    + fi.name + "] of entity [" + fi.entityName + "]", e);
                        }
                        value = strBuf.toString();
                    }
                } else {
                    value = rs.getString(index);
                }
            } else {
                value = rs.getString(index);
            }
            break;
        case 2:
            try {
                value = rs.getTimestamp(index, efi.getCalendarForTzLc());
            } catch (SQLException e) {
                if (logger.isTraceEnabled())
                    logger.trace(
                            "Ignoring SQLException for getTimestamp(), leaving null (found this in MySQL with a date/time value of [0000-00-00 00:00:00]): "
                                    + e.toString());
            }
            break;
        case 3:
            value = rs.getTime(index, efi.getCalendarForTzLc());
            break;
        case 4:
            value = rs.getDate(index, efi.getCalendarForTzLc());
            break;
        case 5:
            int intValue = rs.getInt(index);
            if (!rs.wasNull())
                value = intValue;
            break;
        case 6:
            long longValue = rs.getLong(index);
            if (!rs.wasNull())
                value = longValue;
            break;
        case 7:
            float floatValue = rs.getFloat(index);
            if (!rs.wasNull())
                value = floatValue;
            break;
        case 8:
            double doubleValue = rs.getDouble(index);
            if (!rs.wasNull())
                value = doubleValue;
            break;
        case 9:
            BigDecimal bigDecimalValue = rs.getBigDecimal(index);
            if (!rs.wasNull())
                value = bigDecimalValue != null ? bigDecimalValue.stripTrailingZeros() : null;
            break;
        case 10:
            boolean booleanValue = rs.getBoolean(index);
            if (!rs.wasNull())
                value = booleanValue;
            break;
        case 11:
            Object obj = null;
            byte[] originalBytes = rs.getBytes(index);
            InputStream binaryInput = null;
            if (originalBytes != null && originalBytes.length > 0) {
                binaryInput = new ByteArrayInputStream(originalBytes);
            }
            if (originalBytes != null && originalBytes.length <= 0) {
                logger.warn("Got byte array back empty for serialized Object with length ["
                        + originalBytes.length + "] for field [" + fi.name + "] (" + index + ")");
            }
            if (binaryInput != null) {
                ObjectInputStream inStream = null;
                try {
                    inStream = new ObjectInputStream(binaryInput);
                    obj = inStream.readObject();
                } catch (IOException ex) {
                    if (logger.isTraceEnabled())
                        logger.trace("Unable to read BLOB from input stream for field [" + fi.name + "] ("
                                + index + "): " + ex.toString());
                } catch (ClassNotFoundException ex) {
                    if (logger.isTraceEnabled())
                        logger.trace("Class not found: Unable to cast BLOB data to an Java object for field ["
                                + fi.name + "] (" + index
                                + "); most likely because it is a straight byte[], so just using the raw bytes: "
                                + ex.toString());
                } finally {
                    if (inStream != null) {
                        try {
                            inStream.close();
                        } catch (IOException e) {
                            throw new EntityException("Unable to close binary input stream for field ["
                                    + fi.name + "] (" + index + "): " + e.toString(), e);
                        }
                    }
                }
            }
            if (obj != null) {
                value = obj;
            } else {
                value = originalBytes;
            }
            break;
        case 12:
            SerialBlob sblob = null;
            try {
                // NOTE: changed to try getBytes first because Derby blows up on getBlob and on then calling getBytes for the same field, complains about getting value twice
                byte[] fieldBytes = rs.getBytes(index);
                if (!rs.wasNull())
                    sblob = new SerialBlob(fieldBytes);
                // fieldBytes = theBlob != null ? theBlob.getBytes(1, (int) theBlob.length()) : null
            } catch (SQLException e) {
                if (logger.isTraceEnabled())
                    logger.trace("Ignoring exception trying getBytes(), trying getBlob(): " + e.toString());
                Blob theBlob = rs.getBlob(index);
                if (!rs.wasNull())
                    sblob = new SerialBlob(theBlob);
            }
            value = sblob;
            break;
        case 13:
            value = new SerialClob(rs.getClob(index));
            break;
        case 14:
        case 15:
            value = rs.getObject(index);
            break;
        }
    } catch (SQLException sqle) {
        logger.error("SQL Exception while getting value for field: [" + fi.name + "] (" + index + ")", sqle);
        throw new EntityException(
                "SQL Exception while getting value for field: [" + fi.name + "] (" + index + ")", sqle);
    }

    return value;
}

From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java

@SuppressWarnings("unchecked")
public static <T> T extractData(final Object columnObject, final Class<T> resultingClass) throws SQLException {
    if (columnObject == null) {
        return (T) nullValueMap.get(resultingClass);
    }/*from  w w  w.  j  a  va  2s  .  c  om*/

    // try the simple case
    if (resultingClass.isAssignableFrom(columnObject.getClass())) {
        return resultingClass.cast(columnObject);
    }

    // see if we can do a simple numeric assignment
    if (columnObject instanceof Number) {
        return fromNumber(columnObject, resultingClass);
    }

    // see if we can convert from a string
    if (columnObject instanceof String) {
        return fromString(columnObject, resultingClass);
    }

    if (columnObject instanceof Boolean) {
        final Boolean b = (Boolean) columnObject;
        return fromString(b ? "1" : "0", resultingClass);
    }

    if (columnObject instanceof byte[]) {
        try {
            if (resultingClass.isAssignableFrom(Clob.class)) {
                return resultingClass.cast(
                        new SerialClob(IOUtils.toCharArray(new ByteArrayInputStream((byte[]) columnObject))));
            }
            if (resultingClass.isAssignableFrom(Blob.class)) {
                return resultingClass.cast(new SerialBlob((byte[]) columnObject));
            }
            if (resultingClass.isAssignableFrom(InputStream.class)) {
                return resultingClass.cast(new ByteArrayInputStream((byte[]) columnObject));
            }
            final String s = new String((byte[]) columnObject);
            return fromString(s, resultingClass);
        } catch (final IOException e) {
            throw new SQLException(e.getMessage(), e);
        }
    }

    if (columnObject instanceof Blob) {
        try {
            final Blob b = (Blob) columnObject;
            if (resultingClass.isAssignableFrom(byte[].class)) {
                return resultingClass.cast(IOUtils.toByteArray(b.getBinaryStream()));
            }
            if (resultingClass.isAssignableFrom(Clob.class)) {
                return resultingClass.cast(new SerialClob(IOUtils.toCharArray(b.getBinaryStream())));
            }
            if (resultingClass.isAssignableFrom(InputStream.class)) {
                return resultingClass.cast(b.getBinaryStream());
            }
            final String s = new String(IOUtils.toByteArray(((Blob) columnObject).getBinaryStream()));
            return fromString(s, resultingClass);
        } catch (final IOException e) {
            throw new SQLException(e.getMessage(), e);
        }
    }
    if (columnObject instanceof Clob) {
        try {
            final Clob c = (Clob) columnObject;
            if (resultingClass.isAssignableFrom(byte[].class)) {
                return resultingClass.cast(IOUtils.toByteArray(c.getAsciiStream()));
            }
            if (resultingClass.isAssignableFrom(Blob.class)) {
                return resultingClass.cast(new SerialBlob(IOUtils.toByteArray(c.getAsciiStream())));
            }
            if (resultingClass.isAssignableFrom(InputStream.class)) {
                return resultingClass.cast(c.getAsciiStream());
            }
            final String s = String.valueOf(IOUtils.toCharArray(c.getCharacterStream()));
            return fromString(s, resultingClass);
        } catch (final IOException e) {
            throw new SQLException(e.getMessage(), e);
        }
    }
    if (columnObject instanceof InputStream) {
        try {
            final InputStream is = (InputStream) columnObject;
            if (resultingClass.isAssignableFrom(Clob.class)) {
                return resultingClass.cast(new SerialClob(IOUtils.toCharArray(is)));
            }
            if (resultingClass.isAssignableFrom(Blob.class)) {
                return resultingClass.cast(new SerialBlob(IOUtils.toByteArray(is)));
            }
            if (resultingClass.isAssignableFrom(byte[].class)) {
                return resultingClass.cast(IOUtils.toByteArray(is));
            }
            return fromString(new String(IOUtils.toByteArray(is)), resultingClass);
        } catch (final IOException e) {
            throw new SQLException(e.getMessage(), e);
        }
    }
    throw new SQLException(String.format(" Can not cast %s (%s) to %s", columnObject.getClass(),
            columnObject.toString(), resultingClass));
}

From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java

private static <T> T fromNumber(final Object columnObject, final Class<T> resultingClass) throws SQLException {
    final Number n = Number.class.cast(columnObject);
    if (resultingClass == BigDecimal.class) {
        return resultingClass.cast(new BigDecimal(n.toString()));
    }//from w w  w . j  av  a2  s.  com
    if (resultingClass == BigInteger.class) {
        return resultingClass.cast(new BigInteger(n.toString()));
    }
    if (resultingClass == Byte.class) {
        return resultingClass.cast(new Byte(n.byteValue()));
    }
    if (resultingClass == Double.class) {
        return resultingClass.cast(new Double(n.doubleValue()));
    }
    if (resultingClass == Float.class) {
        return resultingClass.cast(new Float(n.floatValue()));
    }
    if (resultingClass == Integer.class) {
        return resultingClass.cast(new Integer(n.intValue()));
    }
    if (resultingClass == Long.class) {
        return resultingClass.cast(new Long(n.longValue()));
    }
    if (resultingClass == Short.class) {
        return resultingClass.cast(new Short(n.shortValue()));
    }
    if (resultingClass == String.class) {
        return resultingClass.cast(n.toString());
    }
    if (resultingClass == Boolean.class) {
        if (n.byteValue() == 0) {
            return resultingClass.cast(Boolean.FALSE);
        }
        if (n.byteValue() == 1) {
            return resultingClass.cast(Boolean.TRUE);
        }
    }
    if (resultingClass == byte[].class) {
        return resultingClass.cast(n.toString().getBytes());
    }
    if (resultingClass == Blob.class) {
        return resultingClass.cast(new SerialBlob(n.toString().getBytes()));
    }
    if (resultingClass == Clob.class) {
        return resultingClass.cast(new SerialClob(n.toString().toCharArray()));
    }
    return null;
}

From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java

private static <T> T fromString(final Object columnObject, final Class<T> resultingClass) throws SQLException {
    final String val = String.class.cast(columnObject);
    // to numeric casts
    try {//from   www  .j a v  a 2  s.c om
        if (resultingClass == BigDecimal.class) {
            return resultingClass.cast(new BigDecimal(val));
        }
        if (resultingClass == BigInteger.class) {
            return resultingClass.cast(new BigInteger(val));
        }
        if (resultingClass == Byte.class) {
            return resultingClass.cast(new Byte(val));
        }
        if (resultingClass == Double.class) {
            return resultingClass.cast(new Double(val));
        }
        if (resultingClass == Float.class) {
            return resultingClass.cast(new Float(val));
        }
        if (resultingClass == Integer.class) {
            return resultingClass.cast(new Integer(val));
        }
        if (resultingClass == Long.class) {
            return resultingClass.cast(new Long(val));
        }
        if (resultingClass == Short.class) {
            return resultingClass.cast(new Short(val));
        }
    } catch (final NumberFormatException e) {
        return null;
    }

    if (resultingClass == Boolean.class) {
        if ("0".equals(val)) {
            return resultingClass.cast(Boolean.FALSE);
        }
        if ("1".equals(val)) {
            return resultingClass.cast(Boolean.TRUE);
        }
    }
    if (resultingClass == byte[].class) {
        return resultingClass.cast(val.getBytes());
    }
    if (resultingClass == Blob.class) {
        return resultingClass.cast(new SerialBlob(val.getBytes()));
    }
    if (resultingClass == Clob.class) {
        return resultingClass.cast(new SerialClob(val.toCharArray()));
    }
    return null;
}