L
L
lonata2019-01-21 12:01:52
Java
lonata, 2019-01-21 12:01:52

Where should the sqlite database file be located in the program folder so that the program can see it?

I wrote a program for a friend in JavaFX. Intelij Idea development environment. The database is SQlite.
What I want to achieve:
- to have a folder with the program, which will contain a database file (database.db)
- the necessary libraries for the program to work
- an executable program file, since the user is used to that all programs are launched through this file
I made a build program . Many files appeared in the output folder after that. Of all of them, I copied the ones I needed to a separate folder on my desktop so that I could wrap the jar file into an exe and then make an installer out of it all (to make it easier to install the program on other computers)
5c4587f204f11458841875.jpeg
After I made the installer of the program, the program starts and works fine, but the database in the program and the database that is in the database.db file do not match. It seems that the database file was sewn into a jar file during the build. And all the information in the program is taken not from a file in the program folder, but from a file that is in the jar file. I don’t need this, I need to have a database.db file in the program folder that can be edited (for example, with the DB Browser browser) and that all information after that changes in the program too.
In order to make an exe file, I used Launch4j, and to create an installer - Inno Setup
Where should I put the database.db file so that the program takes information from it?
This is the class in which I wrote the path to the database file:

public class DbConnection {

  private static final String SQCONN = "jdbc:sqlite::resource:database.db";

  public static Connection getConnection() throws SQLException{

    String path = SQCONN;
    try {
      Class.forName("org.sqlite.JDBC");
      return DriverManager.getConnection(path);
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }
    return null;
  }
}

Should I build the program in Intelij Idea like this?:
5c4589c62b848954237562.jpeg
Or like this?
5c4589dc8c866074185816.jpegOr do you not need to put the database.db file at all when building the jar file?
My project structure:
5c458b0ccd32c593743282.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sergey, 2019-01-24
kuzmin @sergueik

looked at myself - windows machine
without spring it is possible in my opinion

String.format("%s/src/main/resources/%s",
                                System.getProperty("user.dir"), resourceFileName);

well, variations

S
striver, 2019-03-03
@striver

jdbc:sqlite:database.db is not a sqlite file
If you want to shove it into the root directory, then here
Or in any folder, even on a network
"jdbc:sqlite:z:\\database.sqlite"

O
Orkhan, 2019-01-21
Hasanly @azerphoenix

Hello!
The database file will be where you specify the path to create it. It would be logical if you install the program, then get this path and unpack or create a database there.
It looks like you have a javafx project. In intellij idea there is an option to create an exe artifact.
If this is a pure database, then there is no point in packing it into a jar. You can create a database at the first start of the program and that's it. And already at subsequent launches (since the database is available), it will read the data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question