db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kathey Marsden <kmarsdende...@sbcglobal.net>
Subject Re: Create table if not exists?
Date Sat, 21 Feb 2009 01:08:50 GMT
Chris wrote:
> MySQL has a really nice syntax for creating tables if they don't exist 
> already:
>
> CREATE TABLE IF NOT EXISTS foo (...)
>
> How can I get the same functionality in Derby? I haven't been able to 
> figure out the SQL command to check for the existence of a table.
>
>
You could catch the SQLException and ignore it, e.g.
    try {
        s.executeUpdate("CREATE TABLE FOO (I INT)");
       
    } catch (SQLException se) {
        if (!se.getSQLState().equals("X0Y32"))
        throw se;
    }

alternately you can use the DatabaseMetaData to see if the table is there

    DatabaseMetaData dmd = conn.getMetaData();
    ResultSet rs = dmd.getTables(null,"APP", "FOO",null);
    if (!rs.next()) {
        s.executeUpdate("CREATE TABLE FOO (I INT)");
    }


Mime
View raw message