Search code examples
javasqlitejdbc

Set path to a jdbc SQLite database, where it will be located


I am using the following script to connect to a local SQLite database in Java:

public static Connection createOrOpenDatabase(String database) {

        String url = "jdbc:sqlite:" + database;

        try {
            Connection conn = DriverManager.getConnection(url);
            return conn;
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

So this works pretty good and it saves it in the same directory, where also the jar is located. Now is my question, how can I set a absolute path, where the database is located.

For example the current location of the db file is:

C:\MyProjects\JavaFXProject\app\dist\main.db

Now I want to save this file to the user.home directory:

C:\Users\Julian\MyProjects\JavaFXProject\main.db

How can I achieve this?


Solution

  • how can I set a[n] absolute path

    You can just include it as part of the connection URL, e.g.,

    jdbc:sqlite:C:/Users/Julian/MyProjects/JavaFXProject/main.db