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

查看:94
本文介绍了将嵌入式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:\ usersrs \idxxxxx \\ \\文件\\dist\src'
不存在

Opening connection failed: path to scr/release/db.db3:'C:\users\idxxxxx\Documents\\dist\src' does not exist

在代码中引用数据库时这样:

c = driverManager.getConnection(jdbc:sqlite:db.db3);

并添加db-文件到output-dir的根目录(所以不在jar本身),应用程序部分工作,但我的应用程序中缺少一些db-data(空组合框)。

所以似乎有还有一个问题。

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.

我的问题是:


  • 如何添加嵌入式db - sqlite在这种情况下 - 在netbeans到我的项目,所以它将成为我的项目的一部分?

  • 我应该在哪里放置db-file以及如何从我的项目代码中引用它?

我不希望最终用户看到他将收到的文件中的任何db文件

所以我想要db-file如果可能的话,成为.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,直接进入th e program。

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

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

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

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

然后应用程序知道复制提取文件的位置(db来自jar的.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中。

数据库文件将被解压缩到临时文件夹 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: "...");
 }

更新oct.2014

Update oct. 2014

org.sqlite SQLiteConfig + SQLiteDataSource

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

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

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