Example usage for java.sql Connection getMetaData

List of usage examples for java.sql Connection getMetaData

Introduction

In this page you can find the example usage for java.sql Connection getMetaData.

Prototype

DatabaseMetaData getMetaData() throws SQLException;

Source Link

Document

Retrieves a DatabaseMetaData object that contains metadata about the database to which this Connection object represents a connection.

Usage

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

public boolean isBadColunmsExist() throws SQLException {
    Boolean isCategoriesImported = getApplication().getSettings()
            .getBoolean(ArticlesImporter.CATEGORIES_IMPORTED_APP_PROP, false);
    Boolean isArticlesImported = getApplication().getSettings()
            .getBoolean(ArticlesImporter.ARTICLES_IMPORTED_APP_PROP, false);

    if (isArticlesImported || isCategoriesImported) {
        return Boolean.TRUE;
    }//w  w w  .  ja  va  2  s . c o m

    Connection conn = null;
    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("ID")) {
                return Boolean.TRUE;
            }

            if (columnName.equalsIgnoreCase("CATEGORY")) {
                return Boolean.TRUE;
            }

            if (columnName.equalsIgnoreCase("HASHCODE")) {
                return Boolean.TRUE;
            }
        }
    } finally {
        if (conn != null)
            conn.close();
    }

    return Boolean.FALSE;
}

From source file:com.sap.dirigible.runtime.scripting.DbUtils.java

public String createLimitAndOffset(int limit, int offset) throws SQLException {
    Connection connection = null;
    try {//from  w w w  .  j  a v a 2 s  . co  m
        connection = dataSource.getConnection();
        String productName = connection.getMetaData().getDatabaseProductName();
        IDialectSpecifier dialectSpecifier = DBUtils.getDialectSpecifier(productName);
        return dialectSpecifier.createLimitAndOffset(limit, offset);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:com.sap.dirigible.runtime.scripting.DbUtils.java

public String createTopAndStart(int limit, int offset) throws SQLException {
    Connection connection = null;
    try {// www. jav a 2 s.c o  m
        connection = dataSource.getConnection();
        String productName = connection.getMetaData().getDatabaseProductName();
        IDialectSpecifier dialectSpecifier = DBUtils.getDialectSpecifier(productName);
        return dialectSpecifier.createTopAndStart(limit, offset);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:com.dbsvg.models.JdbcMainDAO.java

/**
 * Grabs all the Columns and fills them with the information from the JDBC.
 * Also detects Primary keys//w  w  w .  j  av a2s.  c o  m
 * 
 * @param table
 * @param conn
 * @return
 * @throws java.lang.Exception
 */
private Table populateTable(Table table, Connection conn) throws Exception {

    int maxWidth = 0;

    maxWidth = (int) (table.getName().length() * 1.5);

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet rs = meta.getColumns(null, null, table.getName(), null);

    while (rs.next()) {
        String columnName = rs.getString("COLUMN_NAME");
        if (columnName.length() > maxWidth)
            maxWidth = columnName.length();
        Column c = new ColumnObject(columnName);
        table.getColumns().put(columnName, c);
        c.setTable(table);
        populateColumn(c, rs);
    }

    table.setWidth(CHAR_WIDTH * maxWidth + PAD_WIDTH);
    table.setHeight(CHAR_HEIGHT * table.getColumns().size() + PAD_HEIGHT);

    try {
        rs = meta.getPrimaryKeys(null, null, table.getName());

        while (rs.next()) {
            String columnName = rs.getString("COLUMN_NAME");
            PrimaryKey pk = table.getColumns().get(columnName).transformToPK();
            table.getColumns().put(columnName, pk);
            table.getPrimaryKeys().put(columnName, pk);
        }
    } catch (Exception e) {
        LOG.error(table.getName() + " Has Primary Key Issues.", e);
    }
    rs.close();

    return table;
}

From source file:binky.reportrunner.service.impl.DatasourceServiceImpl.java

@Override
public String testDataSource(RunnerDataSource runnerDs) {

    try {/*ww w  .  ja  v  a 2s .  c  o m*/
        //fix for issue 105 - when not editing password but just testing
        if (runnerDs.getPassword() == null || runnerDs.getPassword().trim().isEmpty()) {
            // see if ds already exists but we are hiding the password
            RunnerDataSource pwget = this.dataSourceDao.get(runnerDs.getDataSourceName());
            if (pwget != null) {
                logger.debug("supplied password was blank - using stored password (if any)");
                runnerDs.setPassword(pwget.getPassword());
            }
        } else {
            EncryptionUtil enc = new EncryptionUtil();
            runnerDs.setPassword(enc.encrpyt(this.secureKey, runnerDs.getPassword()));
        }

        DataSource ds = this.getDs(runnerDs);
        Connection conn = ds.getConnection();
        DatabaseMetaData meta = conn.getMetaData();
        String information = meta.getDatabaseProductName() + ", " + meta.getDatabaseProductVersion();
        conn.close();
        if (ds instanceof BasicDataSource) {
            ((BasicDataSource) ds).close();
        }
        return information;
    } catch (Exception e) {
        if (e instanceof NullPointerException) {
            logger.fatal(e.getMessage(), e);
        }
        logger.debug(e.getMessage());
        return "ERROR - " + e.getClass().getSimpleName() + ": " + e.getMessage();
    }
}

From source file:io.jawg.osmcontributor.utils.core.database.DatabaseUpgradeTest.java

private Set<String> extractContent(String newUrl) throws SQLException {
    Connection newConnection = DriverManager.getConnection(newUrl);

    Set<String> contents = new TreeSet<String>();
    ResultSet newTables = null;//from   w  w  w.  jav a  2  s . c om
    ResultSet newColumns = null;
    try {
        newTables = newConnection.getMetaData().getTables(null, null, null, null);

        while (newTables.next()) {
            contents.add(newTables.getString("TABLE_TYPE") + " " + newTables.getString("TABLE_NAME"));

            newColumns = newConnection.getMetaData().getColumns(null, null, newTables.getString("TABLE_NAME"),
                    null);
            while (newColumns.next()) {
                contents.add("TABLE " + newColumns.getString("TABLE_NAME") + " COLUMN "
                        + newColumns.getString("COLUMN_NAME") + " " + newColumns.getString("TYPE_NAME")
                        + " NULLABLE=" + newColumns.getString("IS_NULLABLE"));
                ResultSet indexes = newConnection.getMetaData().getIndexInfo(null, null,
                        newColumns.getString("TABLE_NAME"), true, false);
                while (indexes.next()) {
                    contents.add("TABLE " + indexes.getString("TABLE_NAME") + " INDEX "
                            + indexes.getString("INDEX_NAME") + " " + indexes.getString("ORDINAL_POSITION")
                            + " " + indexes.getString("COLUMN_NAME") + " NON_UNIQUE="
                            + indexes.getString("NON_UNIQUE"));
                }
            }
        }
        return contents;
    } finally {
        if (newTables != null) {
            try {
                newTables.close();
            } catch (SQLException ignored) {
            }
        }
        if (newColumns != null) {
            try {
                newColumns.close();
            } catch (SQLException ignored) {
            }
        }
    }
}

From source file:com.splicemachine.derby.test.framework.SpliceFunctionWatcher.java

@Override
protected void starting(Description description) {
    LOG.trace("Starting");
    Connection connection = null;
    Statement statement = null;//from  w  w  w.  ja va 2 s.  c o  m
    ResultSet rs = null;
    try {
        connection = (userName == null) ? SpliceNetConnection.getConnection()
                : SpliceNetConnection.getConnectionAs(userName, password);
        rs = connection.getMetaData().getTables(null, schemaName, functionName, null);
        if (rs.next()) {
            executeDrop(schemaName, functionName);
        }
        connection.commit();
        statement = connection.createStatement();
        statement.execute(CREATE_FUNCTION + schemaName + "." + functionName + " " + createString);
        connection.commit();
    } catch (Exception e) {
        LOG.error("Create function statement is invalid ");
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
    super.starting(description);
}

From source file:com.redhat.victims.database.VictimsSQL.java

protected boolean isSetUp(Connection connection) throws SQLException {
    boolean result = false;
    DatabaseMetaData dbm = connection.getMetaData();
    ResultSet rs = dbm.getTables(null, null, "RECORDS", null);
    result = rs.next();//from   w w w.ja  v  a2  s.  c  om
    rs.close();
    return result;
}

From source file:com.uber.hoodie.hive.client.HoodieHiveClient.java

/**
 * Check if table exists/*from  w w w.  ja  v  a 2 s.  co  m*/
 *
 * @param metadata
 * @return
 */
public boolean checkTableExists(HoodieDatasetReference metadata) {
    ResultSet resultSet = null;
    try {
        Connection conn = getConnection();
        resultSet = conn.getMetaData().getTables(null, metadata.getDatabaseName(), metadata.getTableName(),
                null);
        return resultSet.next();
    } catch (SQLException e) {
        throw new HoodieHiveDatasetException("Failed to check if table exists " + metadata, e);
    } finally {
        closeQuietly(resultSet, null);
    }
}