|
[Help | API Docs | Short Course| Exercises] This exercise reimplements the previous application with the ability to dynamically display table data in a form appropriate to present to an end user using a Swing GUI ( you do not have to know Swing for this excercise, the code is done for you. ) While there is a fair amount of code, most of it is boilerplate, both for the Swing and JDBC portions. Note that this program is not appropriate for large amounts of data, but provides a basic tool for displaying the results of the exercises in the course. PrerequisitesSkeleton CodeTasksGiven the following ConnectJ.properties file: #PropertiesResourceBundle for Connection Properties CSDriver=COM.cloudscape.core.RmiJdbcDriver CSURL=jdbc:cloudscape:rmi:jGuru 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 - ConnectJ, and 2) containing a SELECT statement to get all columns and rows, but omit the table name, which will be obtained and appended later in the program. Access the ResourceBundle and retrieve the driver name and databaseURL. Display a GUI form which requests a userID,password and table name, has a "Connect" button and a textarea. This is done for you. When the button is clicked, in actionPerformed(), verify that a table name was entered ( from jtTable, ) get the userID ( from jtUserID ) and password ( from jpfPassword ) keyed, reset the text area ( jta ) and invoke doConnect(). In doConnect(), the driver is loaded, a Connection obtained and a Statement created as before. Your job: concatenate the query string and table name, then execute the query. In doConnect(), a boolean, a StringBuffer and a while( rs.next() loop are provided. Inside the loop, an if( bFirst ) section to determine the first time through the loop is set up. Your job: use ResultSetMetadata to get and display ColumnCount number of ColumnLabels in a single String in the text area jta. Reset the StringBuffer and ensure that this process only happens once per while loop. In doConnect(), on every pass of the while loop, obtain ColumnCount number of columns for the row using ResultSet.getObject() and concatenate these into a single String. getObject(), as used here, will obtain a String from virtually any type returned from the database. Append the resulting String to the text area jta and reset the StringBuffer. 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 both external and user supplied information: DemonstrationConnectJ should present a form with entry fields at the top for userID and password information, and an entry field for a table name next to the button in the center; After keying this information, upon clicking the "Connect" button the application should connect to the jGuru database using both externally and user provided information; select all records and columns from the requested table ( wth JJJData as the default; ) display the column names and rows in a text area at the bottom of the form.
The display should look like this:
Next ExerciseExercisesShort CourseCopyright 1996-2000 jGuru.com. All Rights Reserved. | ||||
|
| ||||||||||||