I want to use Derby embedded in a Java application. The first
time it runs the database won't exist so I'll have to create it
and all the tables it needs. Then, for all subsequent times
it's run, it just runs without creating anything.
Is there a canonical way to do this? Should I always pass the
"create=true" as part of the connection URL and if I get a
warning, don't create the tables? Or try to access the
database, catch an exception about it not existing, then create
Also, if it's not too much trouble, I'm new to using JDBC so I'm
wondering how SQLWarnings are caught. They're derived from
SQLException, but does that mean that SQLWarnings are thrown and
must be caught using a try/catch block? Or are warnings caught
some other way?
> Also, if it's not too much trouble, I'm new to using JDBC so I'm
> wondering how SQLWarnings are caught. They're derived from
> SQLException, but does that mean that SQLWarnings are thrown and
> must be caught using a try/catch block? Or are warnings caught
> some other way?
SQLWarnings are not thrown, even though they are a sub-class of
Exception. I guess you should see that as an implementation detail.
Various JDBC objects have getWarnings() methods in their interfaces,
like Connection, Statement and ResultSet. You need to call these methods
after an operation and see if any warnings exist. Null is returned for
no warnings. Eg. call conn.getWarnings() after creating a Connection.
A word of warning :-), SQLWarnings and SQLExceptions are chained using
getNextWarning and getNextException. For Derby the most information is
obtained when the full chain is displayed, in some situations the actual
cause of an error can be several levels down in the exception chain.