Example usage for java.sql DatabaseMetaData getSchemas

List of usage examples for java.sql DatabaseMetaData getSchemas

Introduction

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

Prototype

ResultSet getSchemas() throws SQLException;

Source Link

Document

Retrieves the schema names available in this database.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbName";
    Connection conn = DriverManager.getConnection(URL, "user", "passw");

    DatabaseMetaData dmd = conn.getMetaData();
    ResultSet rs1 = dmd.getSchemas();
    while (rs1.next()) {
        String ss = rs1.getString(1);
        ResultSet rs2 = dmd.getTables(null, ss, "%", null);
        while (rs2.next())
            System.out.println(rs2.getString(3) + " " + rs2.getString(4));
    }// 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 = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet schemas = meta.getSchemas();
    while (schemas.next()) {
        String tableSchema = schemas.getString(1); // "TABLE_SCHEM"
        System.out.println("tableSchema:" + tableSchema);
        String tableCatalog = schemas.getString(2); //"TABLE_CATALOG"
        System.out.println("tableCatalog:" + tableCatalog);
    }//from  w  ww .ja  v a  2  s.co  m

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

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet schemas = meta.getSchemas();
    while (schemas.next()) {
        String tableSchema = schemas.getString(1); // "TABLE_SCHEM"
        String tableCatalog = schemas.getString(2); //"TABLE_CATALOG"
        System.out.println("tableSchema" + tableSchema);
    }//  w w w  .  j av a  2 s .  c o  m

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:databaseName";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String user = "guest";
    String password = "guest";

    try {/*from ww w  .j  a  v  a2 s  .c  o m*/
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);

        // Get the MetaData
        DatabaseMetaData metaData = conn.getMetaData();

        // Get driver information
        System.out.println("Driver Informaion");
        System.out.println(metaData.getDriverName());
        System.out.println(metaData.getDriverVersion());
        // Get schema information
        System.out.println("Schemas");
        ResultSet schemas = metaData.getSchemas();
        while (schemas.next()) {
            System.out.println(schemas.getString(1));
        }
        // Get table information
        System.out.println("Tables");
        ResultSet tables = metaData.getTables("", "", "", null);
        while (tables.next()) {
            System.out.println(tables.getString(3));
        }
        conn.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.example.querybuilder.server.Jdbc.java

public static ResultSet getSchemas(DatabaseMetaData databaseMetaData) {
    try {/*from  w  ww  .  j av a 2  s. c om*/
        return databaseMetaData.getSchemas();
    } catch (SQLException e) {
        throw new SqlRuntimeException(e);
    }
}

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;// www .  j  a  va  2s  .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.jaspersoft.jasperserver.api.engine.common.virtualdatasourcequery.VirtualSQLDataSource.java

public static Set<String> discoverNonEmptySchemas(Connection conn) throws SQLException {
    DatabaseMetaData dbMetaData = conn.getMetaData();
    try {// w w  w . j  ava  2 s  .  c o  m
        Set<String> set = new LinkedHashSet<String>();
        Set<String> schemaList = getResult(dbMetaData.getSchemas(), TABLE_SCHEM);
        String[] types = { "TABLE" };

        for (String schema : schemaList) {
            ResultSet rs2 = null;
            try {

                rs2 = conn.getMetaData().getTables(null, schema, null, types);
                if (rs2.next())
                    set.add(schema);
                else
                    log.debug(schema + " schema contains no table.  Ignore in VDS");

                /***  take too long to check empty tables
                Set<String> tableList = getResult(conn.getMetaData().getTables(null, schema, null, types), TABLE_NAME);
                Set<String> tableWithColsList = getResult(conn.getMetaData().getColumns(null, schema, null, null), TABLE_NAME);
                        
                if (tableList.size() == 0) {
                log.debug(schema + " schema contains no table.  Ignore in VDS");
                continue;
                } else {
                set.add(schema);
                        
                // does all tables contain columns
                boolean doesAllTablesContainCols = true;
                for (String tableName : tableList) {
                    if (!tableWithColsList.contains(tableName)) {
                        log.debug(schema + "." +  tableName + " table contains table with no column.  Ignore in VDS");
                        doesAllTablesContainCols = false;
                        break;
                    }
                }
                if (doesAllTablesContainCols) set.add(schema);
                        
                }
                ****/
            } catch (SQLException ex2) {
                log.debug("Fail to read schema, " + schema + ".  Ignore in VDS");
                ex2.printStackTrace();

            } finally {
                if (rs2 != null)
                    rs2.close();
            }
        }
        return set;
    } catch (SQLException ex) {
        log.error("Cannot get schemas", ex);
        throw ex;
    }
}

From source file:com.manydesigns.portofino.database.platforms.AbstractDatabasePlatform.java

public List<String> getSchemaNames(DatabaseMetaData databaseMetaData) throws SQLException {
    ResultSet rs = databaseMetaData.getSchemas();
    List<String> schemaNames = new ArrayList<String>();
    try {/*www  . ja v a  2 s  .  co m*/
        while (rs.next()) {
            String schemaName = rs.getString(TABLE_SCHEM);
            schemaNames.add(schemaName);
        }
    } finally {
        DbUtils.closeQuietly(rs);
    }
    return schemaNames;
}

From source file:com.manydesigns.portofino.model.database.platforms.AbstractDatabasePlatform.java

public List<String[]> getSchemaNames(DatabaseMetaData databaseMetaData) throws SQLException {
    ResultSet rs = databaseMetaData.getSchemas();
    List<String[]> schemaNames = new ArrayList<String[]>();
    try {/*  ww  w .  j  ava2 s . c o m*/
        while (rs.next()) {
            String schemaName = rs.getString(TABLE_SCHEM);
            String schemaCatalog = rs.getString(getCatalogColumnName());
            schemaNames.add(new String[] { schemaCatalog, schemaName });
        }
    } finally {
        DbUtils.closeQuietly(rs);
    }
    return schemaNames;
}

From source file:de.erdesignerng.dialect.oracle.OracleReverseEngineeringStrategy.java

@Override
public List<SchemaEntry> getSchemaEntries(Connection aConnection) throws SQLException {

    List<SchemaEntry> theList = new ArrayList<>();

    DatabaseMetaData theMetadata = aConnection.getMetaData();
    ResultSet theResult = theMetadata.getSchemas();

    while (theResult.next()) {
        String theSchemaName = theResult.getString("TABLE_SCHEM");
        String theCatalogName = null;

        theList.add(new SchemaEntry(theCatalogName, theSchemaName));
    }//w ww .  j a  v  a 2 s  .c  o m

    return theList;
}