简体   繁体   English

Java JDBC获取表

[英]Java JDBC get tables

I have a method which displays tables in a JCombo Box. 我有一种在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));
    }

}

The problem is that I have ONLY 3 tables called: Criminals, Agents, Informants, but the method returns me all kinds of tables like: _GV$SXGG_APPLY_READER, _GV$SXGG_APPLY_SERVER, _GV$SXGG_CAPTURE for example and hundreds of others. 问题是我只有3个表:罪犯,特工,告密者,但该方法返回的表种类繁多,例如:_GV $ SXGG_APPLY_READER,_GV $ SXGG_APPLY_SERVER,_GV $ SXGG_CAPTURE和其他数百个表。 How do I filter them out? 如何过滤掉它们?

I found it. 我找到了。 The proper code is: 正确的代码是:

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

}

Here is some doc suggests using stmt.executeQuery("select object_name from user_objects where object_type = 'TABLE'") like this: 这是一些文档建议使用stmt.executeQuery(“从user_objects中选择object_name,其中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();
  }

This will work for Oracle only though 尽管这仅适用于Oracle

As per the oracle docs getTables() accepts 4 arguments like catalog, schemaPattern,tableNamePattern,types but you are making null so you are getting all the table names 根据oracle 文档, getTables()接受4个参数,例如catalog, schemaPattern,tableNamePattern,types但是您将其catalog, schemaPattern,tableNamePattern,types null,因此将获取所有表名

in your existing code make the 4th arguement this way new String[]{"TABLE"} 在您现有的代码中,用new String[]{"TABLE"}进行第四点辩论

full code will be 完整的代码将是

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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