开发者

If there is a necessary data base

开发者 https://www.devze.com 2023-01-02 05:50 出处:网络
I have a desktop program which uses an embedded data base mechanism. For th开发者_运维技巧e first time a user will execute a program, it must create a database. So that, next time there is a database

I have a desktop program which uses an embedded data base mechanism. For th开发者_运维技巧e first time a user will execute a program, it must create a database. So that, next time there is a database and there is no need to create it.

How do I check if there is database and create it if necessary?


Use the create=true attribute in the JDBC URL, like this:

Connection conn = DriverManager.getConnection("jdbc:derby:sampleDB;create=true");

See the Apache Derby Reference Manual:

create=true attribute

Creates the standard database specified within the database connection URL Derby system and then connects to it. If the database cannot be created, the error appears in the error log and the connection attempt fails with an SQLException indicating that the database cannot be found.

If the database already exists, creates a connection to the existing database and an SQLWarning is issued.

You probably have to catch the SQLWarning (this is an exception) but you could safely ignore it.


I'm make creating the database part of the installation script so you can always count on it being there.

Ok, but how to do it so that it would work on any platform?

If you're using Derby and Java, it'll work on every platform. You'll just have to write different installation scripts: .bat or .cmd for Windows, .sh for Linux and Mac. Not too strenuous.

May be there is a way to check if there is one and if there is no create it?

Try making a JDBC connection. If it fails, execute the DDL SQL.

0

精彩评论

暂无评论...
验证码 换一张
取 消