如何在可执行 Jar 中包含 SQLite 数据库? [英] How to include SQLite database in executable Jar?

查看:46
本文介绍了如何在可执行 Jar 中包含 SQLite 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用 SQLite 作为本地数据库的 Swing 应用程序.数据库文件位于项目的根目录.

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

该应用程序在 Eclipse 上运行良好,但是当我运行打包的可执行 Jar 时,出现以下错误:

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

No such table : table1

这意味着无法访问数据库.当我检查生成的 JAR 文件的内容时,数据库文件已经不存在了.

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

我的问题是,如何在可执行 Jar 中包含 SQLite 数据库?

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

编辑

当我将 DB 文件放在源文件夹 Project/src/DatabaseFile 并将路径更改为 jdbc:sqlite:src/DatabaseFile 时,它在 Eclipse 上工作但再次运行 Jar 文件作为 java -jar Project.jar.它说:

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
amesrc' does not exist

我想我需要为数据库指定一个相对路径.

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

编辑

这是我连接到数据库的方式:

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

推荐答案

SQLite 使用什么库?

What library are you using for SQLite?

我根据您指出的连接 URI 进行了搜索,并找到了这个.在 文档 中,它说:

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

2009 年 5 月 19 日:sqlite-jdbc-3.6.14.1 发布.此版本支持jdbc:sqlite::resource:"语法来访问包含在 JAR 存档中的只读 DB 文件,或通过 URL、本地文件地址等指定的外部资源(另请参见 详情)

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)

如果这是您使用的驱动程序,那么我建议使用以下连接 URI:

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

"jdbc:sqlite::resource:DatabaseFile"

关键是,由于您的数据库是在 jar 文件中,因此无法使用 FileInputStream 作为文件访问.相反,它必须通过 JVM 对它的支持来访问(即使用 Class.getResource()Class.getResourceAsStream()).请注意,jar 文件中包含的资源是只读的.您将无法保存对数据库的任何更改.

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.

这篇关于如何在可执行 Jar 中包含 SQLite 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆