/* * @(#)Graph.java 1.7 98/07/17 * * Copyright 1997, 1998, 1999 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */ /** * This is a demonstration JDBC applet. * It displays some simple standard output from the Coffee database. */ import java.applet.Applet; import java.awt.Graphics; import java.util.Vector; import java.sql.*; public class OutputApplet extends Applet implements Runnable { private Thread worker; private Vector queryResults; private String message = "Initializing"; public synchronized void start() { // Every time "start" is called we create a worker thread to // re-evaluate the database query. if (worker == null) { message = "Connecting to database"; worker = new Thread(this); worker.start(); } } public void init() { } /** * The "run" method is called from the worker thread. Notice that * because this method is doing potentially slow databases accesses * we avoid making it a synchronized method. */ public void run() { // Getting the parameter for jdbcUrl from the calling html file // This enables retargeting of the applet String url = getParameter("jdbcUrl"); // if the JOB jdbc url contains 'appletHost' as the host designation // We can map this to the actual host on which the applet is running // Normally you do not need to use "appletHost" in the // JDBC url. Instead, you should replace it with the full hostname, // or IP address of the machine on which you installed the // Easysoft JDBC-ODBC Bridge (JOB) server. // Use "appletHost" in the JDBC url if you do not // know in advance the name of the machine where JOB server // and your applet will be installed. // 'appletHost' designation allows you to write code to connect to a // JOB server on a host which is specified at run time. // If the 'appletHost' host designation is used in the // Easysoft JDBC url the Easysoft JDBC-ODBC Bridge driver expects // to find the key value pair // // "appletHost", in the // // java.util.Properties passed to the DriverManager.getConnection // method. // To set up this mapping do the following: // get the hostname for the machine where the applet is served from // The JOB Server needs to be running on this box too // as applet restrictions allow TPC/IP connections only to the // machine from which the applet has been served. String hostString = getCodeBase().getHost(); // Create Properties object java.util.Properties info = new java.util.Properties(); // Add to the properties list the key "appletHost" // with value the host of the JOB server info.put("appletHost",hostString); // Add "user" key/value pair // info.put("user","db user name"); // Add "password" key/value pair //info.put("password","db user password"); // Load the driver try { Class.forName("easysoft.sql.jobDriver").newInstance(); } catch(Exception ex) { setError("Can't find Database driver class: " + ex); return; } try { // Now establish connection with via the Driver manager Connection con = DriverManager.getConnection(url, info); Vector results = new Vector(); String query = "select COF_NAME, PRICE from COFFEES"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String s = rs.getString("COF_NAME"); float f = rs.getFloat("PRICE"); String text = s + " " + f; results.addElement(text); } stmt.close(); con.close(); setResults(results); } catch(SQLException ex) { setError("SQLException: " + ex); } } /** * The "paint" method is called by AWT when it wants us to * display our current state on the screen. */ public synchronized void paint(Graphics g) { // If there are no results available, display the current message. if (queryResults == null) { g.drawString(message, 5, 50); return; } // Display the results. g.drawString("Prices of coffee per pound: ", 5, 10); int y = 30; java.util.Enumeration enum = queryResults.elements(); while (enum.hasMoreElements()) { String text = (String)enum.nextElement(); g.drawString(text, 5, y); y = y + 15; } } /** * This private method is used to record an error message for * later display. */ private synchronized void setError(String mess) { queryResults = null; message = mess; worker = null; // And ask AWT to repaint this applet. repaint(); } /** * This private method is used to record the results of a query, for * later display. */ private synchronized void setResults(Vector results) { queryResults = results; worker = null; // And ask AWT to repaint this applet. repaint(); } }