将嵌入式 sqlite-db 添加到 Netbeans 项目 [英] Adding embedded sqlite-db to Netbeans project

查看:29
本文介绍了将嵌入式 sqlite-db 添加到 Netbeans 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题让我忙了一段时间.
我正在 Netbeans 中构建一个 Java 项目,并且我在这个项目中使用了一个嵌入式 sqlite DB.
目前,数据库位于 src/release/ 包中.

I have the following question which is keeping me busy for some time now.
I am building a Java project in Netbeans and I have an embedded sqlite DB which I use in this project.
Currently the DB is located in the package src/release/.

我通过以下方式从代码中引用数据库:

I reference the db from the code the following way:

c = DriverManager.getConnection("jdbc:sqlite:src/release/db.db3");

当我从 Netbeans 中运行该项目时,它可以正常工作.但是当我尝试构建它并运行创建的 jar 文件(在 dist 文件夹中)时.

When I run the project from within Netbeans it works without any problem. But when I try to build it and run the created jar-file (in the dist-folder).

我收到以下错误消息(翻译自荷兰语描述):

I get the following error message (translated from Dutch description):

打开连接失败:路径scr/release/db.db3:'C:usersidxxxxxDocuments\distsrc'不存在

Opening connection failed: path to scr/release/db.db3:'C:usersidxxxxxDocuments\distsrc' does not exist

当像这样在代码中引用数据库时:
c = driverManager.getConnection("jdbc:sqlite:db.db3");
并将 db 文件添加到输出目录的根目录中(因此不在 jar 本身中),应用程序部分工作,但我的应用程序中缺少一些 db 数据(空组合框).
所以似乎也有问题.

When referencing the DB in the code like this:
c = driverManager.getConnection("jdbc:sqlite:db.db3");
and adding the db-file to the root of the output-dir (so not in the jar itself), the application works partly, but some db-data is missing in my application (empty comboboxes).
So there seems to be an issue also.

我的问题是:

  • 如何在我的项目中在 netbeans 中添加嵌入式数据库 - 在这种情况下是 sqlite,以便它成为我项目的一部分?
  • 我应该将 db 文件放在哪里以及如何从我的项目代码中引用它?

我不希望最终用户在他将收到的文件中看到任何 db 文件.
所以如果可能的话,我希望 db 文件成为 .jar 的一部分.

I don't want the enduser to see any db-file in the file(s) he will receive.
so I would like the db-file to be part of the .jar if possible.

Tnx

推荐答案

SQLite 需要单独的文件,甚至不需要更新数据库.

SQLite needs separate files, even without the need to update the database.

需要确保锁定并确保数据库 ACID 属性.

Need ensures locking and ensures database ACID properties.

.jar 中的 SQLite

  • SQLite 旨在处理直接文件访问.
  • SQLite 可能需要某些环境中不可用的额外权限.

SQLite 数据文件可以在启动时从 JAR 中提取到一个临时位置.

The SQLite data files could be extracted from the JAR to a temporary location at start.

将数据库url直接写到程序中不是一个好的选择.

It is not a good choice to write the database url, directly into the program.

getConnection("jdbc:sqlite:src/release/db.db3");

让您的应用程序查找文件.如果未找到:error: File db.db3 not found 消息.然后知道用户,不是程序出错,而是缺少数据库文件.

Let your application look for the file. If not found: error: File db.db3 not found message. Then know the user, not the program is going wrong, but it is missing the database file.

因为你用的是java,所以很容易创建动态url.
例如"jdbc:sqlite:"+PathToApp +"/data/db.db3".
然后应用程序知道在哪里复制提取的文件(来自 jar 的 db.db3).

Since you are working with java, it is easy to create dynamic url.
e.g. "jdbc:sqlite:"+PathToApp +"/data/db.db3".
Then the application knows where to copy the extracted file (db.db3 from the jar ) .

从 .jar 中提取 SQlite

  • 你可以让 java 为你做这件事.
  • 使用jdbc:sqlite::resource:
  • 您需要 sqlite-jdbc JAR,因此提取 JAR 文件并将其添加到应用程序 JAR 中.

DB 文件将被提取到一个临时文件夹 System.getProperty(java.io.tmpdir").

DB files will be extracted to a temporary folder System.getProperty("java.io.tmpdir").

例如

 [...]
 import org.sql.*;
 import org.sqlite.*; 
 [...]
 try {
    Class.forName("org.sqlite.JDBC");
    SQLiteConfig config = new SQLiteConfig();
    config.enableFullSync(true);
    config.setReadOnly(false);
    SQLiteDataSource ds = new SQLiteDataSource(config);
    ds.setUrl("jdbc:sqlite::resource:"+
              getClass().getResource("db.db3").toString());
    conn = ds.getConnection();
 }catch(SQLException se)
 {
  System.out.println("SQLError: "...");
 }

更新十月.2014

org.sqlite SQLiteConfig + SQLiteDataSource

2014 年 10 月 8 日:sqlite-jdbc-3.8.6.jar 更新至 sqlite 3.8.6

这篇关于将嵌入式 sqlite-db 添加到 Netbeans 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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