Search code examples
javaswingderby

Java application with embedded Derby database


I created a java swing application with apache derby database. I want to know how can i do the following things.

The first time the application is double-clicked by a user, that finds a suitable location for the Derby database on the user's machine, creates the database and defines all the tables, views, etc and loads any initial data. Then, on subsequent runs of the application, it will be able to re-open the database and continue using it.


Solution

    • You could use the user.home property from System.getProperty which will return you the user's home directory as the main path for the database. On Windows, I would recommend using {user.home}\AppData\Remote\{You application name} as a base path
    • Derby creates a directory of the same name as the database, you could check for the existence of this directory. The problem with this is there is no guarantee that it contains a valid database.
    • You could create a normal connection to the database and check for the existence of existing tables and build them as required. This ensures that if, for some reason, not all the tables where created, you can recover at that point.