|
[Exercise | API Docs | Short Course| Exercises] Help is available for each task. Task 1Import the java.sql package, which is required for the JDBC API. import java.sql.*; Task 2Load the JDBC driver class.
As mentioned in the text, the form of the code used above has the best chance of working on all platforms. If you aren't concerned with portability and plan to always use the same DBMS ( these are famous last words, ) and know that Class.forName() alone will work, feel free to use that form. Task 3Get a Connection and create a Statement, using jdbc:cloudscape:rmi:jGuru;create=true as the DatabaseURL for Cloudscape.
Notice that programs should always close Connections and Statements when their work has been done and certainly before exiting the program. Task 4If a previous copy of JJJJData exists, remove it from the database.
This aspect was not mentioned in the text, but if the program was rerun without removing the existing table, severe problems would be encountered when executing the CREATE TABLE statement. Be sure you understand that DROP TABLE completely removes the table from the database with no questions asked. The assumption here is that any exception encountered arises because the table does not exist. This is not a good assumption for a production quality program. Task 5Create the JJJJData table and insert the data.
The CREATE TABLE SQL statement is used to define the column names and types, to define a primary key, and to create the JJJJData table.
The data, complete with enclosing parentheses, is defined in an array named SQLData. At this point, the array is walked and the columns are inserted for each row using executeUpdate(). Note that the Statement object can be reused for multiple SQL statements and requests, and that it, in this form, is perfectly dynamic. executeUpdate() returns a count for rows affected, and we use this to accumulate and print the total number of rows inserted.
A typical try, catch, finally block is used to ensure that the Statement and the Connection get closed. This ensures that database resources aren't locked up. Be sure to understand the order, it's like a sandwich: get Connection, create Statement, ... close Statement, close Connection. Copyright 1996-2000 jGuru.com. All Rights Reserved. | |||||||||||||||
|
| ||||||||||||