Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Detect if table exists

Reply
Thread Tools

Detect if table exists

 
 
raffaele castagno
Guest
Posts: n/a
 
      08-08-2004
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
 
Reply With Quote
 
 
 
 
Manish Hatwalne
Guest
Posts: n/a
 
      08-08-2004
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


 
Reply With Quote
 
 
 
 
sks
Guest
Posts: n/a
 
      08-09-2004

"Manish Hatwalne" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-09-2004
"Manish Hatwalne" <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      08-09-2004

"Tor Iver Wilhelmsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Manish Hatwalne" <(E-Mail Removed)> writes:
>
> > 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.


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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to detect if a type exists? Qi C++ 1 07-01-2011 04:58 PM
How to check if a directory exists? folder.exists() does not work! Ulf Meinhardt Java 8 08-28-2009 12:26 PM
Which preprecessor directive will detect if a typedef exists yet? Eg #if IS_TYPE(sometype_t)? Kevin Smith C Programming 3 10-29-2008 12:39 PM
[Q] how to detect if #super exists? Chuck Remes Ruby 8 10-27-2008 11:17 PM
finding if file exists or not in apache webdav server and if exists what is the mime type of it.... Totan Java 0 04-17-2006 05:13 AM



Advertisments