-4

I am a new Java programmer. I am confused with using an array with the getTable method. This code only work if I either leave the array empty like --String [] types={}; or with a {"TABLE"}; inside... I tried accessing the array besides the RS and there is no table names stored in it.... Why do we even use this Array and pass its name in getTable method bcz to see table names we print the rs .... I dont understand the purpose of this array with {"TABLE"} written inside and also what is this String[] types = {"TABLE", "VIEW"}; for... why wont this work if I wrote like String [] types = {"hey", "you"};

public void getDatabaseMetaData(){
    try {

        DatabaseMetaData md = conn.getMetaData();
        String[] types = {"TABLE"};
        //OR using  String[] types = {"TABLE", "VIEW"};
        Result-set rs = md.get-tables(null, null, "%", types);
        while (rs.next()) {
            System.out.println(rs.getString("TABLE_NAME"));
        }
    } 
        catch (SQLException e) {
        e.printStackTrace();
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
HKM
  • 1
  • 3
  • 1
    This is clearly not the code you're actually running - the hyphens in type names and variables are not allowed in Java. Also, most importantly: did you consult the documentation of the APIs you're attempting to use? It's publicly available on the Internet. – kryger Mar 05 '18 at 19:16

1 Answers1

1

Java has extensive documentation for all its classes, methods, etc. I strongly suggest you read it before asking questions on Stack Overflow, as it will usually answer your question.

The documentation of DatabaseMetaData.getTables​(String catalog, String schemaPattern, String tableNamePattern, String[] types) says:

Parameters:
[..]
  types - a list of table types, which must be from the list of table types returned from getTableTypes(),to include; null returns all types

In other words, the types column is an array with the types of tables to include. If we look at DatabaseMetaData.getTableTypes(), it says:

Retrieves the table types available in this database. The results are ordered by table type.

The table type is:

  1. TABLE_TYPE String => table type. Typical types are "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".

In other words, when you use getTables(null, null, "%", types), the value of types indicates which types of tables you want returned.

For example, with null all tables are returned, with { "TABLE" } only 'real' user tables (but not system tables), with { "TABLE", "VIEW" }, you'll get all 'real' user tables and user views (drivers will usually group system views under type "SYSTEM TABLE"), etc.

The reason it doesn't work when you specify { "hey", "you" } is because those aren't table types recognized by your JDBC driver.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197