Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information JDC Resources DukeDollars

Java Cup Logo

JDC Home Page

JDC Applets
(chat, newsreader, calendar)

Log Out

Online Training
shadowSearchFAQFeedback

Course Notes Table of Contents | Exercises
JDBC Short Course Index | Online Training Index

Help: Selecting
Working Applet | Help
Solution

Help is available for each task, or you can go straight to the source code, which is one particular solution.

Task 1

Unzip NetCharts into your current directory.

To see if Netcharts installed properly run javap NFBarchartApp to see if it finds the Bar-Chart class you are going to use.

Task 2

Copy the odbc.datasource file from the previous exercise.

Task 3

Import the JDBC package.

import java.sql.*;

Task 4

Load the sun.jdbc.odbc.JdbcOdbcDriver class.

Use Class.forName to load it, and check for exceptions.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Task 5

Create a Connection to the DriverManager.

Use the resources read in from the odbc.datasource resource file.

Connection con = DriverManager.getConnection(
    url, username, password);

Task 6

Create a Statement to perform the query.

Use Connection.createStatement to do this.

Task 7

Execute a query to select everything from the students table and get the results.

Use Statement.executeQuery to send the SQL SELECT statement. The results will come back as a ResultSet.

If you order the results by name,

ResultSet result = select.executeQuery(
    "SELECT * FROM students order by name");

Task 8

Find out in which columns the name, class, and grade fields are located.

The ResultSet.findColumn method results in quicker result fetching than using the column names with the various getString, getInteger, ... methods.

Task 9

Gather results.

Use ResultSet.next to get each row of the result set.

Task 10

Generate a bar chart from the results. A helper routine, showChartData, is provided, which requires two comma-delimited strings. The first, names, is a list of names, while the other, averages, is a list of their averages. You need to keep each individual's entries at the same position in each list.

Note: The names and averages variables in the skeleton are StringBuffer variables, not String for performance reasons. StringBuffer.append is much quicker than using the "+" with Strings.

For instance, the first String should be something like: "John,Mary,Tom". All the code to generate the chart and put on the screen is included. All you have to do is setup the variables names and averages.

showChartData (names, averages);

Task 11

Close the statement.

Task 12

Close the connection.



Copyright © 1997 MageLang Institute. All Rights Reserved
May-97 Copyright © 1996, 1997 Sun Microsystems Inc. All Rights Reserved