Example usage for java.sql ResultSetMetaData getColumnClassName

List of usage examples for java.sql ResultSetMetaData getColumnClassName

Introduction

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

Prototype

String getColumnClassName(int column) throws SQLException;

Source Link

Document

Returns the fully-qualified name of the Java class whose instances are manufactured if the method ResultSet.getObject is called to retrieve a value from the column.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();// w  w w  .  j a va  2  s. c  o  m
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);

        // get the designated column's class name.
        System.out.println(rsMetaData.getColumnClassName(i));
    }

    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();/* w w w  .j av  a2 s  .c  o  m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);
        System.out.println(rsMetaData.getColumnDisplaySize(i));
        System.out.println(rsMetaData.getColumnLabel(i));
        System.out.println(rsMetaData.getColumnName(i));
        System.out.println(rsMetaData.getColumnType(i));
        System.out.println(rsMetaData.getColumnTypeName(i));
        System.out.println(rsMetaData.getColumnClassName(i));
        System.out.println(rsMetaData.getTableName(i));
        System.out.println(rsMetaData.getPrecision(i));
        System.out.println(rsMetaData.getScale(i));
        System.out.println(rsMetaData.isAutoIncrement(i));
        System.out.println(rsMetaData.isCurrency(i));
        System.out.println(rsMetaData.isWritable(i));
        System.out.println(rsMetaData.isDefinitelyWritable(i));
        System.out.println(rsMetaData.isNullable(i));
        System.out.println(rsMetaData.isReadOnly(i));
        System.out.println(rsMetaData.isCaseSensitive(i));
        System.out.println(rsMetaData.isSearchable(i));
        System.out.println(rsMetaData.isSigned(i));
        System.out.println(rsMetaData.getCatalogName(i));
        System.out.println(rsMetaData.getSchemaName(i));
    }
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();// w w  w  .  j  a va  2s  . co m
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);
        // indicates the designated column's normal maximum width in
        // characters
        System.out.println(rsMetaData.getColumnDisplaySize(i));
        // gets the designated column's suggested title
        // for use in printouts and displays.
        System.out.println(rsMetaData.getColumnLabel(i));
        // get the designated column's name.
        System.out.println(rsMetaData.getColumnName(i));

        // get the designated column's SQL type.
        System.out.println(rsMetaData.getColumnType(i));

        // get the designated column's SQL type name.
        System.out.println(rsMetaData.getColumnTypeName(i));

        // get the designated column's class name.
        System.out.println(rsMetaData.getColumnClassName(i));

        // get the designated column's table name.
        System.out.println(rsMetaData.getTableName(i));

        // get the designated column's number of decimal digits.
        System.out.println(rsMetaData.getPrecision(i));

        // gets the designated column's number of
        // digits to right of the decimal point.
        System.out.println(rsMetaData.getScale(i));

        // indicates whether the designated column is
        // automatically numbered, thus read-only.
        System.out.println(rsMetaData.isAutoIncrement(i));

        // indicates whether the designated column is a cash value.
        System.out.println(rsMetaData.isCurrency(i));

        // indicates whether a write on the designated
        // column will succeed.
        System.out.println(rsMetaData.isWritable(i));

        // indicates whether a write on the designated
        // column will definitely succeed.
        System.out.println(rsMetaData.isDefinitelyWritable(i));

        // indicates the nullability of values
        // in the designated column.
        System.out.println(rsMetaData.isNullable(i));

        // Indicates whether the designated column
        // is definitely not writable.
        System.out.println(rsMetaData.isReadOnly(i));

        // Indicates whether a column's case matters
        // in the designated column.
        System.out.println(rsMetaData.isCaseSensitive(i));

        // Indicates whether a column's case matters
        // in the designated column.
        System.out.println(rsMetaData.isSearchable(i));

        // indicates whether values in the designated
        // column are signed numbers.
        System.out.println(rsMetaData.isSigned(i));

        // Gets the designated column's table's catalog name.
        System.out.println(rsMetaData.getCatalogName(i));

        // Gets the designated column's table's schema name.
        System.out.println(rsMetaData.getSchemaName(i));
    }

    st.close();
    conn.close();
}

From source file:org.openadaptor.util.JDBCUtil.java

public static void logCurrentResultSetRow(Log log, String msg, ResultSet rs) throws SQLException {
    if (log.isDebugEnabled()) {
        ResultSetMetaData rsmd = rs.getMetaData();
        if (msg != null) {
            log.debug(msg);/*  w  ww.  ja v a  2 s.c  om*/
        }
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            log.debug("  " + rsmd.getColumnName(i) + " (" + rsmd.getColumnClassName(i) + ") = "
                    + rs.getString(i));
        }
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.misc.tablemodel.TypeMapper.java

public static Class<?> mapForColumn(ResultSetMetaData rsmd, int i) {
    try {//from   ww  w  .j  a  v a 2s .  c om
        final ClassLoader cl = ObjectUtilities.getClassLoader(TypeMapper.class);
        try {
            final String tn = rsmd.getColumnClassName(i + 1);
            if (tn == null) {
                final int colType = rsmd.getColumnType(i + 1);
                return mapSQLType(colType);
            } else {
                return Class.forName(tn, false, cl);
            }
        } catch (final Exception oops) {
            // ignore exception
            final int colType = rsmd.getColumnType(i + 1);
            return mapSQLType(colType);
        }
    } catch (Exception e) {
        // still ignore the exception
        return Object.class;
    }
}

From source file:org.squale.welcom.outils.jdbc.wrapper.ResultSetUtils.java

/**
 * Populate the properties of the specified JavaBean from the next record of the specified ResultSet, based on
 * matching each column name against the corresponding JavaBeans "property setter" methods in the bean's class.
 * Suitable conversion is done for argument types as described under <code>convert()</code>.
 * /*from   w  ww  .  j a  v  a2 s.  c om*/
 * @param bean The JavaBean whose properties are to be set
 * @param resultSet The ResultSet whose parameters are to be used to populate bean properties
 * @param formatDate (@link SimpleDateFormat)
 * @exception SQLException if an exception is thrown while setting property values or access the ResultSet
 */
public static void populate(final Object bean, final ResultSet resultSet, final SimpleDateFormat formatDate)
        throws SQLException {
    // Build a list of relevant column properties from this resultSet
    final HashMap properties = new HashMap();

    // Acquire resultSet MetaData
    final ResultSetMetaData metaData = resultSet.getMetaData();
    final int cols = metaData.getColumnCount();

    // Scroll to next record and pump into hashmap
    // if (resultSet.next())
    for (int i = 1; i <= cols; i++) {
        if (metaData.getColumnClassName(i).equals("java.sql.Timestamp")) {
            if (resultSet.getTimestamp(i) != null) {
                properties.put(metaData.getColumnName(i),
                        formatDate.format(new Date(resultSet.getTimestamp(i).getTime())));
            } else {
                properties.put(metaData.getColumnName(i), "");
            }
        } else {
            properties.put(metaData.getColumnName(i), resultSet.getString(i));
        }
    }

    // Set the corresponding properties of our bean
    try {
        populateIgnoreCase(bean, properties);
    } catch (final Exception e) {
        throw new SQLException("BeanUtils.populate threw " + e.toString());
    }
}

From source file:org.squale.welcom.outils.jdbc.wrapper.ResultSetUtils.java

/**
 * Populate the properties of the specified JavaBean from the next record of the specified ResultSet, based on
 * matching each column name against the corresponding JavaBeans "property setter" methods in the bean's class.
 * Suitable conversion is done for argument types as described under <code>convert()</code>.
 * /* w w w  .  ja va  2s.c o m*/
 * @param bean The JavaBean whose properties are to be set
 * @param resultSet The ResultSet whose parameters are to be used to populate bean properties
 * @exception SQLException if an exception is thrown while setting property values or access the ResultSet
 */
public static void populate(final Object bean, final ResultSet resultSet) throws SQLException {
    // Format pour les dates
    final SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy HH:mm");

    // Build a list of relevant column properties from this resultSet
    final HashMap properties = new HashMap();

    // Acquire resultSet MetaData
    final ResultSetMetaData metaData = resultSet.getMetaData();
    final int cols = metaData.getColumnCount();

    // Scroll to next record and pump into hashmap
    // if (resultSet.next())
    for (int i = 1; i <= cols; i++) {
        if (metaData.getColumnClassName(i).equals("java.sql.Timestamp")) {

            properties.put(metaData.getColumnName(i),
                    getFormattedDateTimeColumn(resultSet.getTimestamp(i), formatDate));

        } else if (metaData.getColumnClassName(i).equals("oracle.sql.CLOB")) {

            properties.put(metaData.getColumnName(i), getFormattedClobColumn(resultSet.getAsciiStream(i)));

        } else {

            properties.put(metaData.getColumnName(i), getFormattedStringColumn(resultSet.getString(i)));
        }
    }

    // Set the corresponding properties of our bean
    try {
        populateIgnoreCase(bean, properties);
    } catch (final Exception e) {
        throw new SQLException("BeanUtils.populate threw " + e.toString());
    }
}

From source file:org.jabsorb.ext.DataList.java

/**
 * Build an array of ColumnMetaData object from a ResultSetMetaData object.
 *
 * @param rmd ResultSetMetaData to build ColumnMetaData from.
 * @return ColumnMetaData array or null if ResultSetMetaData is null.
 *
 * @throws SQLException if there is a problem processing the 
 * ResultSetMetaData object.//from w w w . j a  v  a  2 s. c  o  m
 */
public static ColumnMetaData[] buildColumnMetaDataFromResultSetMetaData(ResultSetMetaData rmd)
        throws SQLException {
    if (rmd == null) {
        return null;
    }

    int j = rmd.getColumnCount();

    ColumnMetaData[] cmd = new ColumnMetaData[j];

    for (int i = 1; i <= j; i++) {
        ColumnMetaData c = new ColumnMetaData();

        c.setColumnName(rmd.getColumnName(i));
        c.setCatalogName(rmd.getCatalogName(i));
        c.setColumnClassName(rmd.getColumnClassName(i));
        c.setColumnDisplaySize(rmd.getColumnDisplaySize(i));
        c.setColumnLabel(rmd.getColumnLabel(i));
        c.setColumnType(rmd.getColumnType(i));
        c.setColumnTypeName(rmd.getColumnTypeName(i));
        c.setPrecision(rmd.getPrecision(i));
        c.setScale(rmd.getScale(i));
        c.setSchemaName(rmd.getSchemaName(i));
        c.setTableName(rmd.getTableName(i));
        c.setAutoIncrement(rmd.isAutoIncrement(i));
        c.setCaseSensitive(rmd.isCaseSensitive(i));
        c.setCurrency(rmd.isCurrency(i));
        c.setNullable(rmd.isNullable(i));
        c.setReadOnly(rmd.isReadOnly(i));
        c.setSearchable(rmd.isSearchable(i));
        c.setSigned(rmd.isSigned(i));
        c.setWritable(rmd.isWritable(i));
        c.setDefinitelyWritable(rmd.isDefinitelyWritable(i));

        cmd[i - 1] = c;
    }
    return cmd;
}

From source file:org.opoo.oqs.core.mapper.SinglePropertyMapper.java

protected Type guessType(ResultSetMetaData rsmd, int i) {
    try {//ww  w .j av  a  2  s.c  o m
        String className = rsmd.getColumnClassName(i);
        return TypeFactory.guessType(className);
    } catch (SQLException ex) {
        log.error("cannot guess type from result metadata column classname: " + i, ex);
    }
    return null;
}

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;// ww w.jav  a 2  s  . com
    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));
    }

}