Running for the first time

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Running for the first time

Paul J. Lucas
  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
  it?

  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?

  Thanks.

  - Paul
Reply | Threaded
Open this post in threaded view
|

Re: Running for the first time

Daniel John Debrunner
Paul J. Lucas wrote:


>
>     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.

Eg.

  for (SQLWarning sw = rs.getWarnings(); sw != null; sw =
sw.getNextWarning())
      System.out.println(sw.getSQLState() + " " + sw.getMessage());


Dan.