1

I made a javafx application and I made an installation for the user.

I followed the oracle doc for packaging and installation and I customized the config file to allow the user to choose which directory he wants to install the application.

So, I want to have the scenario : wherever the user has chosen where to install the application, he'll be able to write to the database.

The question is: how to deploy an SQLite database in a shared system folder ?

Thank you in advance for yours suggestions

michael laudrup
  • 27
  • 3
  • 10

1 Answers1

2

What about saving it to users home directory? He should have enough privileges to read/write to it. You can ask Java for users home by System.getProperty("user.home");


Edit: JDBC solution

You can create your SQLite DB in application by using jdbc:sqlite:filename.db in your JDBC connection string. That would create it in the current directory, so instead of filename.db use something like

String filePath = System.getProperty("user.home")+System.getProperty("file.separator")+"myDBfile.db";
Connection  connection = DriverManager.getConnection("jdbc:sqlite:"+filePath);

This will create connection to DB file located in the home directory of the current user. If the file doesn't exists, it will create it. Now the problem is, that this DB file is empty. I think you have more possibilites how to deal with it. For example use CREATE TABLE IF NOT EXISTS table_name(..).

It doesn't matter where did user install application because DBs path is set to his home instead of working directory and application will always look for it there.


Edit: JPA solution

General idea is to override url property in your persistence.xml. This can be done by giving map of properties to Persistence.createEntityManagerFactory() when creating EntityManagerFactory . Very nice example is here.

Here is sample code that should create EMF ready to work with database in users home (filePath is defined same as above).

Map<String, String> persistenceMap = new HashMap<String, String>();
persistenceMap.put("javax.persistence.jdbc.url", "jdbc:sqlite:" + filePath);
EntityManagerFactory managerFactory = Persistence.createEntityManagerFactory("MyPU", persistenceMap);
Community
  • 1
  • 1
Manik
  • 96
  • 7
  • It's a solution but I don't know how to deploy the database in this folder, so how could we do it after installation or deployment ? – michael laudrup Aug 30 '15 at 02:12
  • I don't know what kind of installation it is, but you can have some prepared DB files in archive and let the installation wizard copy it to the folder. Second way could be checking right after application start if the DB exists and creating it if it doesn't (or just connect and it could create it automatically as mentioned here: [link](http://stackoverflow.com/a/14951139/5277348) ) – Manik Aug 30 '15 at 11:07
  • I've edited my question to be more clear. I know that if I install in the user directory it's going to work but if the user chooses for example a folder with limited write access, he won't be able to write to it, so I need to install the DB in a shared system folder – michael laudrup Aug 30 '15 at 16:17
  • updated to show how to create SQLite file in home directory – Manik Aug 30 '15 at 17:23
  • Thank you, do you know how to do it in JPA ? found this but not working for me http://stackoverflow.com/questions/3430723/how-to-specify-a-jdbc-url-in-the-persistence-xml-relative-to-the-application-fol – michael laudrup Aug 30 '15 at 22:15
  • Thank you for the update, it works. So how could I let the installation wizard copy the DB file to user directory ? I'm using Netbeans with Ant, what should I add to build.xml ? – michael laudrup Aug 31 '15 at 16:11
  • What kind of installation wizard are you using? Your own or 3rd party solution? If it does use Ant and targets, you can write your target using copy tasks like [here](http://stackoverflow.com/questions/5144496/how-can-i-extract-class-files-from-jar-files) or [here](http://stackoverflow.com/questions/11161539/is-there-a-way-in-ant-to-extract-one-class-file-from-a-jar-and-put-it-in-another) – Manik Aug 31 '15 at 17:50
  • Thank you, I'm using the wizard generated by Inno Setup after packaging using Netbeans and Ant. I don't need to copy a jar or file in the dist folder, I need to copy the prepared DB file in the user folder, so it's an Ant task but it's a post processing installation task. – michael laudrup Aug 31 '15 at 18:09
  • Well... This is a little bit tricky. Inno setup can do this job. [Here](http://stackoverflow.com/a/25306349/5277348) is the question (and answer) about adding additional files into setup. So if you are using your own *.iss file, it could help. – Manik Aug 31 '15 at 23:12
  • I finished by copying the DB file in the dist folder and after installation from the application folder to the user folder – michael laudrup Sep 01 '15 at 00:03
  • Finally, I found that the solution with Inno setup is better than copying the DB file two times – michael laudrup Sep 05 '15 at 18:22
  • Hello Manik, I'm trying to do the same "Copying the Db file while installation " but on Mac, do you know how to do it ? – michael laudrup Oct 06 '15 at 18:24
  • I have no experience with Mac. – Manik Oct 07 '15 at 11:16