Apache Derby - Check Database Already Created?

Peter picture Peter · Feb 25, 2009 · Viewed 10.9k times · Source

Using Apache Derby with Java (J2ME, but I don't think that makes a difference) is there any way of checking if a database already exists and contains a table?

Answer

Adeel Ansari picture Adeel Ansari · Feb 25, 2009

I know of none, except few work around, unlike MySQL where we have that facility of IF EXIST.

What you do is, try to connect to the database, if couldn't its likely its not there. And after a successful connection, you can do a simple select, like SELECT count(*) FROM TABLE_NAME, to know whether the table exist or not. You would be depending on the exception. Even in an official example from Sun, I have seen the similar work around.

In Oracle we have dictionary tables to know about the database objects. I doubt if we have anything like that in Derby.

[Edited]

Well, I found that there is a way to know if the table exist. Try, SELECT tablename FROM SYSTABLES. It is for checking the existence of a table, for checking database you may need to do similar thing, I explained above.