
 import java.applet.Applet;

 import java.awt.Graphics;

 import java.util.Vector;
 import java.util.*;

 import java.sql.*;

 

  public class KahveApplet extends Applet implements Runnable {

     private Thread worker;

     private Vector queryResults;

     private String message = "Baslatiliyor...";

 

     public synchronized void start() {

         //  "start" her cagirildiginda

         // kahve database query tekrar ‡agirilacaktir.

  if (worker == null) {

             message = "Veritabani ile baglanti kuruluyor";

             worker = new Thread(this);

      worker.start();

  }

     }

     public void run() {

         String url = "jdbc:odbc:prog";

         String query = "select KAHVE_ISMI, FIYAT from KAHVE";

 

  try {

             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

  } catch(Exception ex) {

      setError("Can't find Database driver class: " + ex);

      return;

  }

 

  try {

      Vector results = new Vector();

      Connection con = DriverManager.getConnection(url,

       "myLogin", "myPassword");

      Statement stmt = con.createStatement();

      ResultSet rs = stmt.executeQuery(query);

      while (rs.next()) {

                 String s = rs.getString("KAHVE_ISMI");

                 float f = rs.getFloat("FIYAT");

   String text = s + "     " + f;

   results.addElement(text);

      }

 

      stmt.close();

      con.close();

 

      setResults(results);

 

  } catch(SQLException ex) {

      setError("SQLException: " + ex);

  }

     }

 

 

  public synchronized void paint(Graphics g) {

  if (queryResults == null)

 {

      g.drawString(message, 5, 50);

      return;

  }

 

  // Display the results.

  g.drawString("Kahve fiyatlari :  ", 5, 10);

  int y = 30;

  enumeration enum1 = queryResults.elements();

  while (enum1.hasMoreElements()) {

      String text = (String)enum1.nextElement();

      g.drawString(text, 5, y);

      y = y + 15;

  }

     }

 

     private synchronized void setError(String mess) {

  queryResults = null;

  message = mess;

  worker = null;

  // And ask AWT to repaint this applet.

  repaint();

     }

 

 

   private synchronized void setResults(Vector results) {

  queryResults = results;

  worker = null;

  repaint();

     }

 }











