0

I have an sqlite database to which I access from my Java program with JDBC. The connection is set like this:

private final static String dbfilelocation = "\\sql\\";
private final static String dbfilepath = dbfilelocation + "database.sqlite3";

//parse the database
private static void parseAllFromDB() {
    Connection c = connect2db(dbfilepath);
    // Here I get the values from database via connection 'c'
    disconnectfromdb(c);
}

// Connects with sqlite database
private static Connection connect2db(String dbfilename) {
    Connection c = null;
    try {
        Class.forName("org.sqlite.JDBC");
        String strConnection = "jdbc:sqlite:" + dbfilename;
        c = DriverManager.getConnection(strConnection);
        c.setAutoCommit(false);
    } catch (Exception e) {
        //Manage exceptions
    }
    return c;
}

This works as expected.

However, now I want to create a runnable .jar file that contains my 'database.sqlite3' file inside. Then, the database is no longer accesible as until now, since it is inside the .jar. How could I make JDBC to connect to a database that is inside the .jar file? Or is it a better practice to have the database always outside the .jar file? In that case, how?

Thanks.

Iban
  • 139
  • 2
  • 8

0 Answers0