Example usage for java.sql DatabaseMetaData getColumns

List of usage examples for java.sql DatabaseMetaData getColumns

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getColumns.

Prototype

ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
        throws SQLException;

Source Link

Document

Retrieves a description of table columns available in the specified catalog.

Usage

From source file:com.splicemachine.derby.impl.sql.execute.operations.CallStatementOperationIT.java

@Test
public void testConnectionMetadata() throws Exception {
    DatabaseMetaData dmd = methodWatcher.getOrCreateConnection().getMetaData();
    ResultSet rs = dmd.getColumns(null, null, "CATEGORY", null);
    while (rs.next()) { // TODO No Test
    }/*from ww w  . j a va2s  .  c  o m*/
    DbUtils.closeQuietly(rs);
}

From source file:com.oracle2hsqldb.dialect.GenericDialect.java

public Iterator getColumns(final DataSource dataSource, final String schemaName) throws SQLException {
    final List result = new LinkedList();
    MetaDataJdbcTemplate template = new MetaDataJdbcTemplate(dataSource) {
        protected ResultSet getResults(DatabaseMetaData metaData) throws SQLException {
            return metaData.getColumns(null, schemaName, null, null);
        }/*w w  w  . j ava2  s.  c om*/
    };
    template.query(new RowCallbackHandler() {
        public void processRow(ResultSet columns) throws SQLException {
            //retrieve values ahead of time, otherwise you get a stream closed error from Oracle
            String columnName = columns.getString("COLUMN_NAME");
            int dataType = columns.getInt("DATA_TYPE");
            String tableName = columns.getString("TABLE_NAME");
            int columnSize = columns.getInt("COLUMN_SIZE");
            int decimalDigits = columns.getInt("DECIMAL_DIGITS");
            boolean isNullable = columns.getBoolean("NULLABLE");
            String columnDef = columns.getString("COLUMN_DEF");
            result.add(new Column.Spec(tableName, new Column(columnName, dataType, columnSize, decimalDigits,
                    isNullable, parseDefaultValue(columnDef, dataType))));
        }
    });
    return result.iterator();
}

From source file:JDBCUtil.java

/**
 * Checks database metadata to see if a column exists in a table.  This method
 * is sensitive to the case of both the provided table name and column name.
 *
 * @param dbMetaData the database metadata to be used to look up this column
 * @param tableName the case sensitive table name
 * @param columnName the case sensitive column name
 *
 * @throws SQLException if an exception is encountered while accessing the database
 *///from  w  w w. j av  a2s  . c  o  m
public boolean columnExistsCaseSensitive(DatabaseMetaData dbMetaData, String tableName, String columnName)
        throws SQLException {
    ResultSet rsTables = dbMetaData.getColumns(null, null, tableName, columnName);
    try {
        boolean found = rsTables.next();
        return found;
    } finally {
        closeJDBCResultSet(rsTables);
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.db.H2TriggerDescendants.java

public void init(Connection conn, String schemaName, String triggerName, String tableName, boolean before,
        int type) throws SQLException {

    String HIER_TABLE = "HIERARCHY"; // TODO make parameter
    String SCHEMA = "PUBLIC";
    String MAIN_ID = "ID";
    String PARENT_ID = "PARENTID";
    String IS_PROPERTY = "ISPROPERTY";

    // find id and parentid column indices in result sets
    DatabaseMetaData meta = conn.getMetaData();
    ResultSet rs = meta.getColumns(null, SCHEMA, HIER_TABLE, null);
    while (rs.next()) {
        String name = rs.getString("COLUMN_NAME").toUpperCase();
        int index = rs.getInt("ORDINAL_POSITION") - 1;
        if (name.equals(MAIN_ID)) {
            idIndex = index;/*from   w w  w.  j a va 2  s  .com*/
        } else if (name.equals(PARENT_ID)) {
            parentIdIndex = index;
        } else if (name.equals(IS_PROPERTY)) {
            isPropIndex = index;
        }
    }
    rs.close();
}

From source file:com.splicemachine.derby.impl.sql.execute.operations.CallStatementOperationIT.java

@Test
public void testCallSqlColumns() throws Exception {
    int count = 0;
    DatabaseMetaData dmd = methodWatcher.getOrCreateConnection().getMetaData();
    ResultSet rs = dmd.getColumns(null, "SYS", "SYSSCHEMAS", null);
    Set<String> correctNames = Sets.newHashSet("SCHEMAID", "SCHEMANAME", "AUTHORIZATIONID");
    while (rs.next()) {
        String sIdCol = rs.getString(4);
        int colType = rs.getInt(5);
        Assert.assertTrue("No colType returned!", !rs.wasNull());
        int colNum = rs.getInt(17);
        Assert.assertTrue("No colNum returned!", !rs.wasNull());

        Assert.assertTrue("Incorrect column name returned!", correctNames.contains(sIdCol.toUpperCase()));
        count++;//w w  w  .ja v a2  s . com
    }
    Assert.assertEquals("incorrect rows returned!", 3, count);
    DbUtils.closeQuietly(rs);
}

From source file:com.idega.block.article.data.CategoryBugRemover.java

public boolean isBadTableExist(String tableName) throws SQLException {
    Connection conn = null;/* w  w  w  .j a  va  2s  .c o  m*/
    try {
        conn = SimpleQuerier.getConnection();
        DatabaseMetaData meta = conn.getMetaData();
        ResultSet columnsInfo = meta.getColumns(null, null, tableName, null);

        if (columnsInfo.next()) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }

    } finally {
        if (conn != null)
            conn.close();
    }
}

From source file:chh.utils.db.source.common.JdbcClient.java

public List<Column> getColumnSchema(String tableName) {
    Connection connection = null;
    List<Column> columns = new ArrayList<Column>();
    try {//w  w  w. j ava 2  s .  co  m
        connection = connectionProvider.getConnection();
        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet resultSet = metaData.getColumns(null, null, tableName, null);
        while (resultSet.next()) {
            columns.add(new Column(resultSet.getString("COLUMN_NAME"), resultSet.getInt("DATA_TYPE")));
        }
        return columns;
    } catch (SQLException e) {
        throw new RuntimeException("Failed to get schema for table " + tableName, e);
    } finally {
        closeConnection(connection);
    }
}

From source file:com.idega.block.article.data.CategoryBugRemover.java

public boolean isBadColunmExist(String column) throws SQLException {
    Connection conn = null;//from  ww  w. java  2  s.com
    try {
        conn = SimpleQuerier.getConnection();
        DatabaseMetaData meta = conn.getMetaData();
        ResultSet columnsInfo = meta.getColumns(null, null, "IC_CATEGORY", null);
        while (columnsInfo.next()) {
            String columnName = columnsInfo.getString("COLUMN_NAME");
            if (columnName.equalsIgnoreCase(column)) {
                return Boolean.TRUE;
            }
        }
    } finally {
        if (conn != null)
            conn.close();
    }

    return Boolean.FALSE;
}

From source file:org.nuxeo.ecm.directory.sql.SQLHelper.java

private Set<String> getPhysicalColumns() throws SQLException {
    ResultSet rs = null;//from   w  w w.ja  v a2  s. co m
    Set<String> columnNames = new HashSet<>();
    try {
        // fetch the database columns definitions
        DatabaseMetaData metadata = connection.getMetaData();
        rs = metadata.getColumns(null, "%", tableName, "%");

        while (rs.next()) {
            columnNames.add(rs.getString("COLUMN_NAME"));
        }
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                log.warn("Error while trying to close result set", e);
            }
        }
    }
    return columnNames;
}

From source file:org.eclipse.ecr.core.storage.sql.extensions.H2Fulltext.java

private static int getPrimaryKeyType(DatabaseMetaData meta, String schema, String table) throws SQLException {
    // find primary key name
    String primaryKeyName = null;
    ResultSet rs = meta.getPrimaryKeys(null, schema, table);
    while (rs.next()) {
        if (primaryKeyName != null) {
            throw new SQLException("Can only index primary keys on one column for " + schema + '.' + table);
        }//from  ww w .  j av  a2  s  . c o m
        primaryKeyName = rs.getString("COLUMN_NAME");
    }
    if (primaryKeyName == null) {
        throw new SQLException("No primary key for " + schema + '.' + table);
    }
    rs.close();

    // find primary key type
    rs = meta.getColumns(null, schema, table, primaryKeyName);
    if (!rs.next()) {
        throw new SQLException("Could not find primary key");
    }
    int primaryKeyType = rs.getInt("DATA_TYPE");
    rs.close();

    return primaryKeyType;
}