This preview shows page 255 - 257 out of 475 pages.
JDBC addresses this mismatch through the wasNull( )method. As its name implies, wasNull( )returns trueif the last value fetched wasSQL NULL. For calls returning a Java object, the value will generally be NULLwhen an SQL NULLis read from the database. In these instances,wasNull( )may appear somewhat redundant. For primitive datatypes, however, a valid value—such as 0—may be returned on a fetch. ThewasNull( )method gives you a way to see if that value was NULLin the database.13.2.2 Error Handling and Clean UpAll JDBC method calls can throw SQLExceptionor one of its subclasses if something happens during a database call. Your code should be setup to catch this exception, deal with it, and clean up any database resources that have been allocated. Each of the JDBC classes mentioned sofar has a close( )method associated with it. Practically speaking, however, you only really need to make sure you close things whose callingprocesses might remain open for a while. In the examples we have seen so far, you only really need to close your database connections. Closingthe database connection closes any statements and result sets associated with it automatically. If you intend to leave a connection open for anyperiod of time, however, it is a good idea to close the statements you create using that connection when you finish with them. In the JDBCexamples you have seen, this clean up happens in a finallyclause. You do this since you want to make sure to close the database connectionno matter what happens.only for RuBoard - do not distribute or recompile
only for RuBoard - do not distribute or recompile13.3 Dynamic Database AccessSo far, we have dealt with applications in which you know exactly what needs to be done at compile time. If this were the only kind of databasesupport that JDBC provided, no one could ever write tools like the mysqlinteractive command-line tool that determines SQL calls at runtime andexecutes them. The JDBC Statementclass provides the execute( )method for executing SQL that can be either a query or an update.Additionally, ResultSetinstances provide runtime information about themselves in the form of an interface called ResultSetMetaData, whichyou can access via the getMetaData( )call in the ResultSet.13.3.1 MetadataThe term metadata sounds officious, but it is really nothing more than extra data about some object that would otherwise waste resources if itwere actually kept in the object. For example, simple applications do not need the name of the columns associated with a ResultSet—theprogrammer probably knew that when the code was written. Embedding this extra information in the ResultSetclass is thus not considered byJDBC's designers to be part of the core of ResultSetfunctionality. Data such as the column names, however, is very important to somedatabase programmers—especially those writing dynamic database access. The JDBC designers provide access to this extra information—themetadata—via the ResultSetMetaDatainterface. This class specifically provides: