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);