Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Murach's Java Servlets and JSP this week in the Servlets 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
Find out if a table exists
CHECK IF A TABLE ALREADY EXISTS
Checking the table already present or not
Check existence of table
db-create-table: [java] SQL Error at line 340: java.sql.SQLException: Table not