Example usage for java.sql DatabaseMetaData getCatalogs

List of usage examples for java.sql DatabaseMetaData getCatalogs

Introduction

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

Prototype

ResultSet getCatalogs() throws SQLException;

Source Link

Document

Retrieves the catalog names available in this database.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet catalogs = meta.getCatalogs();

    while (catalogs.next()) {
        String catalog = catalogs.getString(1); //"TABLE_CATALOG"
        System.out.println("catalog: " + catalog);
    }// w w w .j a v  a 2  s .c  om

    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')");

    ResultSet catalogs = null;//from   ww  w  .ja v  a 2 s  .  c o  m
    DatabaseMetaData meta = conn.getMetaData();
    catalogs = meta.getCatalogs();

    while (catalogs.next()) {
        String catalog = catalogs.getString(1); //"TABLE_CATALOG"
        System.out.println("catalog: " + catalog);
    }

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();

    DatabaseMetaData mtdt = conn.getMetaData();

    // list catalogs managed by this dbms
    System.out.println(mtdt.getCatalogTerm());

    ResultSet rs = mtdt.getCatalogs();

    ResultSetMetaData rsmd = rs.getMetaData();
    int numCols = rsmd.getColumnCount();
    for (int i = 1; i <= numCols; i++) {
        if (i > 1)
            System.out.print(", ");
        System.out.print(rsmd.getColumnLabel(i));
    }/*from w w  w .  ja v a  2 s  .com*/
    System.out.println("");
    while (rs.next()) {
        for (int i = 1; i <= numCols; i++) {
            if (i > 1)
                System.out.print(", ");
            System.out.print(rs.getString(i));
        }
        System.out.println("");
    }
    conn.close();
}

From source file:de.griffel.confluence.plugins.plantuml.DatasourceHelper.java

private static List<String> getCatalogs(DatabaseMetaData dbmd) throws SQLException {
    final List<String> catalogNames = new LinkedList<String>();
    final ResultSet rs = dbmd.getCatalogs();
    while (rs.next()) {
        catalogNames.add(rs.getString(1));
    }/*from  w  w w. j  a v a 2s. c  o m*/
    rs.close();
    return catalogNames;
}

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;/*w w w  . j a  va2 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.manydesigns.portofino.database.platforms.GoogleCloudSQLDatabasePlatform.java

public List<String> getSchemaNames(DatabaseMetaData databaseMetaData) throws SQLException {
    ResultSet rs = databaseMetaData.getCatalogs();
    List<String> schemaNames = new ArrayList<String>();
    try {/*from www  .  j av  a  2  s . c om*/
        while (rs.next()) {
            String schemaName = rs.getString(TABLE_CAT);
            schemaNames.add(schemaName);
        }
    } finally {
        DbUtils.closeQuietly(rs);
    }
    return schemaNames;
}

From source file:DatabaseBrowser.java

protected void populateCatalogBox() {
    try {//from  w  ww.ja  va2s  . c o m
        DatabaseMetaData dmd = connection.getMetaData();
        ResultSet rset = dmd.getCatalogs();
        Vector values = new Vector();
        while (rset.next()) {
            values.addElement(rset.getString(1));
        }
        rset.close();
        catalogBox.setModel(new DefaultComboBoxModel(values));
        catalogBox.setSelectedItem(connection.getCatalog());
        catalogBox.setEnabled(values.size() > 0);
    } catch (Exception e) {
        catalogBox.setEnabled(false);
    }
}

From source file:kr.co.bitnine.octopus.frame.SessionServerTest.java

private boolean existDataSource(DatabaseMetaData metaData, String name) throws SQLException {
    ResultSet rs = metaData.getCatalogs();
    while (rs.next()) {
        String dsName = rs.getString("TABLE_CAT");
        System.out.println(" *** " + dsName);
        if (dsName.equals(name))
            return true;
    }/*from   ww w .  j a v a 2 s .c o m*/
    return false;
}

From source file:kr.co.bitnine.octopus.frame.SessionServerTest.java

@Test
public void testShow() throws Exception {
    Connection conn = getConnection("octopus", "bitnine");

    System.out.println("* Transaction isolation level");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SHOW TRANSACTION ISOLATION LEVEL");
    while (rs.next())
        System.out.println("  " + rs.getString("transaction_isolation"));
    rs.close();//  w  w  w. j  a va  2s. co m

    System.out.println("* DataSources");
    DatabaseMetaData metaData = conn.getMetaData();
    rs = metaData.getCatalogs();
    while (rs.next()) {
        System.out.println("  " + rs.getString("TABLE_CAT") + ", " + rs.getString("REMARKS"));
    }
    rs.close();

    System.out.println("* Schemas");
    rs = metaData.getSchemas(dataMemDb.name, "%DEFAULT");
    while (rs.next()) {
        System.out.println("  " + rs.getString("TABLE_SCHEM") + ", " + rs.getString("TABLE_CATALOG") + ", "
                + rs.getString("REMARKS") + ", " + rs.getString("TABLE_CAT_REMARKS"));
    }
    rs.close();

    System.out.println("* Tables");
    rs = metaData.getTables(dataMemDb.name, "%DEFAULT", "employee", null);
    while (rs.next()) {
        System.out.println("  " + rs.getString("TABLE_CAT") + ", " + rs.getString("TABLE_SCHEM") + ", "
                + rs.getString("TABLE_NAME") + ", " + rs.getString("REMARKS") + ", "
                + rs.getString("TABLE_CAT_REMARKS") + ", " + rs.getString("TABLE_SCHEM_REMARKS"));
    }
    rs.close();

    System.out.println("* Columns");
    rs = metaData.getColumns(dataMemDb.name, "%DEFAULT", "employee", "%");
    while (rs.next()) {
        System.out.println("  " + rs.getString("TABLE_CAT") + ", " + rs.getString("TABLE_SCHEM") + ", "
                + rs.getString("TABLE_NAME") + ", " + rs.getString("COLUMN_NAME") + ", "
                + rs.getString("REMARKS") + ", " + rs.getString("TABLE_CAT_REMARKS") + ", "
                + rs.getString("TABLE_SCHEM_REMARKS") + ", " + rs.getString("TABLE_NAME_REMARKS"));
    }
    rs.close();

    System.out.println("* Users");

    rs = stmt.executeQuery("SHOW ALL USERS");
    while (rs.next()) {
        System.out.println("  " + rs.getString("USER_NAME") + ", " + rs.getString("REMARKS"));
    }
    rs.close();

    stmt.execute("CREATE USER \"jsyang\" IDENTIFIED BY '0009'");
    stmt.execute("GRANT ALL ON \"" + dataMemDb.name + "\".\"__DEFAULT\" TO \"jsyang\"");
    rs = stmt.executeQuery("SHOW OBJECT PRIVILEGES FOR \"jsyang\"");
    while (rs.next()) {
        System.out.println("  " + rs.getString("TABLE_CAT") + ", " + rs.getString("TABLE_SCHEM") + ", "
                + rs.getString("PRIVILEGE"));
    }
    rs.close();
    stmt.execute("DROP USER \"jsyang\"");

    stmt.close();

    conn.close();
}

From source file:net.certifi.audittablegen.AuditTableGen.java

/**
 * Examines the DataSource metadata for information pertaining to the
 * driver, catalog, schema and the presence of audit table configuration
 * data.//from   w w  w .ja  v a  2 s . c o m
 * 
 * @return String containing datasource information.
 * @throws SQLException 
 */
String getDataSourceInfo() throws SQLException {

    Connection conn = dataSource.getConnection();
    DatabaseMetaData dmd = conn.getMetaData();
    StringBuilder s = new StringBuilder();

    s.append("Driver Name: ").append(dmd.getDriverName()).append("Driver Version: ")
            .append(dmd.getDriverVersion()).append(System.lineSeparator()).append("CatalogSeperator: ")
            .append(dmd.getCatalogSeparator()).append(System.lineSeparator()).append("CatalogTerm: ")
            .append(dmd.getCatalogTerm()).append(System.lineSeparator()).append("SchemaTerm: ")
            .append(dmd.getSchemaTerm()).append(System.lineSeparator()).append("Catalogs: ");

    ResultSet rs = dmd.getCatalogs();
    while (rs.next()) {
        s.append(rs.getString("TABLE_CAT")).append(",");
        logger.debug("Catalog: {}", rs.getString("TABLE_CAT"));
    }
    rs.close();
    s.append(System.lineSeparator());

    s.append("Schemas: ");
    rs = dmd.getSchemas();
    while (rs.next()) {
        logger.debug("Schema: {}", rs.getString("TABLE_SCHEM"));
        s.append("{catalog}:").append(rs.getString("TABLE_CATALOG")).append(" {schema}:")
                .append(rs.getString("TABLE_SCHEM")).append(",");
    }
    rs.close();
    s.append(System.lineSeparator()).append("Target Catalog: ").append(catalog).append(System.lineSeparator())
            .append("Target Schema: ").append(schema).append(System.lineSeparator());

    //       if (dmr.hasAuditConfigTable()){
    //           s.append("Has auditConfigSource table").append(System.lineSeparator());
    //       }

    conn.close();

    return s.toString();

}