|
[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. PrerequisitesSkeleton CodeTasksGiven the following ConnectU.properties file:
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. Access the ResourceBundle and retrieve the driver name, databaseURL, userID and password. Load the driver. Get a Connection; get a DatabaseMetaData object using dbmd and report the DBMS and driver name and version; create a Statement. 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 SourceThe following files contain a complete implementation of the exercise demonstrating generalized connection information for a program requiring no user intervention: DemonstrationConnectU 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:
Next ExerciseExercisesShort CourseCopyright 1996-2000 jGuru.com. All Rights Reserved. | ||||||
|
| ||||||||||||