JDBC addresses this mismatch through the wasNull method As its name implies

Jdbc addresses this mismatch through the wasnull

This preview shows page 255 - 257 out of 475 pages.

JDBC addresses this mismatch through the wasNull( ) method. As its name implies, wasNull( ) returns true if the last value fetched was SQL NULL . For calls returning a Java object, the value will generally be NULL when an SQL NULL is 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. The wasNull( ) method gives you a way to see if that value was NULL in the database. 13.2.2 Error Handling and Clean Up All JDBC method calls can throw SQLException or one of its subclasses if something happens during a database call. Your code should be set up to catch this exception, deal with it, and clean up any database resources that have been allocated. Each of the JDBC classes mentioned so far has a close( ) method associated with it. Practically speaking, however, you only really need to make sure you close things whose calling processes might remain open for a while. In the examples we have seen so far, you only really need to close your database connections. Closing the database connection closes any statements and result sets associated with it automatically. If you intend to leave a connection open for any period of time, however, it is a good idea to close the statements you create using that connection when you finish with them. In the JDBC examples you have seen, this clean up happens in a finally clause. You do this since you want to make sure to close the database connection no matter what happens. only for RuBoard - do not distribute or recompile
Image of page 255
only for RuBoard - do not distribute or recompile 13.3 Dynamic Database Access So 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 database support that JDBC provided, no one could ever write tools like the mysql interactive command-line tool that determines SQL calls at runtime and executes them. The JDBC Statement class provides the execute( ) method for executing SQL that can be either a query or an update. Additionally, ResultSet instances provide runtime information about themselves in the form of an interface called ResultSetMetaData , which you can access via the getMetaData( ) call in the ResultSet . 13.3.1 Metadata The term metadata sounds officious, but it is really nothing more than extra data about some object that would otherwise waste resources if it were actually kept in the object. For example, simple applications do not need the name of the columns associated with a ResultSet —the programmer probably knew that when the code was written. Embedding this extra information in the ResultSet class is thus not considered by JDBC's designers to be part of the core of ResultSet functionality. Data such as the column names, however, is very important to some database programmers—especially those writing dynamic database access. The JDBC designers provide access to this extra information—the metadata—via the ResultSetMetaData interface. This class specifically provides:
Image of page 256
Image of page 257

You've reached the end of your free preview.

Want to read all 475 pages?

  • Fall '19

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture