Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CREATE TABLE

 
maggi sesh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
thanks
maggisesh
 
martin samm
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
m_sam@rroom.net
 
arunkumar hk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.

rgds
arun.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic