check if h2 database existed

2,644 views
Skip to first unread message

moshi

unread,
Dec 17, 2009, 1:42:11 PM12/17/09
to H2 Database
Hi,
I want to check if h2 database existed,
if not existed - to create it.
How can I check if existed in java ?
Thanks.

Sergi Vladykin

unread,
Dec 17, 2009, 2:01:00 PM12/17/09
to H2 Database
Hi,
if database doesn't exists it will be created automatically. You
shouldn't do anything.
See also http://h2database.com/html/features.html#database_only_if_exists

moshi

unread,
Dec 19, 2009, 3:53:34 AM12/19/09
to H2 Database
I know that it is created automaticaly, but if I should create it I
want to create it
with specific tables contain specific data. I don't want it just to be
created.
You know what, I don't have a problem that it will be created by
itself, but I want to
notice that my table are missing in the new database, and to create
them. How can I know
that the database doesn't contain a specific table ?
Thanks a lot !

On Dec 17, 9:01 pm, Sergi Vladykin <sergi.vlady...@googlemail.com>
wrote:

Sergi Vladykin

unread,
Dec 19, 2009, 4:24:09 AM12/19/09
to H2 Database
The simplest solution I see is to try connect with ;IFEXISTS=TRUE and
if it fail with error code 90013 (database does not exists) you should
reopen connection without ;IFEXISTS=TRUE and fill it with initial
data. If you want to chek if existing database contains specific table
you should use information_schema. See http://h2database.com/html/grammar.html#information_schema

Tsvetozar

unread,
Dec 19, 2009, 6:28:26 AM12/19/09
to h2-da...@googlegroups.com
From JAVA you may also use DatabaseMetaData to retrieve specific DB info. Check its methods to see what you can do with it. Here's an example how you check if a custom table exists:

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;

public class Main {
public static void main(String[] argv) throws Exception {
Connection c = null; // set your connection here
DatabaseMetaData dbm = c.getMetaData();
ResultSet rs = dbm.getTables(null, null, "employee", null);
if (rs.next()) {
System.out.println("Table exists");
} else {
System.out.println("Table does not exist");
}
}
}

> --
>
> You received this message because you are subscribed to the Google Groups "H2 Database" group.
> To post to this group, send email to h2-da...@googlegroups.com.
> To unsubscribe from this group, send email to h2-database...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.
>
>
>
>

Reply all
Reply to author
Forward
0 new messages