/* * This sample applet just selects 'Hello World' and the date from the database */ // Import the JDBC classes import java.sql.*; // Import the java classes used in applets import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; public class JdbcApplet 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:scott/tiger@amber:1521:orcl"; // The query we will execute static String query = "select 'Hello JDBC: ' || sysdate from dual"; // The button to push for executing the query Button execute_button; // The place to obtain a new query. TextField queryInput; // The place where to dump the query result TextArea output; // The connection to the database Connection conn; // Create the User Interface public void init () { connect_string = getParameter("CONNECT"); this.setLayout (new BorderLayout ()); Panel p = new Panel (); p.setLayout (new FlowLayout (FlowLayout.LEFT)); execute_button = new Button ("Execute"); p.add (execute_button); this.add ("South", p); output = new TextArea (10, 60); this.add ("Center", output); queryInput = new TextField( query ) ; this.add("North", queryInput); } // Do the work public boolean action (Event ev, Object arg) { if (ev.target == execute_button) { try { // Clear the output area output.setText (""); // 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 databse output.appendText ("Connecting to " + connect_string + "\n"); conn = DriverManager.getConnection (connect_string); output.appendText ("Connected\n"); } // Create a statement Statement stmt = conn.createStatement (); // Execute the query if (queryInput.getText().length() > 0) { query = queryInput.getText(); output.appendText ("Executing query " + query + "\n"); ResultSet rset = stmt.executeQuery (query); // Dump the result displayResults(rset); } else { output.appendText("no query to execute\n"); } // We're done output.appendText ("done.\n"); } catch (Exception e) { // Oops output.appendText (e.getMessage () + "\n"); e.printStackTrace(); } return true; } else return false; } private void displayResults(ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); int numcols = meta.getColumnCount(); int numrows = 0; Panel disp = new Panel(); output.appendText ("Identifying columns.\n"); for(int i = 1; i <= numcols; i++) { disp.add(new Label(meta.getColumnLabel(i))); } output.appendText ("Building rows.\n"); boolean more = rs.next(); while (more) { for (int i=1; i <= numcols; i++) { disp.add(new TextField(rs.getString(i))); } output.appendText ("+"); numrows ++; more = rs.next(); } disp.setLayout( new GridLayout(numrows+1, numcols)); output.appendText ("\nBuilding Window\n"); Frame dlg = new Frame("Results"); dlg.addWindowListener( new WindowCloser(dlg) ); dlg.add(disp); dlg.setBackground( Color.white ); dlg.setSize( (numrows+1) * 25, numcols * 75); dlg.show(); } } class WindowCloser extends WindowAdapter { private Window adaptee; public WindowCloser(Window target) { adaptee = target; } public void windowClosing(WindowEvent we) { adaptee.dispose(); } }