Example usage for java.sql Types CLOB

List of usage examples for java.sql Types CLOB

Introduction

In this page you can find the example usage for java.sql Types CLOB.

Prototype

int CLOB

To view the source code for java.sql Types CLOB.

Click Source Link

Document

The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type CLOB.

Usage

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

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

    StringWriter writer = new StringWriter();
    try (OutputStream out = new WriterOutputStream(writer)) {
        translator.write(row, 0, out);//from  www.j  av  a 2s  .co  m
    }
    String translated = writer.toString();
    assertEquals("SomeData", translated);
}

From source file:io.lightlink.oracle.AbstractOracleType.java

protected ARRAY createArray(Connection con, Object value, String typeName) throws SQLException {

    if (value == null)
        return null;

    ArrayDescriptor arrayStructDesc = safeCreateArrayDescriptor(typeName, con);

    if (value == null)
        return null;

    if (value.getClass().isArray()) {
        value = Arrays.asList((Object[]) value);
    }//from   ww  w . ja va2s . co m

    List records = (List) value;
    String baseName = arrayStructDesc.getBaseName();
    int baseType = arrayStructDesc.getBaseType();
    if (baseType == Types.VARCHAR || baseType == Types.CHAR || baseType == Types.CLOB
            || baseType == Types.NUMERIC || baseType == Types.INTEGER || baseType == Types.BIGINT
            || baseType == Types.FLOAT || baseType == Types.DOUBLE || baseType == Types.DECIMAL
            || baseType == Types.NCHAR || baseType == Types.NVARCHAR || baseType == Types.NCLOB) {
        return new ARRAY(arrayStructDesc, con, records.toArray()); // primitive array
    } else {

        Object[] structArray = new Object[records.size()];

        for (int i = 0; i < structArray.length; i++) {
            Object record = records.get(i);

            if (baseType == OracleTypes.JAVA_STRUCT || baseType == OracleTypes.JAVA_OBJECT
                    || baseType == OracleTypes.STRUCT || baseType == OracleTypes.JAVA_STRUCT) {
                record = createStruct(con, record, baseName);
            } else if (baseType == OracleTypes.ARRAY) {
                record = createArray(con, record, baseName);
            }
            structArray[i] = record;
        }

        return new ARRAY(arrayStructDesc, con, structArray);
    }
}

From source file:it.greenvulcano.gvesb.utils.ResultSetUtils.java

/**
 * Returns all values from the ResultSet as an XML.
 * For instance, if the ResultSet has 3 values, the returned XML will have following fields:
 *                                <RowSet>
 *                                  <data>
 *                                    <row>
 *                                      <col>value1</col>
 *                                      <col>value2</col>
 *                                      <col>value3</col>
 *                                    </row>
 *                                    <row>
 *                                      <col>value4</col>
 *                                      <col>value5</col>
 *                                      <col>value6</col>
 *                                    </row>
 *                                  ..//w  w w .j  av  a  2s. com
 *                                    <row>
 *                                      <col>valuex</col>
 *                                      <col>valuey</col>
 *                                      <col>valuez</col>
 *                                    </row>
 *                                  </data>
 *                                </RowSet>
 * @param rs
 * @return
 * @throws Exception
 */
public static Document getResultSetAsDOM(ResultSet rs) throws Exception {
    XMLUtils xml = XMLUtils.getParserInstance();
    try {
        Document doc = xml.newDocument("RowSet");
        Element docRoot = doc.getDocumentElement();

        if (rs != null) {
            try {
                ResultSetMetaData metadata = rs.getMetaData();
                Element data = null;
                Element row = null;
                Element col = null;
                Text text = null;
                String textVal = null;
                while (rs.next()) {
                    boolean restartResultset = false;
                    for (int j = 1; j <= metadata.getColumnCount() && !restartResultset; j++) {
                        col = xml.createElement(doc, "col");
                        restartResultset = false;
                        switch (metadata.getColumnType(j)) {
                        case Types.CLOB: {
                            Clob clob = rs.getClob(j);
                            if (clob != null) {
                                Reader is = clob.getCharacterStream();
                                StringWriter strW = new StringWriter();

                                IOUtils.copy(is, strW);
                                is.close();
                                textVal = strW.toString();
                            } else {
                                textVal = "";
                            }
                        }
                            break;
                        case Types.BLOB: {
                            Blob blob = rs.getBlob(j);
                            if (blob != null) {
                                InputStream is = blob.getBinaryStream();
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                IOUtils.copy(is, baos);
                                is.close();
                                try {
                                    byte[] buffer = Arrays.copyOf(baos.toByteArray(), (int) blob.length());
                                    textVal = new String(Base64.getEncoder().encode(buffer));
                                } catch (SQLFeatureNotSupportedException exc) {
                                    textVal = new String(Base64.getEncoder().encode(baos.toByteArray()));
                                }
                            } else {
                                textVal = "";
                            }
                        }
                            break;
                        case -10: { // OracleTypes.CURSOR
                            Object obj = rs.getObject(j);
                            if (obj instanceof ResultSet) {
                                rs = (ResultSet) obj;
                                metadata = rs.getMetaData();
                            }
                            restartResultset = true;
                        }
                            break;
                        default: {
                            textVal = rs.getString(j);
                            if (textVal == null) {
                                textVal = "";
                            }
                        }
                        }
                        if (restartResultset) {
                            continue;
                        }
                        if (row == null || j == 1) {
                            row = xml.createElement(doc, "row");
                        }
                        if (textVal != null) {
                            text = doc.createTextNode(textVal);
                            col.appendChild(text);
                        }
                        row.appendChild(col);
                    }
                    if (row != null) {
                        if (data == null) {
                            data = xml.createElement(doc, "data");
                        }
                        data.appendChild(row);
                    }
                }
                if (data != null) {
                    docRoot.appendChild(data);
                }
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (Exception exc) {
                        // do nothing
                    }
                    rs = null;
                }
            }
        }

        return doc;
    } finally {
        XMLUtils.releaseParserInstance(xml);
    }
}

From source file:org.jumpmind.db.platform.h2.H2DdlBuilder.java

public H2DdlBuilder() {

    super(DatabaseNamesConstants.H2);

    databaseInfo.setNonPKIdentityColumnsSupported(false);
    databaseInfo.setIdentityOverrideAllowed(false);
    databaseInfo.setSystemForeignKeyIndicesAlwaysNonUnique(true);
    databaseInfo.setNullAsDefaultValueRequired(false);
    databaseInfo.addNativeTypeMapping(Types.ARRAY, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.DISTINCT, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.NULL, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.REF, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.STRUCT, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.DATALINK, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.BIT, "BOOLEAN", Types.BIT);
    databaseInfo.addNativeTypeMapping(Types.NUMERIC, "DECIMAL", Types.DECIMAL);
    databaseInfo.addNativeTypeMapping(Types.BINARY, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.BLOB, "BLOB", Types.BLOB);
    databaseInfo.addNativeTypeMapping(Types.CLOB, "CLOB", Types.CLOB);
    databaseInfo.addNativeTypeMapping(Types.LONGVARCHAR, "VARCHAR(" + Integer.MAX_VALUE + ")", Types.VARCHAR);
    databaseInfo.addNativeTypeMapping(Types.FLOAT, "DOUBLE", Types.DOUBLE);
    databaseInfo.addNativeTypeMapping(Types.JAVA_OBJECT, "OTHER");

    databaseInfo.setDefaultSize(Types.CHAR, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.VARCHAR, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.BINARY, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.VARBINARY, Integer.MAX_VALUE);

    databaseInfo.setNonBlankCharColumnSpacePadded(false);
    databaseInfo.setBlankCharColumnSpacePadded(false);
    databaseInfo.setCharColumnSpaceTrimmed(true);
    databaseInfo.setEmptyStringNulled(false);
    databaseInfo.setNullAsDefaultValueRequired(true);

}

From source file:org.apache.ddlutils.platform.postgresql.PostgreSqlPlatform.java

/**
 * Creates a new platform instance./*w w w . j av a  2s  . c  o  m*/
 */
public PostgreSqlPlatform() {
    PlatformInfo info = getPlatformInfo();

    info.setPrimaryKeyColumnAutomaticallyRequired(true);
    // this is the default length though it might be changed when building PostgreSQL
    // in file src/include/postgres_ext.h
    info.setMaxIdentifierLength(31);

    info.addNativeTypeMapping(Types.ARRAY, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.BINARY, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.BIT, "BOOLEAN");
    info.addNativeTypeMapping(Types.BLOB, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.BOOLEAN, "BOOLEAN", Types.BIT);
    info.addNativeTypeMapping(Types.CLOB, "TEXT", Types.LONGVARCHAR);
    info.addNativeTypeMapping(Types.DATALINK, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.DECIMAL, "NUMERIC", Types.NUMERIC);
    info.addNativeTypeMapping(Types.DISTINCT, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION");
    info.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION", Types.DOUBLE);
    info.addNativeTypeMapping(Types.JAVA_OBJECT, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.LONGVARBINARY, "BYTEA");
    info.addNativeTypeMapping(Types.LONGVARCHAR, "TEXT", Types.LONGVARCHAR);
    info.addNativeTypeMapping(Types.NULL, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.OTHER, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.REF, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.STRUCT, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.TINYINT, "SMALLINT", Types.SMALLINT);
    info.addNativeTypeMapping(Types.VARBINARY, "BYTEA", Types.LONGVARBINARY);

    info.setDefaultSize(Types.CHAR, 254);
    info.setDefaultSize(Types.VARCHAR, 254);

    // no support for specifying the size for these types (because they are mapped
    // to BYTEA which back-maps to BLOB)
    info.setHasSize(Types.BINARY, false);
    info.setHasSize(Types.VARBINARY, false);

    setSqlBuilder(new PostgreSqlBuilder(this));
    setModelReader(new PostgreSqlModelReader(this));
}

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

public int[] sqlTypes() {
    return new int[] { Types.CLOB };
}

From source file:org.jumpmind.symmetric.db.derby.DerbyFunctions.java

protected static void appendCsvString(String tableName, String[] columnNames, String[] pkColumnNames,
        ResultSet rs, StringBuilder builder) throws SQLException {
    ResultSetMetaData metaData = rs.getMetaData();
    for (String columnName : columnNames) {
        if (StringUtils.isNotBlank(columnName)) {
            int index = findColumnIndex(metaData, columnName);
            if (index >= 0) {
                int type = metaData.getColumnType(index);
                switch (type) {
                case Types.BLOB:
                    builder.append(// ww  w  .  j a va 2s  .c  o m
                            blobToString(columnName, tableName, getPrimaryKeyWhereString(pkColumnNames, rs)));
                    builder.append(",");
                    break;
                case Types.CLOB:
                    builder.append(
                            clobToString(columnName, tableName, getPrimaryKeyWhereString(pkColumnNames, rs)));
                    builder.append(",");
                    break;
                default:
                    builder.append(escape(rs.getString(index)));
                    builder.append(",");
                    break;
                }
            } else {
                builder.append(",");
            }
        } else {
            builder.append(",");
        }
    }
}

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

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

    StringWriter writer = new StringWriter();
    try (OutputStream out = new WriterOutputStream(writer)) {
        translator.write(row, "testField", out);
    }/*from  ww  w  .ja  v  a  2 s. c  o m*/
    String translated = writer.toString();
    assertEquals("SomeData", translated);
}

From source file:data.DefaultExchanger.java

protected void setClob(PreparedStatement ps, short index, JsonNode node, String column) throws SQLException {
    String value = node.get(column).textValue();
    if (value == null) {
        ps.setNull(index, Types.CLOB);
    } else {/*from w ww  .j  a  va2s .  c o  m*/
        Clob clob = ps.getConnection().createClob();
        clob.setString(1, value);
        ps.setClob(index, clob);
    }
}

From source file:org.jumpmind.db.platform.sqlite.SqliteJdbcSqlTemplate.java

@Override
public void setValues(PreparedStatement ps, Object[] args, int[] argTypes, LobHandler lobHandler)
        throws SQLException {
    for (int i = 1; i <= args.length; i++) {
        Object arg = args[i - 1];
        int argType = argTypes != null && argTypes.length >= i ? argTypes[i - 1] : SqlTypeValue.TYPE_UNKNOWN;

        if (argType == Types.BLOB && lobHandler != null && arg instanceof byte[]) {
            lobHandler.getLobCreator().setBlobAsBytes(ps, i, (byte[]) arg);
        } else if (argType == Types.BLOB && lobHandler != null && arg instanceof String) {
            lobHandler.getLobCreator().setBlobAsBytes(ps, i, arg.toString().getBytes());
        } else if (argType == Types.CLOB && lobHandler != null) {
            lobHandler.getLobCreator().setClobAsString(ps, i, (String) arg);
        } else if (arg != null && argType == Types.DATE && arg instanceof Date) {
            Date clone = (Date) (((Date) arg).clone());
            arg = FormatUtils.TIMESTAMP_FORMATTER.format(DateUtils.truncate(clone, Calendar.DATE));
            args[i - 1] = arg;//ww w  .jav a 2 s . c om
            StatementCreatorUtils.setParameterValue(ps, i, verifyArgType(arg, argType), arg);
        } else if (arg != null && (arg instanceof Date || arg instanceof Timestamp)) {
            arg = FormatUtils.TIMESTAMP_FORMATTER.format(arg);
            args[i - 1] = arg;
            StatementCreatorUtils.setParameterValue(ps, i, verifyArgType(arg, argType), arg);
        } else {
            if (arg instanceof BigDecimal) {
                arg = ((BigDecimal) arg).doubleValue();
                args[i - 1] = arg;
            }
            StatementCreatorUtils.setParameterValue(ps, i, verifyArgType(arg, argType), arg);
        }
    }
}