1

我有一个在 JCombo Box 中显示表格的方法。

private void getTables() throws SQLException {


    dburl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    connection = DriverManager.getConnection(dburl, "c##lambros", "16111111");
    dbmd = connection.getMetaData();
    rset= dbmd.getTables(null, null, "%", null);

    while (rset.next()) {
      System.out.println(rset.getString(3));
    }

}

问题是我只有 3 个表称为:犯罪分子、特工、线人,但该方法返回给我各种表,例如:_GV$SXGG_APPLY_READER、_GV$SXGG_APPLY_SERVER、_GV$SXGG_CAPTURE 和数百个其他表。我如何过滤掉它们?

4

3 回答 3

2

我找到了。正确的代码是:

private void getTables() throws SQLException {


    dburl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    connection = DriverManager.getConnection(dburl, "c##lambros", "16111111");
    dbmd = connection.getMetaData();
    query = " Select table_name FROM user_tables ";
    stmt = connection.createStatement();
    rset = stmt.executeQuery(query);

    while (rset.next()) {
      System.out.println(rset.getString(1));
    }

}
于 2013-10-16T08:54:44.453 回答
0

这是一些文档建议使用 stmt.executeQuery("select object_name from user_objects where object_type = 'TABLE'") 像这样:

public static void main(String[] args) throws Exception {
    Connection conn = getOracleConnection();
    Statement stmt = null;
    ResultSet rs = null;
    stmt = conn.createStatement();
    //only for Oracle
    rs = stmt.executeQuery("select object_name from user_objects where object_type = 'TABLE'");

    while (rs.next()) {
      String tableName = rs.getString(1);
      System.out.println("tableName=" + tableName);
    }

    stmt.close();
    conn.close();
  }

这仅适用于 Oracle

于 2013-10-16T08:36:29.817 回答
0

根据 oracle docs getTables() 接受 4 个参数,例如catalog, schemaPattern,tableNamePattern,types,但您将其设为 null,因此您将获得所有表名

在您现有的代码中以这种方式进行第四次争论new String[]{"TABLE"}

完整的代码将是

private void getTables() throws SQLException {

String[] types = {"TABLE"};
    dburl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    connection = DriverManager.getConnection(dburl, "c##lambros", "16111111");
    dbmd = connection.getMetaData();
    rset= dbmd.getTables(null, null, "%", types);

    while (rset.next()) {
      System.out.println(rset.getString("TABLE_NAME"));
    }

}
于 2013-10-16T08:37:30.710 回答