import java.sql.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.rmi.*; import java.rmi.registry.*; public class JRApplet extends Applet implements ActionListener { Panel north, center; TextField searchCriteria; Button searchButton, clearButton; List searchResults; int connectionId; JR servlet; public void init() { String registryName; // build the applet display setLayout(new BorderLayout()); north = new Panel(); north.setLayout(new FlowLayout()); north.add(new Label("Search Criteria:")); searchCriteria = new TextField(40); north.add(searchCriteria); searchButton = new Button("Search"); searchButton.addActionListener(this); north.add(searchButton); clearButton = new Button("Clear"); clearButton.addActionListener(this); north.add(clearButton); add("North", north); searchResults = new List(); add("Center", searchResults); validate(); setVisible(true); try { // get the host name where the applet was loaded registryName = "//" + getCodeBase().getHost() + "/"; // append the name of the remote object within the registry registryName += "jdbcrmi"; // obtain a reference to the remote object servlet = (JR)Naming.lookup(registryName); } catch (Exception e) { showStatus("Cannot connect to RMI registry for jdbcrmi"); return; } try { connectionId = servlet.openDatabase(); if (connectionId == -1) { searchResults.addItem("Cannot open database connection."); } } catch (Exception ex) { searchResults.addItem("Cannot open database connection."); } } public void finalize() { try { servlet.closeDatabase(connectionId); } catch (Exception ex) {} super.destroy(); } /* handle applet's action events */ public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Search")) { try { performSearch(); } catch (Exception ex) { showStatus(ex.toString() + ": Can't issue search"); } } else { clearListBox(); } return; } /** Clear the contents of the list box */ public void clearListBox() { searchResults.removeAll(); } /** Issue a SQL query with a WHERE clause */ public void performSearch() throws Exception { String searchString; // SQL where clause String result; // Row retrieved from query clearListBox(); searchString = searchCriteria.getText(); servlet.performSearch(connectionId, searchString); result = servlet.getNextRow(connectionId); if (result == null) { showStatus("No rows found using specified search criteria."); return; } while (result != null) { searchResults.addItem(result); result = servlet.getNextRow(connectionId); } } }