query for tables
Hello
In my computer, I have some databases for different applications. I want to check a database to see whether it has tables that I need or not. If there are needed tables, I will create a connect to that database. If not, no connection is created.
How I can do that
Thanks
[301 byte] By [
suhua] at [2007-11-27 9:42:20]

# 1
> I want to check a database to see whether it has tables that I need or not. If there> are needed tables, I will create a connect to that database. If not, no connection is created.do you already know how to connect to a specific database?
# 2
Yep, I know. What I want is when user enter a wrong database name (but this database is already existed in the system - but with different data), that user cannot create a connection (or error is displayed)
suhua at 2007-7-12 23:45:34 >

# 3
You cannot query a database without connecting to it.Just connect to it, check if it has the tables, if not then simply close the connection and show a warning or so.
# 5
Yep, my main question is how I can know "whether a database has a needed table or not." ?
If I have query "SELECT * FROM my_table_name" for a connection to a database. It return null when that database does not have the table "my_table_name". But it also return null if that database has the table "my_table_name" and it is empty. How I can distinct two cases.
Thanks
suhua at 2007-7-12 23:45:34 >

# 6
Make use of the DatabaseMetaData, which is available by Connection#getMetaData(). The getCatalogs() function returns a resultset with the table names.You can also do it with SQL, but the specific queries differs per DB.
# 7
> Yep, my main question is how I can know "whether a
> database has a needed table or not." ?
>
> If I have query "SELECT * FROM my_table_name" for a
> connection to a database. It return null when that
> database does not have the table "my_table_name".
No. An SQLException will be thrown.
> But
> it also return null if that database has the table
> "my_table_name" and it is empty.
No. An empty ResultSet will be returned.
> How I can distinct
> two cases.
> Thanks