使用Java .jar文件提供数据库?

时间:2021-01-23 18:04:28

I want to create a program which users run just by running a .jar file. I want this program to have access to a database, which is pre-filled with some data when the program is first downloaded, and as the user uses it, more data is added to the db.

我想创建一个程序,用户只需运行.jar文件即可运行。我希望这个程序能够访问数据库,该数据库在首次下载程序时预先填充了一些数据,并且当用户使用它时,会向数据库添加更多数据。

The db should be locally stored on the user's computer.

数据库应本地存储在用户的计算机上。

What is my best option for this? Which database engine should I go with, and what do I have to do to make it so the user won't have to setup the db when he installs the app, it will come pre-filled with the .jar file?

我最好的选择是什么?我应该使用哪个数据库引擎,以及我需要做些什么才能使用户在安装应用程序时不必设置数据库,它将预先填充.jar文件?

3 个解决方案

#1


4  

A simple solution would be to use an embedded database stored locally on the disk (for example near the jar file). You can use h2, or even sqlite for this.

一个简单的解决方案是使用本地存储在磁盘上的嵌入式数据库(例如在jar文件附近)。您可以使用h2,甚至是sqlite。

When loaded the program will check for the existence of the database, if it's not here, just play a setup SQL script that will create the database structure and initial data, otherwise you're good to go, just do the stuff that your application was created to do.

加载程序时会检查数据库是否存在,如果不在这里,只需播放一个设置SQL脚本,创建数据库结构和初始数据,否则你很好,只需要做你的应用程序的东西。创造了做。

Here is a really simple example with h2 database:

这是一个非常简单的h2数据库示例:

Say you are packaging a SQL file named init.sql in the /resources/ folder of your JAR, that will create a table, something like:

假设您在JAR的/ resources /文件夹中打包一个名为init.sql的SQL文件,它将创建一个表,如:

create table foobar(bazz VARCHAR);
insert into foobar values('foo');
insert into foobar values('bar');
// and so on

Then somewhere in your code, where you need to access the data you will try to load the DB or create it if it does not exists yet.

然后在代码中的某个位置,您需要访问数据,您将尝试加载数据库或创建数据库(如果它尚不存在)。

Connection loadDatabase() throws Exception {
    Class.forName("org.h2.Driver");
    Connection con = null;
    try {
        // throws an exception if the database does not exists
        con = DriverManager.getConnection("jdbc:h2:./foo.db;ifexists=true");
    } catch (SQLException e) {
        // if the database does not exists it will be created
        conn = DriverManager.getConnection("jdbc:h2:./foo.db");
        // init the db
        initDatabase(con);
    }

    return con;
}

void initDatabase(Connection con) throws Exception {
    // load the init.sql script from JAR
    InputStreamReader isr = new InputStreamReader(
        getClass().getResourceAsStream("/resources/init.sql"));
    // run it on the database to create the whole structure, tables and so on
    RunScript.execute(con, isr);
    isr.close();
}

void doSomeStuffWithDb() throws Exception {
    Connection con = loadDatabase();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from foobar");
    // will print foo, then bar
    while (rs.next()) {
        System.out.println(rs.getString(1));
    }
    rs.close();
    stmt.close();
    conn.close();
}

After execution, you should have a file named foo.db next to your application where lies the created tables etc.

执行之后,您的应用程序旁边应该有一个名为foo.db的文件,其中包含创建的表等。

This is a really simple example, of course you can use any wrapper around JDBC to avoid use of ResultSet etc, like spring jdbcTemplate, even to load the connection instance etc.

这是一个非常简单的例子,当然你可以使用JDBC周围的任何包装来避免使用ResultSet等,比如spring jdbcTemplate,甚至可以加载连接实例等。

#2


2  

Use Java DB, then when the user starts the program the first time, load a script to it.

使用Java DB,然后当用户第一次启动程序时,将脚本加载到它。

#3


2  

I would look into h2. It's a great database with many persisting options. Also since you're using Java, I would consider looking into hibernate.

我会调查h2。这是一个很棒的数据库,有许多持久的选项。此外,既然你正在使用Java,我会考虑研究hibernate。

#1


4  

A simple solution would be to use an embedded database stored locally on the disk (for example near the jar file). You can use h2, or even sqlite for this.

一个简单的解决方案是使用本地存储在磁盘上的嵌入式数据库(例如在jar文件附近)。您可以使用h2,甚至是sqlite。

When loaded the program will check for the existence of the database, if it's not here, just play a setup SQL script that will create the database structure and initial data, otherwise you're good to go, just do the stuff that your application was created to do.

加载程序时会检查数据库是否存在,如果不在这里,只需播放一个设置SQL脚本,创建数据库结构和初始数据,否则你很好,只需要做你的应用程序的东西。创造了做。

Here is a really simple example with h2 database:

这是一个非常简单的h2数据库示例:

Say you are packaging a SQL file named init.sql in the /resources/ folder of your JAR, that will create a table, something like:

假设您在JAR的/ resources /文件夹中打包一个名为init.sql的SQL文件,它将创建一个表,如:

create table foobar(bazz VARCHAR);
insert into foobar values('foo');
insert into foobar values('bar');
// and so on

Then somewhere in your code, where you need to access the data you will try to load the DB or create it if it does not exists yet.

然后在代码中的某个位置,您需要访问数据,您将尝试加载数据库或创建数据库(如果它尚不存在)。

Connection loadDatabase() throws Exception {
    Class.forName("org.h2.Driver");
    Connection con = null;
    try {
        // throws an exception if the database does not exists
        con = DriverManager.getConnection("jdbc:h2:./foo.db;ifexists=true");
    } catch (SQLException e) {
        // if the database does not exists it will be created
        conn = DriverManager.getConnection("jdbc:h2:./foo.db");
        // init the db
        initDatabase(con);
    }

    return con;
}

void initDatabase(Connection con) throws Exception {
    // load the init.sql script from JAR
    InputStreamReader isr = new InputStreamReader(
        getClass().getResourceAsStream("/resources/init.sql"));
    // run it on the database to create the whole structure, tables and so on
    RunScript.execute(con, isr);
    isr.close();
}

void doSomeStuffWithDb() throws Exception {
    Connection con = loadDatabase();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from foobar");
    // will print foo, then bar
    while (rs.next()) {
        System.out.println(rs.getString(1));
    }
    rs.close();
    stmt.close();
    conn.close();
}

After execution, you should have a file named foo.db next to your application where lies the created tables etc.

执行之后,您的应用程序旁边应该有一个名为foo.db的文件,其中包含创建的表等。

This is a really simple example, of course you can use any wrapper around JDBC to avoid use of ResultSet etc, like spring jdbcTemplate, even to load the connection instance etc.

这是一个非常简单的例子,当然你可以使用JDBC周围的任何包装来避免使用ResultSet等,比如spring jdbcTemplate,甚至可以加载连接实例等。

#2


2  

Use Java DB, then when the user starts the program the first time, load a script to it.

使用Java DB,然后当用户第一次启动程序时,将脚本加载到它。

#3


2  

I would look into h2. It's a great database with many persisting options. Also since you're using Java, I would consider looking into hibernate.

我会调查h2。这是一个很棒的数据库,有许多持久的选项。此外,既然你正在使用Java,我会考虑研究hibernate。