Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

jGuru: Generalizing Connection Information - Batch

 


[Help | API Docs | Short Course| Exercises]

The first course examples hardcoded connection information. This helped to keep things simple at the price of program changes and recompilation for different databases -- hardly the Java ideal! In this exercise, a more general method of obtaining the information without user intervention is provided via a ResourceBundle backed up by a properties file. While a normal property file and explicit file I/O could be used, ResourceBundles do all of the work for the programmer implicitly. Additionally, the ResourceBundle can be backed up by either a PropertyResourceBundle or a ListResourceBundle with no code changes. The localization features can also allow interesting ( if somewhat bizarre to an i18n programmer, ) possibilities. Keep in mind that there are other alternatives that you may prefer, for example, passing arguments in the classic manner.

"Batch," is a term familiar in the mainframe and midrange worlds, used here as a shortcut to mean "a program run without user intervention." This is directly opposed to an interactive program. The important point is that in this scenario, *all* information necessary to the application must be provided without involving an end user.

Prerequisites

Skeleton Code

Tasks

  1. Given the following ConnectU.properties file:

    #PropertiesResourceBundle for Connection Properties
    CSDriver=COM.cloudscape.core.RmiJdbcDriver
    CSURL=jdbc:cloudscape:rmi:jGuru
    CSUserID=sa
    CSPassword=admin
    

    Set up String variables to access the keys and to hold the returned values. Also set up Strings 1) containing the name of the properties file - ConnectU, and 2) containing a SELECT statement to get the single row having MoJava in its Type column.

  2. Access the ResourceBundle and retrieve the driver name, databaseURL, userID and password.

  3. Load the driver.

  4. Get a Connection; get a DatabaseMetaData object using dbmd and report the DBMS and driver name and version; create a Statement.

  5. Execute the query. If data was returned, get the ResultSetMetadata ColumnName, ColumnTypeName and ColumnClassName and report them for each column in the ResultSet.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

The following files contain a complete implementation of the exercise demonstrating generalized connection information for a program requiring no user intervention:

Demonstration

ConnectU should connect to the jGuru database using externally provided information; get and report DBMS and driver name and version; select one record from the JJJJData table; report the name, SQL type name and Java class equivalent for each column in the table as follows:

DBMS: DBMS:cloudscape, 3.0.4
Driver: Cloudscape Embedded JDBC Driver, 3.0

Column Name: ENTRY.
Column SQL Type: INT.
Column Java Class Equivalent: java.lang.Integer.

Column Name: CUSTOMER.
Column SQL Type: VARCHAR.
Column Java Class Equivalent: java.lang.String.

Column Name: DOW.
Column SQL Type: VARCHAR.
Column Java Class Equivalent: java.lang.String.

Column Name: CUPS.
Column SQL Type: INT.
Column Java Class Equivalent: java.lang.Integer.

Column Name: TYPE.
Column SQL Type: VARCHAR.
Column Java Class Equivalent: java.lang.String.

Next Exercise

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.