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

1 Answer 1

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

13 Comments

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 ?
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 )
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
updated to show how to create SQLite file in home directory
Thank you, do you know how to do it in JPA ? found this but not working for me stackoverflow.com/questions/3430723/…
|

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.