package TestIDL; import java.sql.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; public class JIApplet extends Applet implements ActionListener { Panel north, center; TextField searchCriteria; Button searchButton, clearButton; List searchResults; int connectionId; JI orblet; public void init() { String orbName; // 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 { // initialize the name of the remote object orbName = "jdbcidl"; // create a name component for the object NameComponent nc = new NameComponent(orbName, ""); NameComponent path[] = {nc}; // connect to the ORB on the applet's host ORB orb = ORB.init(this, null); // access the ORB's Name Service NamingContext nctxt = NamingContextHelper.narrow( orb.resolve_initial_references("NameService")); // obtain a reference to the remote object orblet = JIHelper.narrow(nctxt.resolve(path)); } catch (Exception e) { showStatus("Cannot connect to ORB for jdbcidl"); return; } try { connectionId = orblet.openDatabase(); if (connectionId == -1) { searchResults.addItem("Cannot open database connection."); } } catch (Exception ex) { searchResults.addItem("Cannot open database connection."); } } public void finalize() { try { orblet.closeDatabase(connectionId); } catch (Exception ex) {} } /* 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(); orblet.performSearch(connectionId, searchString); result = orblet.getNextRow(connectionId); if (result.equals("")) { showStatus("No rows found using specified search criteria."); return; } while (!result.equals("")) { searchResults.addItem(result); result = orblet.getNextRow(connectionId); } } }