Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click NOW to join Planet IT!
Click NOW to join Planet IT!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Applet: JDBC Airlines

Looking for online airline information? Choose JDBC Airlines! This applet illustrates remote access to a database through a nice user interface. The example runs as an applet within World Wide Web browsers that support Java. It uses JDBC to connect and retrieve flight schedules from a database. No middleware is involved. The JDBC driver used for this example is a 100-percent Java driver that directly connects to the database server. Figure 10-4 shows the JDBC Airlines applet.


Figure 10-4:  Connect Software’s JDBC Airlines applet.

This is an example from Connect Software, Inc. Thanks to its 100-percent Java drivers, this applet is able to run within any Java-compatible WWW browser. Again, no specific classes must be preinstalled on the client machine. The JDBC driver downloads from the Web server along with the applet classes.

The HTML File

The HTML file contains the tag to load the applet as well as parameters that provide connection information to this applet. Such connection information includes the driver to use to connect to the database and the database’s URL.

<html>
<head>
<title>
Airplet, the Airline Applet by Connect Software
</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<img src="images/banner.gif">
<p>
<applet code=airplet.Airplet width=500 height=600>
<param name=driver value=connect.sybase.SybaseDriver>
<param name=connection value='jdbc:sybase://db.mydomain.com:8192/airline;user=guest;password=guest'>
</center>
</applet>
</html>

Airplet.java

Airplet.java is the main applet class. Initialization establishes a connection with the database server. As soon as the connection is established, various panels are prepared and displayed. These panels include a panel with name and preset choices for departure and arrival airports, a panel with search and roundtrip buttons, and a panel with maps and routes between airports.

User events are handled when selecting airport choices and clicking search or roundtrip buttons. A search for flights from the origin to the destination is performed. A method is also provided to load map images from the server.

Listing 10-4 shows the source code for the main part of the applet, Airplet.java.

Listing 10-4: Airplet.java.

//
// Airplet.java - Connect Software's Airline Applet, a.k.a. jdbc airlines
//
// Copyright (C) 1996 by Connect Software. All rights reserved.
//
// Written by Gionata Mettifogo, Peter Ham.
//
// Connect Software, Inc.
// 81 Lansing Street, Suite #411
// San Francisco, CA 94105
// (415) 710-1544 (phone) - (415) 543-6695 (fax)
//
// email: info@connectsw.com - www: http://www.connectsw.com
package airplet; // airplet's package
import java.applet.*; // import a number of java libraries
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import jdbc.sql.*; // import modified jdbc libraries
public class Airplet extends java.applet.Applet // main applet class
{
  private TextField nameTo = null; // names of departure and   arrival airports
  private TextField nameFrom = null;
  private AirportChoice choiceTo = null; // preset lists of   departure and arrival airports
  private AirportChoice choiceFrom = null;
  private FlightsPanel panelFlights = null; // panel containing   maps and flights listings
  /** Initialize the applet, opens the connection with the database and add user interface
  items in the applet. */
  synchronized public void init()
  {
       airplet = this; // static reference to this airplet
       setBackground(Color.white); // white background for this applet
       LayoutManager columnLayout = new ColumnLayout(5,5); // all panels go in a
         single column
       setLayout(columnLayout);
       showStatus("Connecting..."); // let user know we're
connecting to the database
       try
       {
            String driver = getParameter("driver"); // use sql driver specified in
               'driver' parameter
            if(driver != null) Class.forName(driver).newInstance(); // register driver
               with DriverManager
            String url = getParameter("connection"); // get connection's url
            connection = DriverManager.getConnection(url); // establish connection
               with server
       }
       catch(Exception sqlEx)
       {
            System.out.println("Connection failed because " + sqlEx + "\n");
       }
       showStatus("Preparing...");
       try
       {
            Panel panelFrom = new Panel(); // create panel with name and preset
               choices for departure's airport
            ImageCanvas imageFrom = new
            ImageCanvas("images/airFrom.gif"); panelFrom.add(imageFrom);
            nameFrom = new TextField(25); panelFrom.add(nameFrom);
            choiceFrom = new AirportChoice(); panelFrom.add(choiceFrom);
            Panel panelTo = new Panel(); // create panel with name and preset
            choices for arrival's airport
            ImageCanvas imageTo = new ImageCanvas("images/airTo.gif");
              panelTo.add(imageTo);
            nameTo = new TextField(25); panelTo.add(nameTo);
            choiceTo = new AirportChoice(); panelTo.add(choiceTo);
            Panel panelButtons = new Panel(); // panel with search and roundtrip
               buttons
            Button button1 = new Button("Search"); panelButtons.add(button1);
            Button button2 = new Button("Roundtrip");             panelButtons.add(button2);
            panelFlights = new FlightsPanel(); // panel with maps and routes
            add(panelFrom); // add all panels to the applet
            add(panelTo);
            add(panelButtons);
            add(panelFlights);
            showStatus("Connect Software, 1996."); // here we are!
       }
       catch(SQLException sqlEx) { showStatus("Sorry, could not initialize, e-mail
       support@connectsw.com"); }
  }
  /** Responds to user selecting an airport in the choice menus or clicking search or
  roundtrip. */
  public boolean action(Event iEvent,Object iArgument)
  {
       if(iEvent.target == choiceFrom) // user picked an origin from the choices
       {
            Airport air = choiceFrom.getSelectedAirport();
            nameFrom.setText(air.getName()); // copy origin's name to origin's text
            field
            return true;
       }
       if(iEvent.target == choiceTo) // user picked a destination from the choices
       {
       Airport air = choiceTo.getSelectedAirport();
       nameTo.setText(air.getName()); // copy destination's name to destination's text
         field
       }
       if(iArgument.equals("Search") || iArgument.equals("Roundtrip")) // search for
         flights from origin to destination
       {
            String airFrom = nameFrom.getText();
            if(airFrom.length() < 1) airFrom = choiceFrom.getSelectedItem();
            String airTo = nameTo.getText();
            if(airTo.length() < 1) airTo = choiceTo.getSelectedItem();
       searchFlights(airFrom,airTo,iArgument.equals("Roundtrip"));
return true;
       }
       return super.action(iEvent,iArgument); // event was handled
  }
  private void searchFlights(String departingFrom,String arrivingTo,boolean roundTrip)
  {
       try
       {
            // System.out.println("Airplet.searchFlights - from " + departingFrom + "
            to " + arrivingTo);
            Airport airFrom = Airport.getAirport(departingFrom); // find out more
              about departing airport
            Airport airTo = Airport.getAirport(arrivingTo); // find out more about
              arriving airport
            if(airFrom != null && airTo != null) // if both airports where found (and
              they are different)
            {
                 nameFrom.setText(airFrom.getName()); // show complete name
                   and code for departure airport
                 choiceFrom.select(airFrom.getCode());
            nameTo.setText(airTo.getName()); // show complete name and
              code for arrival airport
            choiceTo.select(airTo.getCode());
            if(roundTrip) // if user requested return trip
            {
                 panelFlights.setAirports(airTo,airFrom); // show
                   inverse route
                 }
                 else panelFlights.setAirports(airFrom, airTo); // show
                   normal route
                 layout();
                 }
            }
            catch(SQLException sqlEx) { panelFlights.setText(sqlEx.toString());
       }
  }
  static Statement createStatement() throws SQLException
  {
  return connection.createStatement();
  }
       private static Connection connection = null; // connection to the airline database
       /** Loads given image from the network, or file system, and returns it. */
       static Image loadImage(String iName)
       {
            if(images == null) // if there's no hash table for images yet
            {
            images = new Hashtable(); // create an empty hash table
            }
       Image image = (Image) images.get(iName); // try to get image from the hash table
       (hash is image's name)
       if(image == null) // if this image hasn't been loaded yet
       {
            try // catch all loading problems
            {
                 URL url = new URL(airplet.getDocumentBase(), iName); //
                   create url of image on web server or local file system
                 image = airplet.getImage(url); // try to load image
                 airplet.prepareImage(image,airplet);
            }
            catch(Exception e) { }
            if(image != null) // if image was loaded
            {
                 images.put(iName,image); // add it to the hash table so next
                   time we don't have to load it
                 }
            }
       return image; // return the image
  }
  static private Hashtable images = null; // an hash table of loaded images
  static private Airplet airplet = null; // a static reference to this applet (there's only one
  instance of it running at any time)
}


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.