Apache Derby - database validation already created?

Using Apache Derby with Java (J2ME, but I don't think it matters) is there a way to check if the database already exists and contains a table?

+6
java database derby
source share
3 answers

I know no one but a few works, unlike MySQL, where we have this IF EXIST facility.

What are you doing, try connecting to the database if it were not for its probable absence. And after a successful join, you can make a simple choice, for example, SELECT count (*) FROM TABLE_NAME, to find out if the table exists or not. You will be subject to an exception. Even in the official Sun example, I saw a similar work.

At Oracle, we have vocabulary tables to know about database objects. I doubt we have something like this in Derby.

[Edited]

Well, I found that there is a way to find out if a table exists. Try SELECT tablename FROM SYSTABLES. This is to check the existence of the table, to check the database, you may need to do something like this, I explained above.

+10
source share

Adeel , you can also use Connection.getMetaData to return DatabaseMetaData , then use getTables as soon as you connect to the database, of course. This has the advantage that you work in any database with a JDBC driver that is worth soloing.

To check for the presence of a database, if you use Derby in the built-in way or the server is on the same computer, you can check if the folder for the database exists. This is a bit of shreds. I would do as Adele suggests, and try to connect by catching an exception if it is not there.

+8
source share

I would suggest getting a DatabaseMetaData object, and then use the getTables method (null, null, null, new String [] {"TABLE"}), which returns a ResultSet. Use the next () method of ResultSet, which returns a boolean value to check if any tables exist. If it checks true, you have tables. False and the database is empty.

+2
source share

All Articles