使用Java .jar文件提供数据库? [英] Supplying a database with a Java .jar file?

查看:168
本文介绍了使用Java .jar文件提供数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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文件附近)。您可以使用 h2 ,甚至 sqlite 为此。

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.

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

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.

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

Here is a really simple example with h2 database:

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

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

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

执行后,你应该有一个名为 foo的文件.db 你的应用程序旁边的创建表等等。

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

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

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.

这篇关于使用Java .jar文件提供数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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