简体   繁体   中英

View Table Names from all schema in MySql using Java

I wanted to know how to view tables from both schemas (in this example lets say world and world2), through JSF. I can log into my application using a specified schema and view the tables from that schema but I do not know how to log in and view a different schema. (ex. log in as world, view world2 schema table names.)

So I want to ask if there is a way to rewrite tablelist, or maybe repurpose the code to handle sql queries even though the connection is for a specific schema.

Currently I am using:

    public TableList[] getTableList() {
    try {
        String[] TABLE_TYPES = { "TABLE", "VIEW" };
        DatabaseMetaData databaseMetaData;
        String query = "";

        // st = conn.createStatement();
        st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        databaseMetaData = conn.getMetaData();
        rs = databaseMetaData.getTables(null, dbaseBean.getUserName(),
                null, TABLE_TYPES);
        rs.last();
        int count = rs.getRow();
        tableList = new TableList[count];
        rs.beforeFirst();
        int i = 0;
        while (rs.next()) {
            tableList[i] = new TableList(rs.getString("TABLE_NAME"));
            i++;
        }
    } catch (Exception e) {
        e.printStackTrace();
        FacesMessage msg = new FacesMessage("" + " Error Occured");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    setTableList(tableList);
    return tableList;
}

With the following connection:

public boolean connect() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, Object> m = context.getExternalContext().getSessionMap();
    messageBean = (MessageBean) m.get("messageBean");
    dbmsUserBean = (DbmsUserBean) m.get("dbmsUserBean");
    userName = dbmsUserBean.getUserName();
    password = dbmsUserBean.getPassword();

    switch (dbmsUserBean.getDbms().toLowerCase()) {

    case "mysql":
        jdbcDriver = "com.mysql.jdbc.Driver";
        url = "jdbc:mysql://" + dbmsUserBean.getDbmsHost() + ":3306/"
                + dbmsUserBean.getDatabaseSchema();
        break;

    case "db2":
        jdbcDriver = "com.ibm.db2.jcc.DB2Driver";
        url = "jdbc:db2://" + dbmsUserBean.getDbmsHost() + ":50000/"
                + dbmsUserBean.getDatabaseSchema();
        userName = userName.toUpperCase();
        break;

    case "oracle":
        jdbcDriver = "oracle.jdbc.driver.OracleDriver";
        url = "jdbc:oracle:thin:@" + dbmsUserBean.getDbmsHost() + ":1521:"
                + dbmsUserBean.getDatabaseSchema();
        userName = userName.toUpperCase();
        break;

    case "odbc":
    default:
        //
        break;
    } // end switch

    try {
        // register driver
        Class.forName(jdbcDriver);
        // get connection
        connection = DriverManager.getConnection(url, userName, password);
        // get SQL statement object instance
        statement = connection.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        // retrieve DB meta data
        databaseMetaData = connection.getMetaData();

        DbaseBean dbaseBean = new DbaseBean();
        dbaseBean.setConnection(connection);
        dbaseBean.setDatabaseMetaData(databaseMetaData);
        dbaseBean.setJdbcDriver(jdbcDriver);
        dbaseBean.setUserName(userName);
        dbaseBean.setPassword(password);
        dbaseBean.setUrl(url);
        dbaseBean.setResultSet(resultSet);
        m.put("dbaseBean", dbaseBean);

        status = true;
    }

    catch (ClassNotFoundException e) {
        // assumes there is a corresponding printException method
        // PrintException("Connect: Class not found Exception - could not loadJDBC driver");
        status = false;
    }

    catch (SQLException e) {
        // printException(e,"Connect: SQLException information");
        status = false;
    } catch (Exception e) {
        // printException(e,"Connect: Exception information");
        status = false;
    }

    return status;

}

Thank you.

If you are going against Oracle and you have access to the system tables, you can retrieve tables from it with "select user, table_name from system.all_tables where user in ('myuser1', 'myuser2');"

If you want everything, you can go against all_objects where type in 'TABLE', 'VIEW' (and procedure and trigger and...)

If you are going against different databases, you would then have to determine where and what the data dictionaries for each DB are and write code for each one.

DOH - You stated MySQL in the title:

select table_schema, table_name from INFORMATION_SCHEMA.TABLES where table_schema in ('myfirstone', 'mysecondone');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM