How to include SQLite database in executable Jar?

天涯浪子 提交于 2019-11-26 08:56:19

问题


I have created a Swing application that uses SQLite as a local database. The database file is located in project\'s root directory.

Project/DatabaseFile

The application runs fine on Eclipse, but when I run the packaged executable Jar, I get the following error:

No such table : table1

This means that the database is not reachable. When I examined the contents of the resulting JAR file, the database file was not there anymore.

In the code, I\'ve linked the database as follows:

jdbc:sqlite:DatabaseFile

My question is, how to include the SQLite database in the executable Jar?

EDIT

When I placed the DB file in the source Folder Project/src/DatabaseFile and changed the path to jdbc:sqlite:src/DatabaseFile, it worked on Eclipse but again when running the Jar file as java -jar Project.jar. It said:

path to \'src/DatabaseFile\': \'C:\\Users\\name\\src\' does not exist

I think I need to specify a relative path for the database.

EDIT

This is how I connect to the database:

public Connection getConnection(){      
    try{
        Class.forName(\"org.sqlite.JDBC\").newInstance();             
        con = DriverManager.getConnection(\"jdbc:sqlite:src/DatabaseFile\");              

    } catch (Exception e) {
        Log.fatal(\"Méthode: getConnection() | Class  : SQLiteConnection | msg system : \" + e.getMessage());
    }
    return con;
}

回答1:


What library are you using for SQLite?

I did a search based on the connection URI you indicated and found this one. In the documentation it says:

2009 May 19th: sqlite-jdbc-3.6.14.1 released. This version supports "jdbc:sqlite::resource:" syntax to access read-only DB files contained in JAR archives, or external resources specified via URL, local files address etc. (see also the detailes)

If that is the driver you are using, then I would suggest the following connection URI:

"jdbc:sqlite::resource:DatabaseFile"

The key is that since your database is in a jar file, it can not be access as a file with FileInputStream. Instead it must be accessed through the JVM's support for it (namely with Class.getResource() or Class.getResourceAsStream()). Do note that resources contained within jar files are read-only. You won't be able to save any changes to your database.




回答2:


I have found two different ways to name the filepath depending on how you are trying to access it. Assuming you are accessing the db is located in /yourproject/resource/ or /yourproject/bin/resource ( havent narrowed it down, mine is in both and I'm happy with it) you should use this as your path:

//Eclipse test path
String url = "jdbc:sqlite:resource/mydb.db";

or

//runnable jar path
String url = "jdbc:sqlite::resource:mydb.db";

then

mysqlitedatasource.setUrl(url);

Your way also works... by putting the db in /src




回答3:


I found the solution. You should CREATE table in database after connectiom. Like this in your class with connection.

 public void createDb() throws SQLException {
        Statement statement = connection.createStatement();
        PreparedStatement preparedStatement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS threads `your table`;    
        preparedStatement.executeUpdate();
        preparedStatement.close();
    }

Because when the programm compiles in jar it doesn't automatically creates the table in databse.

So first of all you have to get connection and create a table if not exists like this:

 Connect connect = new Connect();
        connect.getConnection();
        connect.createDb();
        connect.closeConnection();


来源:https://stackoverflow.com/questions/12019974/how-to-include-sqlite-database-in-executable-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!