This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JDBC 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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "CREATE TABLE" Watch "CREATE TABLE" New topic
Author

CREATE TABLE

maggi sesh
Greenhorn

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
thanks
maggisesh
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
m_sam@rroom.net


martin samm<BR>m_sam@rroom.net
arunkumar hk
Greenhorn

Joined: Mar 03, 2001
Posts: 5
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

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: http://aspose.com/file-tools
 
subject: CREATE TABLE
 
Similar Threads
Check existence of table
Checking the table already present or not
Find out if a table exists
db-create-table: [java] SQL Error at line 340: java.sql.SQLException: Table not
CHECK IF A TABLE ALREADY EXISTS