Database applet with a parameter


File McKoiAppletParam.java

The program performs search by first name using the string passed as a parameter via field StudentName of the <EMBED> tag. The method getParameter() of the class Applet retrieves the parameter. The results are displayed in the text field "text" of the graphical interface.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

public class McKoiAppletParam extends JApplet {
    JButton button;
    JTextArea text;
    McKoiAppletParam me;
    String searchFor;

    public void init() {
	// Reading the value of parameter:
	searchFor = getParameter("StudentName");

	Container content = getContentPane();
	content.setBackground(Color.blue);
	content.setLayout(new FlowLayout());

	button = new JButton("GO");
	content.add(button);
	button.addActionListener(new ButtonListener());

	text = new JTextArea(10, 60);
	text.setLineWrap(true);
	content.add(text);
	
	me = this;
    }


    public void getResults() {
	text.setText("Wait a minute... Looking for " + searchFor + "\n");

	   try {
	       Class.forName("com.mckoi.JDBCDriver").newInstance();
           }
           catch (Exception e) {
             text.setText(
               "Unable to register the JDBC Driver.\n" +
               "Make sure the JDBC driver is in the\n" +
               "classpath.\n");
           }

           // This URL specifies we are connecting with a database server
           // on localhost.
           String url = "jdbc:mckoi://birch/";

           // The username / password to connect under.
           String username = "cs349";
           String password = "cs349";

           // Make a connection with the database.
           Connection con;
           try {
             con = DriverManager.getConnection(url, username, password);
           }
           catch (SQLException e) {
             System.out.println(
               "Unable to make a connection to the database.\n" +
               "The reason: " + e.getMessage());
             return;
           }

           try {
	       
	     Statement stmt = con.createStatement();
           
	     // store the query in a string
	     String searchString = "SELECT * FROM Students WHERE First_Name";
	     searchString = searchString + " LIKE \'" + searchFor + "%\'";

	     ResultSet rs = stmt.executeQuery(searchString);
	     if (rs.next() == false)
		 text.setText("No results to your query");
	     else {
		 rs.previous(); // move the cursor back to the beginning

		 text.setText("");
		 // the results are displayed in the text field of the applet
		 while (rs.next()) {
		     int id = rs.getInt("ID");
		     String first = rs.getString("First_Name");
		     String last = rs.getString("Last_Name");
		     String year = rs.getString("Year");
		     float gpa = rs.getFloat("GPA");
		     Date date = rs.getDate("Date_of_birth");
		     text.append(id + "   " + first + "   " + last + "   ");
		     text.append(year + "   " +  gpa + "   " + date + "\n");
		 }
	     }

             // Close the connection when finished
             con.close();

           }
           catch (SQLException e) {
             text.setText(
               "An error occured\n" +
               "The SQLException message is: " + e.getMessage());
             return;
	   }
    }

    class ButtonListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    me.getResults();
	}

    }
}


This page has been created and is maintained by Elena Machkasova
Comments and suggestions are welcome at emachkas@wellesley.edu

Spring Semester 2002