简体   繁体   English

将sqlite DB添加到可执行的JAR文件中

[英]add sqlite DB to executable JAR file

I am using JAVA (with eclipse juno) and try to create an executable JAR file which include sqlite DB file. 我正在使用JAVA(使用eclipse juno)并尝试创建一个包含sqlite DB文件的可执行JAR文件。 I've try to get connection to the DB by this line: 我试图通过这一行获得与DB的连接:

DriverManager.getConnection("jdbc:sqlite:"+DataController.class.getResource("test.sqlite").getPath())

The DataController is a class that located where the sqlite located. DataController是一个位于sqlite所在位置的类。

and i keep get an error: 我一直得到一个错误:

java.sql.SQLException: invalid database address

Does someone can help and give step by step instructions about how to include sqlite DB inside an executable JAR file? 有人可以帮助并提供有关如何在可执行JAR文件中包含sqlite DB的分步说明吗?

Apparently sqlite-jdbc can open resources on it's own. 显然sqlite-jdbc可以自己打开资源。 Per this thread https://groups.google.com/forum/?fromgroups=#!topic/xerial/Oayzj5nrJGk , add :resource to the path. 根据此主题https://groups.google.com/forum/?fromgroups=#!topic/xerial/Oayzj5nrJGk ,将:resource添加到路径中。 So try the following: 请尝试以下方法:

DriverManager.getConnection("jdbc:sqlite::resource:package/test.sqlite"); 

or depends on version of sqlite 或取决于sqlite的版本

DriverManager.getConnection("jdbc:sqlite::resource:package/test.db"); 

Replacing package with the '/' separated path to the package this file is in. 用'/'分隔的路径替换包到此文件所在的包。

Be aware that it will actually copy the file to a tmp directory.- 请注意,它实际上会将文件复制到tmp目录.-

The ::resource way is right. ::resource方式是对的。 And these explanations will help if you are using ::resource but still get errors like resource database.db not found: java.net.MalformedURLException: no protocol: database.db like verdana. 如果您正在使用::resource但这些解释将有所帮助,但仍然会resource database.db not found: java.net.MalformedURLException: no protocol: database.db错误resource database.db not found: java.net.MalformedURLException: no protocol: database.db like verdana。

Most common anwsers are: DriverManager.getConnection("jdbc:sqlite::resource:path/to/database.db") 最常见的anwsers是: DriverManager.getConnection("jdbc:sqlite::resource:path/to/database.db")

However the path/to/database.db must be the exact path( the Path in Real File System but not in Jar ) to your jar file. 但是, path/to/database.db必须是jar文件中的确切路径( 实际文件系统中的路径,但不在Jar中 )。

I recommend to use getClass().getResource() : 我建议使用getClass().getResource()

DriverManager.getConnection("jdbc:sqlite::resource:" + 
        getClass().getResource("/path/to/db/in/the/jar/file"))

NOTE :the /path/to/db/in/the/jar/file part must start with / 注意/path/to/db/in/the/jar/file部分必须以/开头

I'm not sure if this has changed in recent JDBC versions, but after fiddling with it for about an hour and getting various exceptions (MalformedURLException and "Database has been closed"), I found that the following worked for me: 我不确定在最近的JDBC版本中是否已经改变了,但在使用它大约一个小时并获得各种异常(MalformedURLException和“数据库已关闭”)后,我发现以下内容对我有用:

DriverManager.getConnection("jdbc:sqlite::resource:file:/path/to/database.db")

The :file: portion seems to be missing from other answers and I couldn't get it to work without it. :file:部分似乎从其他答案中丢失了,如果没有它我就无法工作。

Also, note that the 另外,请注意

/path/to/database.db

is the absolute path starting from the root of the .jar file, rather than a normal resource root. 是从.jar文件的根开始的绝对路径,而不是普通的资源根。

jdbc:sqlite::resource:notes_app.db

worked for me. 为我工作。 My database(notes_app.db) was in the root of generated jar file. 我的数据库(notes_app.db)位于生成的jar文件的根目录中。

public class Database {
    public static Connection con () throws SQLException {
        String url = "jdbc:sqlite::resource:notes_app.db";
        return DriverManager.getConnection(url);
    }
}

My notes_app.db is in the root of generated jar. 我的notes_app.db位于生成的jar的根目录中。 I'm developing with maven and notepadd++ 我正在使用mavennotepadd ++开发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM