为数据库提供 Java .jar 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12961133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Supplying a database with a Java .jar file?
提问by Click Upvote
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.
db 应该本地存储在用户的计算机上。
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 文件?
回答by Alex
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 sqlitefor 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:
假设您要打包一个init.sql
在/resources/
JAR 文件夹中命名的 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,甚至加载连接实例等。