java sqlite jar,如何将SQLite数据库打包到jar中?

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

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.

My package structure looks like this:

Bank

|

|

+---src

| ...

|

+---bin

| ...

|

+---data

| bank_database.db

|

+---img

| ajax-refresh-icon.gif

|

+---doc

| Datenbankschema.uxf

|

\---resources

sqlitejdbc-v056.jar

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();

}

}

}

If I pack all this stuff into a jar with the Eclipse export manager and try to execute it it throws a ClassNotFoundException for the JDBC driver.

How can I fix this problem?

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

解决方案

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.).

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值