jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes Detect if a table exists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Detect if a table exists" Watch "Detect if a table exists" New topic
Author

Detect if a table exists

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I am writing an application that uses JDBC to connect to a database. I want to create the necessary tables when the application starts, but only if the tables dont' exists already. Is there a generic way to use JDBC to detect if a certain table exists? I think there is a way using the database's metadata tables and select statements, but I'm not sure if this will be available across all platforms. I really want to create the tables programmatically so the user doesn't have to worry about it.

Layne


Java API Documentation
The Java Tutorial
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You should probably do this in SQL and not in your java code per say. I don't know what all DB's support this syntax but I know in mysql you can do:

create table if not exists table_name....


GenRocket - Experts at Building Test Data
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'm developing with MySQL, but I think the target platform will use an Access database (at least initially). I'll see if this syntax works with Access as well.

I would guess that I need to do it with SQL, but I want to be able to execute the SQL statement in my Java program via JDBC. I'll have to see if this will work for multiple platforms.

Layne
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Layne,
Have you tried the "getTables()" method of the "java.sql.DatabaseMetaData" interface? Of-course, it is not guaranteed to be supported by every JDBC driver.

Good Luck,
Avi.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Avi Abrami:
Layne,
Have you tried the "getTables()" method of the "java.sql.DatabaseMetaData" interface? Of-course, it is not guaranteed to be supported by every JDBC driver.

Good Luck,
Avi.


Nope, I haven't come across that method yet. Thanks for the suggestion. Now that you mention it, I remember seeing a reference to the DatabseMetaData class, but I haven't looked at it further. It sounds like I should look at that class more closely. It might provide some solutions to other problems I hope to solve soon.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Detect if a table exists
 
Similar Threads
help with the derby-ij tool
Hibernate 2: check if table exists and get the name of the table out of the class
How can i get all the tables of an active JDBC connections.
Detect if table exists
db-create-table: [java] SQL Error at line 340: java.sql.SQLException: Table not