|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 4
// Database connection supposedly // already made. Connection conn; CallableStatement aStatement = null;
try { // Create the database callable statement. // For example, assume that the "<Some_SQL_Procedure_call>" // is something like // // Scenario 3: "{? = call calculate_living_costs()}" // where the '?' denotes an output parameter that should // be interpreted as a java.sql.ResultSet. If you use this syntax // you will need to register the output parameter, as per scenario // (1) and (2). For some drivers, you could use the same syntax as // scenario four below: // // Scenario 4: "{call calculate_living_costs()}" // The difference is essentially that the database procedure in // the fourth scenario hides any return parameters. // From a JDBC standpoint, however, the code is similar. aStatement = conn.prepareCall("<Some_SQL_Procedure_call>"); // Thankfully, no need to register a ResultSet output parameter // as the return value of the stored procedure is a CURSOR/ResultSet. // // Most calls to procedures of this type would handle only single // ResultSets return values, though... // // Call the procedure in the database ResultSet valueReceived = aStatement.executeQuery(); // Handle the received ResultSet while(valueReceived.next()) { // ... } } catch(Exception ex) { // Log an error message System.err.println("Database communication error: " + ex); }
How have various driver manufacturers approached the solution to the ResultSet CallableStatement scenario? Here are three examples that assume that a single ResultSet will return (multiple ResultSet scenarios will be covered in the next installment of this series):
| Database | ResultSet CallableStatement scenario solution |
|---|---|
| Oracle8i |
// The scenario requires importing some oracle driver types import oracle.jdbc.driver.*; |
| Sybase Enterprise Server and Microsoft SQL Server 7 |
// The two Transact-SQL databases use a similar technique // to handle the ResultSet CallableStatement scenario: // // Only a single ResultSet may be returned in one call, // unless lots of non-standard functionality should be used. CallableStatement aStatement; ResultSet valueReceived; |
| Informix |
// The Informix way to handle the ResultSet // CallableStatement scenario conforms to the way of // Sybase and Microsoft above. |
Table 6. Various database products and their solutions to the ResultSet CallableStatement scenario
Although the last two examples in the table above might look easy enough to handle from the standpoint of Java application
development, note that none of them have dealt with multiple ResultSets returning from a single procedure call.
I've described two of the three principal ways of calling a database to extract information, using the JDBC API: the standard
ResultSet and standard Callable Statement scenarios (the third, which I haven't covered, is the class hierarchy in the java.sql package). However, for industrial-strength system performance, you can gain better performance and ease of maintenance by using a
combination of these two scenarios. Developers making ample use of the ResultSet CallableStatement scenario can essentially build a database application API that is called from the Java server application, thus encapsulating
the physical database layout.
The price you pay when employing this combined scenario is a lack of thorough calling standards; thus, in a small way, you are abandoning the JDBC's database independence.
The authors of this month's server-side Java computing articles will be holding a free online seminar on January 13 at 11:00 a.m. PST. Register to join at http://seminars.jguru.com
Read more about Enterprise Java in JavaWorld's Enterprise Java section.
Server-side Java: Read the whole series -archived on JavaWorld