将sqlite DB添加到可执行JAR文件 [英] add sqlite DB to executable JAR file
问题描述
我正在使用JAVA(带有eclipse juno)并尝试创建一个包含sqlite DB文件的可执行JAR文件. 我尝试通过此行连接到数据库:
I am using JAVA (with eclipse juno) and try to create an executable JAR file which include sqlite DB file. I've try to get connection to the DB by this line:
DriverManager.getConnection("jdbc:sqlite:"+DataController.class.getResource("test.sqlite").getPath())
DataController是一个位于sqlite所在位置的类.
The DataController is a class that located where the sqlite located.
我一直收到错误消息:
java.sql.SQLException: invalid database address
有人可以提供帮助并逐步说明如何在可执行的JAR文件中包含sqlite DB吗?
Does someone can help and give step by step instructions about how to include sqlite DB inside an executable JAR file?
推荐答案
显然,sqlite-jdbc可以自行打开资源.通过该线程 https://groups.google.com/forum/?fromgroups =#!topic/xerial/Oayzj5nrJGk ,在路径中添加:resource.因此,请尝试以下操作:
Apparently sqlite-jdbc can open resources on it's own. Per this thread https://groups.google.com/forum/?fromgroups=#!topic/xerial/Oayzj5nrJGk, add :resource to the path. So try the following:
DriverManager.getConnection("jdbc:sqlite::resource:package/test.sqlite");
或取决于sqlite的版本
or depends on version of sqlite
DriverManager.getConnection("jdbc:sqlite::resource:package/test.db");
用带有/分隔的路径替换该文件所在的软件包.
Replacing package with the '/' separated path to the package this file is in.
请注意,它实际上会将文件复制到tmp目录中.-
Be aware that it will actually copy the file to a tmp directory.-
这篇关于将sqlite DB添加到可执行JAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!