Example usage for java.sql ResultSetMetaData getColumnType

List of usage examples for java.sql ResultSetMetaData getColumnType

Introduction

In this page you can find the example usage for java.sql ResultSetMetaData getColumnType.

Prototype

int getColumnType(int column) throws SQLException;

Source Link

Document

Retrieves the designated column's SQL type.

Usage

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

public static boolean isBlobType(final ResultSet rs, final int colNum, final ResultSetMetaData rsmeta)
        throws SQLException {
    switch (rsmeta.getColumnType(colNum)) {
    case Types.LONGVARBINARY:
    case Types.VARBINARY:
    case Types.BLOB:
        return true;
    default://from  w ww.  j av a2 s  . c o  m
        return false;
    }
}

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

public Object getSource(String id, ResultSet rs) throws SQLException {
    ResultSetMetaData metadata = rs.getMetaData();
    String rv = null;// www .  j a v  a  2s  . 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.content.impl.serialize.impl.conversion.Type1BlobCollectionConversionHandler.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 ww w.  j av a  2s  .  com*/
            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;
}

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 a v  a 2s .  c  o 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:cherry.foundation.etl.ExtractorResultSetExtractor.java

@Override
public Long extractData(ResultSet rs) throws SQLException, DataAccessException {
    try {/*www .  j av  a  2 s  .c  o  m*/

        ResultSetMetaData metaData = rs.getMetaData();
        Column[] col = new Column[metaData.getColumnCount()];
        for (int i = 1; i <= col.length; i++) {
            col[i - 1] = new Column();
            col[i - 1].setType(metaData.getColumnType(i));
            col[i - 1].setLabel(metaData.getColumnLabel(i));
        }

        consumer.begin(col);

        long count;
        for (count = 0L; rs.next(); count++) {

            Object[] record = new Object[col.length];
            for (int i = 1; i <= record.length; i++) {
                record[i - 1] = rs.getObject(i);
            }

            consumer.consume(record);
            limiter.tick();
        }

        consumer.end();
        return count;

    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:br.bookmark.db.util.ResultSetUtils.java

/**
 * Map JDBC objects to Java equivalents.
 * Used by getBean() and getBeans().//w w w. j  a v a2 s  .c  o  m
 * <p>
 * Some types not supported.
 * Many not work with all drivers.
 * <p>
 * Makes binary conversions of BIGINT, DATE, DECIMAL, DOUBLE, FLOAT, INTEGER,
 * REAL, SMALLINT, TIME, TIMESTAMP, TINYINT.
 * Makes Sting conversions of CHAR, CLOB, VARCHAR, LONGVARCHAR, BLOB, LONGVARBINARY,
 * VARBINARY.
 * <p>
 * DECIMAL, INTEGER, SMALLINT, TIMESTAMP, CHAR, VARCHAR tested with MySQL and Poolman.
 * Others not guaranteed.
 * @param classeDestino 
 * @throws NoSuchFieldException 
 * @throws SecurityException 
 */
private static void putEntry(Map properties, ResultSetMetaData metaData, ResultSet resultSet, int i,
        Class classeDestino) throws Exception {

    /*
    In a perfect universe, this would be enough
    properties.put(
        metaData.getColumnName(i),
        resultSet.getObject(i));
    But only String, Timestamp, and Integer seem to get through that way.
    */

    String columnName = metaData.getColumnName(i);

    // Testa se  uma FK
    /*Field[] fields = classeDestino.getDeclaredFields();
    for (int j = 0; j < fields.length; j++) {
    if (fields[j].getAnnotation(DBFK.class) != null) {
        properties.put(columnName, resultSet.getString(i));
    }
    }*/
    //System.out.println(i+"-"+metaData.getColumnType(i));
    switch (metaData.getColumnType(i)) {

    // http://java.sun.com/j2se/1.3.0/docs/api/java/sql/Types.html

    case Types.BIGINT:
        properties.put(columnName, new Long(resultSet.getLong(i)));
        break;

    case Types.DATE:
        properties.put(columnName, resultSet.getDate(i));
        break;

    case Types.DECIMAL:
    case Types.DOUBLE:
        properties.put(columnName, new Double(resultSet.getDouble(i)));
        break;

    case Types.FLOAT:
        properties.put(columnName, new Float(resultSet.getFloat(i)));
        break;

    case Types.INTEGER:
        int valor = 0;
        try { // Se o campo esta vazio d erro
            valor = resultSet.getInt(i);
        } catch (SQLException e) {
        }
        properties.put(columnName, new Integer(valor));
        break;

    case Types.REAL:
        properties.put(columnName, new Double(resultSet.getString(i)));
        break;

    case Types.SMALLINT:
        properties.put(columnName, new Short(resultSet.getShort(i)));
        break;

    case Types.TIME:
        properties.put(columnName, resultSet.getTime(i));
        break;

    case Types.TIMESTAMP:
        properties.put(columnName, resultSet.getTimestamp(i));
        break;

    // :FIXME: Throws java.lang.ClassCastException: java.lang.Integer
    // :FIXME: with Poolman and MySQL unless use getString.
    case Types.TINYINT:
        properties.put(columnName, new Byte(resultSet.getString(i)));
        break;

    case Types.CHAR:
    case Types.CLOB:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        // :FIXME: Handle binaries differently?
    case Types.BLOB:
    case Types.LONGVARBINARY:
    case Types.VARBINARY:
        properties.put(columnName, resultSet.getString(i));
        break;

    /*
        :FIXME: Add handlers for
        ARRAY
        BINARY
        BIT
        DISTINCT
        JAVA_OBJECT
        NULL
        NUMERIC
        OTHER
        REF
        STRUCT
    */

    // Otherwise, pass as *String property to be converted
    default:
        properties.put(columnName + "String", resultSet.getString(i));
        break;
    } // end switch

}

From source file:org.jtalks.poulpe.util.databasebackup.persistence.DbTableData.java

/**
 * Constructs a new ColumnMetaData objects from given ResultSetMetaData with provided column default values map and
 * the object's index.//from  w ww . jav a2  s  . c  o m
 * 
 * @param rsmd
 *            A ResultSetMetaData which contains meta information about all columns for the table.
 * @param columnDefaultValues
 *            A map of possibly defined values by default for columns.
 * @param i
 *            Index of column which should be constructed.
 * @return A constructed ColumnMetaData object.
 * @throws SQLException
 *             Is thrown in case any errors during work with database occur.
 */
private ColumnMetaData getColumnMetaData(ResultSetMetaData rsmd, Map<String, String> columnDefaultValues, int i)
        throws SQLException {
    SqlTypes columnType = SqlTypes.getSqlTypeByJdbcSqlType(rsmd.getColumnType(i));

    ColumnMetaData column = ColumnMetaData.getInstance(rsmd.getColumnName(i), columnType)
            .setNullable(rsmd.isNullable(i) == ResultSetMetaData.columnNullable)
            .setAutoincrement(rsmd.isAutoIncrement(i));
    if (columnDefaultValues.containsKey(rsmd.getColumnName(i))) {
        column.setDefaultValue(columnDefaultValues.get(rsmd.getColumnName(i)));
    }
    if (columnType.isHasSize()) {
        column.setSize(rsmd.getColumnDisplaySize(i));
    }
    return column;
}

From source file:solidbase.core.plugins.DBReader.java

public DBReader(ResultSet result, ExportLogger counter, boolean dateAsTimestamp) throws SQLException {
    this.result = result;
    this.counter = counter;

    ResultSetMetaData metaData = result.getMetaData();
    int count = metaData.getColumnCount();
    this.columns = new Column[count];

    for (int i = 0; i < count; i++) {
        int col = i + 1;
        String name = metaData.getColumnName(col).toUpperCase();
        int type = metaData.getColumnType(col);
        if (type == Types.DATE && dateAsTimestamp)
            type = Types.TIMESTAMP;
        String table = StringUtils.upperCase(StringUtils.defaultIfEmpty(metaData.getTableName(col), null));
        String schema = StringUtils.upperCase(StringUtils.defaultIfEmpty(metaData.getSchemaName(col), null));
        this.columns[i] = new Column(name, type, table, schema);
    }//from   w  w  w.j a v a2s .c om
}

From source file:com.tesora.dve.sql.util.JdbcConnectionResourceResponse.java

@Override
public List<ColumnChecker> getColumnCheckers() throws Throwable {
    ResultSetMetaData rsmd = results.getMetaData();
    ArrayList<ColumnChecker> checkers = new ArrayList<ColumnChecker>();
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        int dt = rsmd.getColumnType(i);
        if (dt == Types.BINARY || dt == Types.VARBINARY || dt == Types.LONGVARBINARY)
            checkers.add(BLOB_COLUMN);//from   ww  w . j  av  a2 s. c o m
        else if (dt == Types.TIMESTAMP)
            checkers.add(TIMESTAMP_COLUMN);
        else
            checkers.add(REGULAR_COLUMN);
    }
    return checkers;
}

From source file:org.apache.sqoop.manager.sqlserver.MSSQLTestUtils.java

public void metadataStuff(String table) {
    Connection dbcon = this.getConnection();
    String sql = "select top 1 * from " + table;

    Statement st;/*  w  w w  . ja  v  a  2 s .c om*/
    try {

        st = dbcon.createStatement();
        ResultSet rs = st.executeQuery(sql);
        ResultSetMetaData rsmd = rs.getMetaData();

        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            System.out.println(rsmd.getColumnName(i) + "\t" + rsmd.getColumnClassName(i) + "\t"
                    + rsmd.getColumnType(i) + "\t" + rsmd.getColumnTypeName(i) + "\n");
        }

    } catch (SQLException e) {
        LOG.error(StringUtils.stringifyException(e));
    }

}