简体   繁体   中英

Fetching Table Names from Oracle with Java?

I'm working on a Java GUI project; in one of its property screen I use 3 comboboxes in order to get: first user chooses DB name, with this value 2.Schema names and with this all table names under that schema. It's OK when I use ResultSet when getting the values from DB and setting them. But with this way, I could only bring ONE value because you know the way ResultSet works. So is there any other way that I could fetch all the table names with let's say:

SELECT TABLE_NAME FROM ALL_TABLES; and put them on an array of Strings or sth... Anything possible, I just wanna have their names in a list or anything.

Thank you.

EDIT:

Here's the simple code tested for the purpose:

ResultSet rs = null;
PreparedStatement ps = null;
String result;
ps = conn.prepareStatement("SELECT TABLE_NAME FROM ALL_TABLES");

rs = ps.executeQuery(); 

while(rs.next())
{
        result = rs.getString(1);
    System.out.println(rs.getString(1));
}

You could also use:

DatabaseMetaData  metadata = currentConnection.getMetaData();
String[] names = {"TABLE"}; 
ResultSet tables = metadata.getTables(null,"%", "%", names);
while (tables.next()) { 
  String tableName = tables.getString("TABLE_NAME"); 
  String tableSchema = tables.getString("TABLE_SCHEM");
}

Read this for more info.

You need to quesry for meta data basically. See this example on how to do it.

Not sure what you mean by 'you know the way resultset works'?! This will return all the rows from the query:

while (rs.next()) {
    System.out.print("Column 1 returned ");
    System.out.println(rs.getString(1));
}

Just use a List:

List<String> tableNames = new ArrayList<String>();
while(rs.next()) {
    String tableName = rs.getString(1);
    tableNames.add(tableName);
}
// there: your list contains all the table names

Read the tutorial about collections . This is stuff that all Java developers must know.

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