rockcairn logoRockCairn.com rockcairn logo
/**********************************************************************
* Written by Aaron Penner
*
* CU-Denver
* CSC559 Database Systems
* FaLL 1999 Semester
*
**********************************************************************/

// Import the JDBC classes
import java.sql.*;

// Import the java classes used in applets
import java.awt.*;
import java.io.*;
import java.util.*;

public class JdbcProject extends java.applet.Applet
{
// The driver to load
static final String driver_class = "oracle.jdbc.driver.OracleDriver";

// The connect string
static final String connect_string =
"jdbc:oracle:thin:apenner/csc4240@carbon.cudenver.edu:1521:oracle";
static final String part_conn_string = "carbon.cudenver.edu";

// The button to push for executing the query
Button execute_button;

//pop-up list of choices
Choice loc_choice; // Select a location
Choice arr_dep_choice; // Select departures or arrivals
Choice airline_choice; // Select an airlines

//labels for the choices
Label loc_label; // location label
Label arr_dep_label; // arr_dep label
Label airline_label; // airline label

// The place where to dump the query result
TextArea output;

// The connection to the database
Connection conn;

// Create the User Interface
public void init ()
{
this.setLayout (new BorderLayout ());
Panel cp = new Panel ();
cp.setLayout (new GridLayout(0,3));
setFont(new Font("Helvetica", Font.BOLD, 12));
Panel p = new Panel ();
p.setLayout (new FlowLayout (FlowLayout.LEFT));


// Create labels for choces
loc_label = new Label("Location");
cp.add (loc_label);
arr_dep_label = new Label("Arrival/Departure");
cp.add (arr_dep_label);
airline_label = new Label("Airlines");
cp.add (airline_label);


// Create location choice
loc_choice = new Choice();
loc_choice.addItem("DEN");
loc_choice.addItem("JFK");
loc_choice.addItem("LAX");
cp.add (loc_choice);

// Create departure or arrival choice
arr_dep_choice = new Choice();
arr_dep_choice.addItem("Arrivals");
arr_dep_choice.addItem("Departures");
cp.add (arr_dep_choice);

// Create airline choice
airline_choice = new Choice();
airline_choice.addItem("American");
airline_choice.addItem("Continental");
airline_choice.addItem("United");
cp.add (airline_choice);

execute_button = new Button ("Run Query");
cp.add (execute_button);

// Make final GUI attachments
this.add ("North", cp);

output = new TextArea (10, 70);
this.add ("Center", output);

}

// Handle the action events for the Java applet
public boolean action (Event ev, Object arg)
{
if (ev.target == execute_button)
{
try
{
// Clear the output area
output.setText (null);

// See if we need to open the connection to the database
if (conn == null)
{
// Load the JDBC driver
output.appendText ("Loading JDBC driver " + driver_class + "\n");
Class.forName (driver_class);

// Connect to the database
output.appendText ("Connecting to " + part_conn_string + "\n");
conn = DriverManager.getConnection (connect_string);
output.appendText ("Connected\n");
}

// Create the query string from the choice list selections
String dest = new String("select * from " + arr_dep_choice.getSelectedItem() + "_view where " + arr_dep_choice.getSelectedItem() + " = '" + loc_choice.getSelectedItem() + "' and Airline = '" + airline_choice.getSelectedItem() + "'\n");

// Create a statement
Statement stmt = conn.createStatement ();

// Execute the query
output.appendText ("Executing query " + dest + "\n");
ResultSet rset = stmt.executeQuery (dest);

// Write the field headers
if (arr_dep_choice.getSelectedIndex() == 0)
output.appendText ("Date | Arriving_From | Arr_Time | Dep_Time | Airline | Flight_Num | Arriving_In\n");
else
output.appendText ("Date | Arriving_In | Dep_Time | Arr_Time | Airline | Flight_Num | Departing_From\n");

output.appendText ("------------------------------------------------------------------------------\n");

// Print the result rows
while (rset.next ())
output.appendText (rset.getString (1) + " | " + rset.getString(2) + " | " + rset.getString(3) + " | " + rset.getString(4) + " | " + rset.getString(5) + " | " + rset.getString(6) + " | " + rset.getString(7) + "\n");

// Query completed
output.appendText ("done.\n");
}
catch (Exception e)
{
// Oops
output.appendText (e.getMessage () + "\n");
}
return true;
}
else
return false;
}
}