使用Java

时间:2016-04-05 22:43:00

标签: java sql jdbc

我想知道如何通过JSF查看两个模式中的表(在本例中可以说是world和world2)。我可以使用指定的模式登录我的应用程序并查看该模式中的表,但我不知道如何登录并查看不同的模式。 (例如,以world身份登录,查看world2架构表名称。)

所以我想问一下是否有办法重写tablelist,或者重新调整代码来处理sql查询,即使连接是针对特定模式的。

目前我正在使用:

    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;
}

通过以下连接:

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;

}

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您要反对Oracle并且您可以访问系统表,则可以使用“select user,table_name from system.all_tables where user('myuser1','myuser2');”来检索表中的表。

如果你想要一切,你可以反对all_objects,其中输入'TABLE','VIEW'(和过程和触发器......)

如果您要针对不同的数据库,则必须确定每个数据库的数据字典的位置和内容,并为每个数据库编写代码。

DOH - 您在标题中声明了MySQL:

从INFORMATION_SCHEMA.TABLES中选择table_schema,table_name,其中table_schema in('myfirstone','mysecondone');