如何将SQLite数据库打包到jar中? [英] How to pack a SQLite database into a jar?

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

问题描述

我有一个Java项目,它使用一个小的SQLite数据库。

I've got a Java project, which uses a small SQLite database.

现在我想用Database文件和驱动程序创建一个可执行的jar文件( sqlitejdbc-v056 )里面有一个包含所有内容的包。

Now I want to create an executable jar File, with the Database file and the driver (sqlitejdbc-v056) inside to have a single, all containing package.

我的包结构如下所示:

Bank
|
| 
+---src
|   ...
|           
+---bin
|   ...
|           
+---data
|       bank_database.db
|       
+---img
|       ajax-refresh-icon.gif
|       
+---doc
|       Datenbankschema.uxf
|       
\---resources
        sqlitejdbc-v056.jar

我访问具有这个小Java类的DB:

I access the DB with this small Java class:

package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBHandle {

    Connection conn;

    public DBHandle() throws Exception {
        Class.forName("org.sqlite.JDBC");
    }

    public Connection openConnection() throws SQLException {
        conn = DriverManager.getConnection("jdbc:sqlite:data\\bank_database.db");
        return conn;
    }

    public void closeConnection() throws SQLException {
        if (!conn.isClosed()) {
            conn.close();
        }

    }

}



<如果我使用Eclipse导出管理器将所有这些内容打包到jar中并尝试执行它,则会为JDBC驱动程序抛出 ClassNotFoundException

如何解决这个问题?

甚至可以将资源修改为与数据库相关的jar吗?

Is it even possible to modify resources into a jar regarding to the database?

推荐答案

AFAIK你不能在没有太多麻烦的情况下修改jar内的资源(基本上它是一个zip,因此它应该是可能的,但可能有由JVM等文件锁定。)。

AFAIK you can't modify resources inside a jar without much hassle (basically it is a zip and thus it should be possible, but there might be file locks by the JVM etc.).

此外,你不能把一个jar放到另一个jar中。但是,如果需要,可以解压缩驱动程序jar并将内容包含在jar中。 Maven程序集插件的目标是这样做: jar-with-dependencies

Additionally, you can't put one jar into another. However, you could unpackage the driver jar and include the contents in your jar, if you want that. The Maven assembly plugin has a goal that does this: jar-with-dependencies.

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

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