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:application.gen.gen.java

public static List<Column> getColumns(Connection con, String catalog, String schema, String tableName)
        throws SQLException {
    List<Column> list = new ArrayList<Column>();
    ResultSet rs = con.getMetaData().getColumns(catalog, schema, tableName, "%");
    while (rs != null && rs.next()) {
        // System.out.println(rs.getString("COLUMN_NAME")+"
        // "+rs.getString("TYPE_NAME")+" "+rs.getString("COLUMN_SIZE")+"
        // "+rs.getString("NULLABLE"));
        Column column = new Column(rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"),
                rs.getString("COLUMN_SIZE"), rs.getString("NULLABLE"));
        list.add(column);/*from  w  ww .  ja  v  a2  s.c om*/
    }

    return list;
}

From source file:com.oltpbenchmark.catalog.Catalog.java

public static void setSeparator(Connection c) throws SQLException {
    Catalog.separator = c.getMetaData().getIdentifierQuoteString();
}

From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java

public static void cursorHoldabilitySupport(Connection conn) throws SQLException {
    DatabaseMetaData dbMetaData = conn.getMetaData();
    System.out.println("ResultSet.HOLD_CURSORS_OVER_COMMIT = " + ResultSet.HOLD_CURSORS_OVER_COMMIT);
    System.out.println("ResultSet.CLOSE_CURSORS_AT_COMMIT = " + ResultSet.CLOSE_CURSORS_AT_COMMIT);
    System.out.println("Default cursor holdability: " + dbMetaData.getResultSetHoldability());
    System.out.println("Supports HOLD_CURSORS_OVER_COMMIT? "
            + dbMetaData.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT));
    System.out.println("Supports CLOSE_CURSORS_AT_COMMIT? "
            + dbMetaData.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT));
}

From source file:com.mirth.connect.server.util.DatabaseUtil.java

/**
 * Tell whether or not the given table exists in the database
 *///  w ww. j  a  v a  2s  .c  o m
public static boolean tableExists(Connection connection, String tableName) {
    ResultSet resultSet = null;

    try {
        DatabaseMetaData metaData = connection.getMetaData();
        resultSet = metaData.getTables(null, null, tableName.toUpperCase(), new String[] { "TABLE" });

        if (resultSet.next()) {
            return true;
        }

        resultSet = metaData.getTables(null, null, tableName.toLowerCase(), new String[] { "TABLE" });
        return resultSet.next();
    } catch (SQLException e) {
        throw new DonkeyDaoException(e);
    } finally {
        DbUtils.closeQuietly(resultSet);
    }
}

From source file:com.jaspersoft.jasperserver.api.engine.common.virtualdatasourcequery.VirtualSQLDataSource.java

public static Set discoverCatalogs(Connection conn) throws SQLException {
    DatabaseMetaData dbMetaData = conn.getMetaData();
    ResultSet rs = null;//from  w  ww  . ja  va 2  s  .c  o  m
    try {
        Set set = new LinkedHashSet();
        rs = dbMetaData.getCatalogs();
        while (rs.next()) {
            String catalog = rs.getString(TABLE_CAT);
            set.add(catalog);
        }
        return set;
    } catch (SQLException ex) {
        log.error("Cannot get catalogs", ex);
        throw ex;
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:com.jaspersoft.jasperserver.api.engine.common.virtualdatasourcequery.VirtualSQLDataSource.java

public static Set<String> discoverSchemas(Connection conn) throws SQLException {
    DatabaseMetaData dbMetaData = conn.getMetaData();
    ResultSet rs = null;/*from w w  w  . j av  a 2 s. c om*/
    try {
        Set<String> set = new LinkedHashSet<String>();
        rs = dbMetaData.getSchemas();
        while (rs.next()) {
            String schema = rs.getString(TABLE_SCHEM);
            if (schema != null)
                set.add(schema);
        }
        return set;
    } catch (SQLException ex) {
        log.error("Cannot get schemas", ex);
        throw ex;
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:com.predic8.membrane.core.interceptor.statistics.util.JDBCUtil.java

public static boolean tableExists(Connection con, String table) throws SQLException {
    DatabaseMetaData meta = con.getMetaData();
    ResultSet rs = meta.getTables("", null, table.toUpperCase(), null);
    return rs.next();
}

From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java

public static void rowIdLifetime(Connection conn) throws SQLException {
    DatabaseMetaData dbMetaData = conn.getMetaData();
    RowIdLifetime lifetime = dbMetaData.getRowIdLifetime();
    switch (lifetime) {
    case ROWID_UNSUPPORTED:
        System.out.println("ROWID type not supported");
        break;//from  w w  w . j ava  2s. c  om
    case ROWID_VALID_FOREVER:
        System.out.println("ROWID has unlimited lifetime");
        break;
    case ROWID_VALID_OTHER:
        System.out.println("ROWID has indeterminate lifetime");
        break;
    case ROWID_VALID_SESSION:
        System.out.println("ROWID type has lifetime that is valid for at least the containing session");
        break;
    case ROWID_VALID_TRANSACTION:
        System.out.println("ROWID type has lifetime that is valid for at least the containing transaction");
    }
}

From source file:com.netradius.hibernate.support.HibernateUtil.java

private static List<String[]> getTables() {
    final List<String[]> rows = new ArrayList<String[]>();
    rows.add(new String[] { "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "TABLE_TYPE", "REMARKS", "TYPE_CAT",
            "TYPE_SCHEM", "TYPE_NAME", "SELF_REFERENCING_COL_NAME", "REF_GENERATION " });
    Connection con = null;
    ResultSet rs = null;/*w w w.j a  v  a 2s.c  o m*/
    try {
        con = getConnection();
        final DatabaseMetaData md = con.getMetaData();
        rs = md.getTables(null, null, "%", null);
        while (rs.next()) {
            List<String> s = new ArrayList<String>(10);
            for (int i = 10; i > 0; i--)
                s.add(rs.getString(i));
            rows.add(s.toArray(new String[10]));
        }
    } catch (SQLException x) {
        log.error("Error listing tables: " + x.getMessage(), x);
    } finally {
        close(con, null, rs);
    }
    return rows;
}

From source file:com.netradius.hibernate.support.HibernateUtil.java

private static List<String[]> getColumns(final String table) {
    final List<String[]> rows = new ArrayList<String[]>();
    rows.add(new String[] { "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME",
            "COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS", "NUM_PREC_RADIX", "NULLABLE", "REMARKS",
            "COLUMN_DEF", "SQL_DATA_TYPE", "SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH", "ORDINAL_POSITION",
            "IS_NULLABLE", "SCOPE_CATLOG", "SCOPE_SCHEMA", "SCOPE_TABLE", "SOURCE_DATA_TYPE",
            "IS_AUTOINCREMENT" });
    Connection con = null;
    ResultSet rs = null;/*from  w w w .j a va 2s.co m*/
    try {
        con = getConnection();
        final DatabaseMetaData md = con.getMetaData();
        rs = md.getColumns(null, null, table, "%");
        while (rs.next()) {
            List<String> s = new ArrayList<String>(23);
            for (int i = 23; i > 0; i--)
                s.add(rs.getString(i));
            rows.add(s.toArray(new String[23]));
        }
    } catch (SQLException x) {
        log.error("Error describing table: " + x.getMessage(), x);
    } finally {
        close(con, null, rs);
    }
    return rows;
}