//  Source File Name: DB2Applt.java  1.2
//  
//   Licensed Materials -- Property of IBM 
//  
//   (c) Copyright International Business Machines Corporation, 1996, 1999. 
//       All Rights Reserved. 
//  
//   US Government Users Restricted Rights - 
//   Use, duplication or disclosure restricted by 
//   GSA ADP Schedule Contract with IBM Corp. 

//  This sample program shows how to write a Java applet using the 
//  JDBC applet driver to access a DB2 database. 

//  Run this sample using the following steps: 
//  (1) create and populate the "sample" database with the following  
//      command: db2sampl 
//  (2) customize DB2Applt.html with your server, port, user ID, and  
//      password 
//  (3) start the DB2 JDBC server on a TCP/IP port with the following 
//      command: db2jstrt portno 
//  (4) run this sample: install the applet and the HTML file according 
//      to the documentation, and view it in a Java-enabled browser, or 
//      view it locally with the following command:  
//         appletviewer DB2Applt.html 

//  For more information about this sample, refer to the README file. 

//  For more information on Programming in Java, refer to the  
//  "Programming in Java" section of the Application Development Guide. 

//  For more information on building and running Java programs for DB2, 
//  refer to the "Building Java Applets and Applications" section of the  
//  Application Building Guide. 

//  For more information on the SQL language, refer to the SQL Reference. 

import java.sql.*;
import java.awt.*;
import java.applet.Applet;

public class DB2Applt extends Applet {

   static {
      try {
         //  register the driver with DriverManager 
         //  The newInstance() call is needed for the sample to work with 
         //  JDK 1.1.1 on OS/2, where the Class.forName() method does not 
         //  run the static initializer. For other JDKs, the newInstance 
         //  call can be omitted. 

         Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   Connection con;

   public void init() {

      try {
         //  get parameter values from the html page 
         String server = getParameter("server");
         String port = getParameter("port");

         //  construct the URL ( sample is the database name ) 
         String url = "jdbc:db2://"+server+":"+port+"/sample";

         String userid = getParameter("userid");
         String password = getParameter("password");

         //  connect to database with userid and password 
         con = DriverManager.getConnection(url, userid, password );

      } catch( Exception e ) {
         e.printStackTrace();
      }
   }

   public void paint(Graphics g) {
      try {
         //  retrieve data from database 
         g.drawString("First, let's retrieve some data from the database...", 10, 10);

         Statement stmt = con.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT * from employee");
         g.drawString("Received results:", 10, 25);

         //  display the result set 
         //  rs.next() returns false when there are no more rows 
         int y = 50;
         int i = 0;
         while (rs.next() && (i<2)) {
            i++;
            String a= rs.getString(1);
            String str = rs.getString(2);
            String oneLine = " empno= " + a + " firstname= " + str;
            g.drawString(oneLine, 20, y );
            y = y + 15;

         }
         stmt.close();

         //  update the database 
         g.drawString("Now, update the database...", 10, 100);
         stmt = con.createStatement();
         int rowsUpdated = stmt.executeUpdate("UPDATE employee set firstnme = 'SHILI' where empno = '000010'");

         //  display the number of rows updated 
         String msg = "Updated " + rowsUpdated;

         if (1 == rowsUpdated)
            msg = msg +" row.";
         else
            msg = msg +" rows.";
         y = y + 40;
         g.drawString(msg, 20, y);

         stmt.close();
      } catch( Exception e ) {
         e.printStackTrace();
      }
   }
}