Database search applet


File McKoiApplet.java

The program selects all data from the table Students of the database and prints it out in the text field of the graphical interface.

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

public class McKoiApplet extends JApplet {
    JButton button;
    JTextArea text;
    McKoiApplet me;

    public void init() {
	//WindowUtilities.setNativeLookAndFeel();
	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...");

	   try {
	       //System.out.println("I am here");
	       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();
           
	     // using executeQuery():
	     ResultSet rs = stmt.executeQuery("SELECT * FROM Students ");

	     text.setText("");
	     // moving forward in the result set:
	     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