Example usage for java.sql ResultSetMetaData getCatalogName

List of usage examples for java.sql ResultSetMetaData getCatalogName

Introduction

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

Prototype

String getCatalogName(int column) throws SQLException;

Source Link

Document

Gets the designated column's table's catalog name.

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();//from   w  ww  .j av  a 2s.  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);

        // Gets the designated column's table's catalog name.
        System.out.println(rsMetaData.getCatalogName(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();//from   w w w .jav  a 2 s  . 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);
        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();/*from w  w  w .  j a va2 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);
        // 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.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 ww  w .jav a2  s .c  om*/
 */
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:kenh.xscript.database.beans.ResultSetBean.java

/**
 * Use result set to initial a bean.//from   w  ww  . ja va2s.c  o  m
 * 
 * @param rs
 * @param includeFieldName
 * @throws SQLException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public ResultSetBean(ResultSet rs, boolean includeFieldName)
        throws SQLException, IllegalAccessException, InstantiationException {
    include_field_name = includeFieldName;

    LazyDynaClass beanClass = new LazyDynaClass();

    ResultSetMetaData m = rs.getMetaData();
    for (int i = 1; i <= m.getColumnCount(); i++) {
        Column c = new Column();

        try {
            c.catalogName = m.getCatalogName(i);
        } catch (SQLException e) {
        }
        try {
            c.className = m.getColumnClassName(i);
        } catch (SQLException e) {
        }
        try {
            c.displaySize = m.getColumnDisplaySize(i);
        } catch (SQLException e) {
        }
        try {
            c.label = m.getColumnLabel(i);
        } catch (SQLException e) {
        }
        try {
            c.name = m.getColumnName(i);
        } catch (SQLException e) {
        }
        try {
            c.type = m.getColumnType(i);
        } catch (SQLException e) {
        }
        try {
            c.typeName = m.getColumnTypeName(i);
        } catch (SQLException e) {
        }
        try {
            c.precision = m.getPrecision(i);
        } catch (SQLException e) {
        }
        try {
            c.scale = m.getScale(i);
        } catch (SQLException e) {
        }
        try {
            c.schemaName = m.getSchemaName(i);
        } catch (SQLException e) {
        }
        try {
            c.tableName = m.getTableName(i);
        } catch (SQLException e) {
        }

        beanClass.add(m.getColumnLabel(i).toLowerCase());
        beanClass.add("" + i);

        cols.add(c);
    }

    DynaBean colBean = beanClass.newInstance();
    int i = 1;
    for (Column col : cols) {
        String field = col.getLabel().toLowerCase();
        colBean.set(field, col.getLabel());
        colBean.set("" + i, col.getLabel());
        i++;
    }

    if (include_field_name)
        rows.add(colBean);

    while (rs.next()) {
        DynaBean bean = beanClass.newInstance();
        i = 1;
        for (Column c : cols) {
            String field = c.getLabel().toLowerCase();
            Object obj = rs.getObject(field);
            bean.set(field, obj);
            bean.set("" + i, obj);
            i++;
        }
        rows.add(bean);
    }

}

From source file:org.orbisgis.geocatalog.impl.SourceListModel.java

private void putAttribute(Map<IFilter.ATTRIBUTES, String> tableAttr, IFilter.ATTRIBUTES attribute,
        ResultSet rs) {/*from w ww . j ava  2  s.  c  om*/
    try {
        String columnName = attribute.toString().toLowerCase();
        Integer columnId = columnMap.get(columnName);
        if (columnId == null) {
            ResultSetMetaData meta = rs.getMetaData();
            columnMap.put(columnName, 0);
            for (int i = 1; i <= meta.getColumnCount(); i++) {
                if (columnName.equals(meta.getCatalogName(i).toLowerCase())) {
                    columnMap.put(columnName, i);
                    columnId = i;
                    break;
                }
            }
        }
        if (columnId != null && columnId > 0) {
            tableAttr.put(attribute, rs.getString(columnId));
        }
    } catch (SQLException ex) {
        // Ignore
    }
}

From source file:org.apache.calcite.avatica.jdbc.JdbcMeta.java

/**
 * Convert from JDBC metadata to Avatica columns.
 *//*from   ww  w .  j av a  2s . c  o  m*/
protected static List<ColumnMetaData> columns(ResultSetMetaData metaData) throws SQLException {
    if (metaData == null) {
        return Collections.emptyList();
    }
    final List<ColumnMetaData> columns = new ArrayList<>();
    for (int i = 1; i <= metaData.getColumnCount(); i++) {
        final SqlType sqlType = SqlType.valueOf(metaData.getColumnType(i));
        final ColumnMetaData.Rep rep = ColumnMetaData.Rep.of(sqlType.internal);
        ColumnMetaData.AvaticaType t = ColumnMetaData.scalar(metaData.getColumnType(i),
                metaData.getColumnTypeName(i), rep);
        ColumnMetaData md = new ColumnMetaData(i - 1, metaData.isAutoIncrement(i), metaData.isCaseSensitive(i),
                metaData.isSearchable(i), metaData.isCurrency(i), metaData.isNullable(i), metaData.isSigned(i),
                metaData.getColumnDisplaySize(i), metaData.getColumnLabel(i), metaData.getColumnName(i),
                metaData.getSchemaName(i), metaData.getPrecision(i), metaData.getScale(i),
                metaData.getTableName(i), metaData.getCatalogName(i), t, metaData.isReadOnly(i),
                metaData.isWritable(i), metaData.isDefinitelyWritable(i), metaData.getColumnClassName(i));
        columns.add(md);
    }
    return columns;
}

From source file:org.apache.hadoop.hive.jdbc.storagehandler.AtsdDBRecordReader.java

private ResultSet replaceDotsInColumnNames(ResultSet resultSet) throws SQLException {
    ResultSetMetaData metaData = resultSet.getMetaData();
    int columnCount = metaData.getColumnCount();

    if (columnCount > 0) {
        CachedRowSetImpl crs = new CachedRowSetImpl();
        crs.populate(resultSet);/*from w w  w. j a  v  a2 s.  c  o  m*/

        RowSetMetaDataImpl rwsm = new RowSetMetaDataImpl();

        rwsm.setColumnCount(columnCount);

        for (int i = 1; i <= columnCount; i++) {
            String columnName = metaData.getColumnName(i);
            if (columnName.contains(".")) {
                columnName = columnName.replaceAll("\\.", "\\$");
            }
            rwsm.setColumnName(i, columnName);
            rwsm.setColumnLabel(i, metaData.getColumnLabel(i));
            rwsm.setCatalogName(i, metaData.getCatalogName(i));
            rwsm.setColumnType(i, metaData.getColumnType(i));
            rwsm.setColumnTypeName(i, metaData.getColumnTypeName(i));
            rwsm.setSchemaName(i, metaData.getSchemaName(i));
            rwsm.setTableName(i, metaData.getTableName(i));
        }
        crs.setMetaData(rwsm);
        return crs;
    }
    return resultSet;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.sql.ResultSetTableModelFactory.java

/**
 * No longer used./*from   w ww  . j  av a 2  s .  c  om*/
 *
 * @param rsmd
 * @param metaData
 * @param column
 */
@Deprecated
public static void updateMetaData(final ResultSetMetaData rsmd, final DefaultTableMetaData metaData,
        final int column) {
    try {
        if (rsmd.isCurrency(column + 1)) {
            metaData.setColumnAttribute(column, MetaAttributeNames.Numeric.NAMESPACE,
                    MetaAttributeNames.Numeric.CURRENCY, Boolean.TRUE);
        } else {
            metaData.setColumnAttribute(column, MetaAttributeNames.Numeric.NAMESPACE,
                    MetaAttributeNames.Numeric.CURRENCY, Boolean.FALSE);
        }

        if (rsmd.isSigned(column + 1)) {
            metaData.setColumnAttribute(column, MetaAttributeNames.Numeric.NAMESPACE,
                    MetaAttributeNames.Numeric.SIGNED, Boolean.TRUE);
        } else {
            metaData.setColumnAttribute(column, MetaAttributeNames.Numeric.NAMESPACE,
                    MetaAttributeNames.Numeric.SIGNED, Boolean.FALSE);
        }

        final String tableName = rsmd.getTableName(column + 1);
        if (tableName != null) {
            metaData.setColumnAttribute(column, MetaAttributeNames.Database.NAMESPACE,
                    MetaAttributeNames.Database.TABLE, tableName);
        }
        final String schemaName = rsmd.getSchemaName(column + 1);
        if (schemaName != null) {
            metaData.setColumnAttribute(column, MetaAttributeNames.Database.NAMESPACE,
                    MetaAttributeNames.Database.SCHEMA, schemaName);
        }
        final String catalogName = rsmd.getCatalogName(column + 1);
        if (catalogName != null) {
            metaData.setColumnAttribute(column, MetaAttributeNames.Database.NAMESPACE,
                    MetaAttributeNames.Database.CATALOG, catalogName);
        }
        final String label = rsmd.getColumnLabel(column + 1);
        if (label != null) {
            metaData.setColumnAttribute(column, MetaAttributeNames.Formatting.NAMESPACE,
                    MetaAttributeNames.Formatting.LABEL, label);
        }
        final int displaySize = rsmd.getColumnDisplaySize(column + 1);
        metaData.setColumnAttribute(column, MetaAttributeNames.Formatting.NAMESPACE,
                MetaAttributeNames.Formatting.DISPLAY_SIZE, IntegerCache.getInteger(displaySize));

        final int precision = rsmd.getPrecision(column + 1);
        metaData.setColumnAttribute(column, MetaAttributeNames.Numeric.NAMESPACE,
                MetaAttributeNames.Numeric.PRECISION, IntegerCache.getInteger(precision));
        final int scale = rsmd.getScale(column + 1);
        metaData.setColumnAttribute(column, MetaAttributeNames.Numeric.NAMESPACE,
                MetaAttributeNames.Numeric.SCALE, IntegerCache.getInteger(scale));
    } catch (SQLException sqle) {
        // It is non-fatal if the meta-data cannot be read from the result set. Drivers are
        // buggy all the time ..
    }
}

From source file:de.innovationgate.webgate.api.jdbc.custom.JDBCSource.java

/**
 * @param resultSet//from   w ww. j  av  a  2s. co  m
 * @return
 */
private Map extractRowKey(ResultSet resultSet, String tableHint) throws SQLException {
    ResultSetMetaData rsMeta = resultSet.getMetaData();
    TableName tableName = new TableName(rsMeta.getCatalogName(1), rsMeta.getSchemaName(1),
            rsMeta.getTableName(1));
    String completeTableName = tableName.getCompleteName();
    if (completeTableName == null || completeTableName.trim().equals("")) {
        completeTableName = tableHint;
    }

    Map keys = new KeyMap();
    List keyColumns = (List) _tables.get(String.valueOf(completeTableName).toLowerCase());

    // If key columns are not retrievable, just return the empty map as key
    if (keyColumns == null) {
        return keys;
    }

    Iterator keyColumnsIt = keyColumns.iterator();
    while (keyColumnsIt.hasNext()) {
        String keyColumn = (String) keyColumnsIt.next();
        Object keyValue = resultSet.getObject(keyColumn);

        // Since the key columns from KeyMap originate from the _tables list, we should use the column names unmodified
        // keys.put(keyColumn.toLowerCase(), keyValue);
        keys.put(keyColumn, keyValue);
    }

    return keys;

}