user3747544
user3747544

Reputation: 35

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.

Upvotes: 2

Views: 566

Answers (2)

Mohammad Badiuzzaman
Mohammad Badiuzzaman

Reputation: 748

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.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

  • 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.

Upvotes: 3

Related Questions