This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes CREATE TABLE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "CREATE TABLE" Watch "CREATE TABLE" New topic


maggi sesh

Joined: Nov 01, 2000
Posts: 8
Hi Friends,
i would like to know how to check if a particular table name exists in a database (in the java program). When i give a create statement , if the table already exists it throws an exception.
I tried dropping the table first and then creating a new..that too gives a problem in cases where the table doesnt exist..can someone help me out
martin samm
Ranch Hand

Joined: Nov 09, 2000
Posts: 31
for Oracle you can use the data dictionary view 'USER_TABLES' or 'ALL_TABLES', and use the 'EXISTS' clause - i'm not sure, however, if this is going to be present in the database you're using

martin samm

martin samm<BR>
arunkumar hk

Joined: Mar 03, 2001
Posts: 5
You can execute this query before creating or dropping the table. Based on the returned value, either create or drop the table.
select tname from tab where tname ='XYZ';
if the result set contains rows then the table exists else table does not exists.

Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
JDBC offers database-independent access to the data dictionary. See how your JDBC driver reacts to connection.getMetaData.getTables(null, "schema", "table"). This returns a ResultSet.
- Peter
I agree. Here's the link:
It's not a secret anymore!