Detect if table exists

  • Thread starter raffaele castagno
  • Start date
R

raffaele castagno

Il Sun, 08 Aug 2004 23:46:11 +0530, Manish Hatwalne ha scritto:
This should be easy, but I am unable to find it now.
How can I detect if a particular table exists or not in my DB (I am using
MySQL).
I cannot delete table, so I can't use drop if exists; but can I use count
(*) if exists, or sth similar? I am not much of a DB person.
Any pointers would be very useful.

AFAIK, if you try to access a table wich does not exist, JDBS should raise
a DataAccessException, if I remember right... You can try to run a query
on the table you want to check, and catch that exception.

Read MySql JDBC driver and Java JDBC documentation for details, and of
course wait the answer of developer more experienced than me...

Bye

Raffaele
 
M

Manish Hatwalne

This should be easy, but I am unable to find it now.
How can I detect if a particular table exists or not in my DB (I am using
MySQL).
I cannot delete table, so I can't use drop if exists; but can I use count
(*) if exists, or sth similar? I am not much of a DB person.
Any pointers would be very useful.

TIA,
- Manish
 
S

sks

Manish Hatwalne said:
This should be easy, but I am unable to find it now.
How can I detect if a particular table exists or not in my DB (I am using
MySQL).
I cannot delete table, so I can't use drop if exists; but can I use count
(*) if exists, or sth similar? I am not much of a DB person.
Any pointers would be very useful.

You could use DESCRIBE tablename and catch the exception if it doesn't
exist.

Or you could use SHOW TABLES and loop through results to see if its exists.
 
T

Tor Iver Wilhelmsen

Manish Hatwalne said:
How can I detect if a particular table exists or not in my DB (I am using
MySQL).

Ask the driver for database metadata, and check for the table there.

Connection c = ...
DatabaseMetaData dbm = c.getMetaData();
ResultSet tables = dbm.getTableNames(null, null, "TABLENAME", null);
if (rs.next()) {
// Table exists
}
else {
// Table does not exist
}

Adjust parameters, check for multiple entries etc. as you see fit.
 
L

Liz

Tor Iver Wilhelmsen said:
Ask the driver for database metadata, and check for the table there.

Connection c = ...
DatabaseMetaData dbm = c.getMetaData();
ResultSet tables = dbm.getTableNames(null, null, "TABLENAME", null);
if (rs.next()) {
// Table exists
}
else {
// Table does not exist
}

Adjust parameters, check for multiple entries etc. as you see fit.

I use
execute("SHOW TABLES");
which returns a result set that contains the names of all tables in
the database.
This way you have no exception to worry about.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top