File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Saving all tablenames into a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Saving all tablenames into a string" Watch "Saving all tablenames into a string" New topic

Saving all tablenames into a string

Markus Reis

Joined: Jan 06, 2013
Posts: 12
Hi all,

I'm trying to save all names of all tables in my database into a String. I currently have the following:

is the problem
I found this solution on another board, but this doesnt work.

btw - is there maybe a better way or even a SQL-Statement to get the highest ID among several tables? I couldnt find one so I tried like this.

chris webster

Joined: Mar 01, 2009
Posts: 2289

Do you need to include more information in your call to getTables()?
Retrieves a description of the tables available in the given catalog. Only table descriptions matching the catalog, schema, table name and type criteria are returned. They are ordered by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM and TABLE_NAME.
catalog - a catalog name; must match the catalog name as it is stored in the database; "" retrieves those without a catalog; null means that the catalog name should not be used to narrow the search
schemaPattern - a schema name pattern; must match the schema name as it is stored in the database; "" retrieves those without a schema; null means that the schema name should not be used to narrow the search
tableNamePattern - a table name pattern; must match the table name as it is stored in the database
types - a list of table types, which must be from the list of table types returned from getTableTypes(),to include; null returns all types

Also, even if you get a list of all the tables in your database schema, are you sure that every table has an ID column? If not, your query will fail. It might be better to use a SQL query that will look in your database catalogue to find only those tables which have an ID column.

And why are you doing this? If you're doing this in order to determine the next ID to use for a record, it would be much better and safer to use your database's built-in mechanisms for this (e.g. auto-increment or sequence). Your query will be out of date as soon as a new record gets created somewhere with a new ID anyway. And what happens if somebody deletes the latest record - do you want to re-use the ID? Also, if you have a surrogate key (an arbitrary numeric ID as your primary key) then it's best not to worry about the actual value in the ID, so long as it's guaranteed to be unique (via sequence for example). If you're using sequences (e.g. on Oracle) to populate IDs, then you can use a single sequence as the source of your ID values and simply check the value in the sequence instead of having to check all your tables. Or just trust the sequence to do its job and don't worry about the actual value.

No more Blub for me, thank you, Vicar.
I agree. Here's the link:
subject: Saving all tablenames into a string
It's not a secret anymore!