2

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.

3
  • 1
    You can use "user.home" from System.getProperty to find the users home directory. You can use this as a base for your path (I'd append the application name to this). The database is stored in a directory with the same name as the database. If this directory exists (and constrains file), the database should exit. Another solution might be to create a connection to the database in the normal way and then check for the existence of one or more tables Commented Sep 15, 2014 at 22:14
  • That comment might be better as an answer, @Mad. Commented Sep 15, 2014 at 22:18
  • You might want to have a look into liquibase and use it programatically for setting up and populating the database. Commented Sep 15, 2014 at 22:19

2 Answers 2

3
  • 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.
Sign up to request clarification or add additional context in comments.

Comments

0

Following shall start derby database service on default port (1527) on localhost

    NetworkServerControl obj= new NetworkServerControl();
    obj.start(null);

Use dburl with 'create=true', i.e.

    jdbc:derby://localhost:1527/macdb;create=true 

Above shall make db dir (macdb), same name as service name on current dir.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.