db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Cecchet <m...@frogthinker.org>
Subject Re: Create table if not exists?
Date Sat, 21 Feb 2009 04:11:12 GMT
Kathey Marsden wrote:
> alternately you can use the DatabaseMetaData to see if the table is there
I don't know if this is still the case, but Derby could only do that in 
a transaction so make sure you call conn.setAutoCommit(false) before.
>    DatabaseMetaData dmd = conn.getMetaData();
>    ResultSet rs = dmd.getTables(null,"APP", "FOO",null);
If you just want the table (not views or system tables), you should 
specify it using:
ResultSet rs = dmd.getTables(null,"APP", "FOO", new String[]{"TABLE"});
>    if (!rs.next()) {
>        s.executeUpdate("CREATE TABLE FOO (I INT)");
>    }
This is probably the cleanest method especially if you want this DDL 
statement to be part of a larger transaction, not triggering exception 
in the middle is usually much better.

Hope this helps,

Emmanuel Cecchet
FTO @ Frog Thinker 
Open Source Development & Consulting
Web: http://www.frogthinker.org
email: manu@frogthinker.org
Skype: emmanuel_cecchet

View raw message