![]() |
Cloudscape Database Applications
|
|
Handling SQLExceptionsHelloWorldApp lacks two basic steps to make it a real embedded Cloudscape application: SQLException handling and shutting down Cloudscape. As you learned in Lesson 2, "Cloudscape Basics and the Sample Database", it is important for an application to shut down Cloudscape before exiting the JVM so that Cloudscape can issue a checkpoint. Methods that access a Cloudscape database can throw SQLExceptions if something goes wrong during database access. Applications should handle SQLExceptions to get the details. For example, if the SQL-J statement specifies columns that do not exist, an SQLException is thrown. Figure 5-6 How HelloWorldApp needs to be modified The catch block of HelloWorldApp should treat exceptions in a more sophisticated manner and thus make HelloWorldApp a real Cloudscape application. HelloWorldExc is a duplicate of HelloWorldApp, except that it makes the additions we identified in Figure 5-6:
Change the logSeverityLevel PropertyYou have already been working with one Cloudscape property, which is a configuration parameter for a Cloudscape database or application session. You have been setting the cloudscape.system.home property as a command-line option to the JVM every time you have run an application. You can also set a property in a file called cloudscape.properties. This file is not included with the product; you must create it and edit it yourself. This file is one of the files from the /demo/programs/tours/scripts directory that you copied into your home or working directory. In this task you will set the value of the cloudscape.stream.error.logSeverityLevel property so that you can see all errors written to the information log, regardless of their severity.
Compile, Run, and Examine HelloWorldExc
A successful shutdown command always raises an SQLException in Cloudscape. This may seem counterintuitive, but it does this to let you know that no connection is available. The catch block checks that the SQLException was issued correctly but does not process it. Figure 5-7 Catching the SQLException The main catch block catches exceptions, as shown in Figure 5-8. If an exception is an instance of an SQLException, it processes the exception to display information about it. Figure 5-8 Processing SQLExceptions. SQLExceptions return information about the SQLState and the ErrorCode. SQLStates are standard, ISO-defined codes describing the nature of the error. ErrorCodes are vendor-specific codes. In Cloudscape, these codes describe the severity of the error. In addition, the JDBC specification states that a single error can generate more than one SQLException; an SQLException object can be "chained." Use a loop and the getNextException method to process all SQLExceptions in the chain. Cloudscape SQLExceptions use SQLState class codes starting with X. Cloudscape returns standard SQLState values or exceptions where appropriate. Closing Statements and ResultSetsHelloWorldExc explicitly closes Connections, Statements, and ResultSets when they are no longer needed. Connections to Cloudscape are resources external to an application, and the garbage collector may not close them automatically. |
|||
|
![]() Cloudscape Version 3.6 For information about Cloudscape technical support, go to: www.cloudscape.com/support/.Copyright © 1998, 1999, 2000 Informix Software, Inc. All rights reserved. |