tags:

views:

547

answers:

11

How can I detect if a certain table exists in a given SQL database in Java?

A: 

Maybe catch the exception if a simple query to it fail?

José Leal
duh, wow I totally missed that sorry for the stupid question. Thanks though.
Soldier.moth
Wouldn't this mean that you would interpret a misconfigured database connection or another error as the table not existing? You need to query the system to see definitively.
Paul Morie
It's not that stupid. There are many ways a simple query can fail, not only the inexistence of a table. Querying the metadata is the best option.
Alexandre
Certainly sounds like a hacky solution.
RichardOD
=D aeuhae, not if the query is "select * from table" and you catch only a SQLException
José Leal
+1  A: 

This is not a language-specific, but a database-specific problem. You'd query the metadata in the database for the existence of that particular object.

In SQL Server for instance:

SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[table]')
   AND type in (N'U')
cdonner
+2  A: 

Write a query that queries the table/view that will list the tables (this is different depending on DB vendor). Call that from Java.

Googling information_schema.tables will help a lot.

RichardOD
+1  A: 

Depending on the DB, you can do (MySQL)

SHOW TABLES

or (Oracle)

SELECT * FROM user_objects WHERE object_type = 'TABLE'

or another thing for SQL Server. Cycle through the results for MySQL or further filter on the Oracle one.

belgariontheking
+1  A: 

Why not just see if it is in sysobjects (for SQL Server)?

SELECT [name] FROM [sysobjects] WHERE type = 'U' AND [name] = 'TableName'
Bob
+9  A: 

You can use DatabaseMetaData.getTables() to get information about existing tables.

This method works transparently and is independent of the database engine. I think it queries information schema tables behind the scenes.

Edit:

Here is an example that prints all existing table names.

DatabaseMetaData md = connection.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
  System.out.println(rs.getString(3));
}
Ayman Hourieh
Thanks that is perfect.
Soldier.moth
A: 

There is a JDBC feature, database vendor independent - see [java.sql.DatabaseMetaData#getTables()][1]

You can get the DatabaseMetaData instance by calling java.sql.Connection#getMetaData()

[1]: http://java.sun.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String, java.lang.String, java.lang.String, java.lang.String[])

Swiety
+3  A: 

Use java.sql.DatabaseMetaData.getTables(null, null, YOUR_TABLE, null). If the table exists, you will get a ResultSet with one record.

See DatabaseMetaData.getTables

adrian.tarau
A: 

For MS Access:

Select Count(*) From MSysObjects 
Where type=1 And name='your_table_name_here'
Arvo
A: 

For ALL ANSI-compliant databases: (mySQL, SQL Server 2005/2008, Oracle, PostgreSQL, SQLLite, maybe others)

select 1 from information_schema.tables where table_name = @tableName
Jeff Meatball Yang
A: 

Check this link: HOW TO: SQL and Java for details on how to connect to SQL Server database from Java database applications as well as C#.NET database applications. It also describes how to pass embedded SQL queries, calling stored procedures, pass parameter etc.

SNK111