home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / public / vhostdata / htdocs / mSQL / mSQLApplt.java < prev    next >
Encoding:
Java Source  |  1998-12-12  |  3.6 KB  |  113 lines

  1. //  Source File Name: mSQLApplt.java  %I%
  2. //
  3. //  Licensed Materials -- Property of IBM
  4. //
  5. //  (c) Copyright International Business Machines Corporation, 1996, 1997.
  6. //      All Rights Reserved.
  7. //
  8. //  US Government Users Restricted Rights -
  9. //  Use, duplication or disclosure restricted by
  10. //  GSA ADP Schedule Contract with IBM Corp.
  11.  
  12. // This sample program shows how to write a Java applet using the
  13. // JDBC applet driver to access a DB2 database.
  14. //
  15.  
  16. // Run this sample by the following steps:
  17. // (1) run db2sampl to create and populate the "sample" database
  18. // (2) modify the DB2Applt.html according to the instructions there
  19. // (4) start our JDBC applet server on some TCP/IP port: db2jstrt portno
  20. // (5) run this sample: install the applet and the HTML file according
  21. //     to the documentation, run it from a Java-enabled brower.
  22.  
  23. import java.sql.*;
  24. import java.awt.*;
  25. import java.applet.Applet;
  26.  
  27. public class mSQLApplt extends Applet {
  28.  
  29.    static {
  30.       try {
  31.          // register the driver with DriverManager
  32.          // The newInstance() call is needed for the sample to work with
  33.          // JDK 1.1.1 on OS/2, where the Class.forName() method does not
  34.          // run the static initializer. For other JDKs, the newInstance
  35.          // call can be omitted.
  36.  
  37.          Class.forName("com.imaginary.sql.msql.MsqlDriver").newInstance();
  38.       } catch (Exception e) {
  39.          e.printStackTrace();
  40.       }
  41.    }
  42.  
  43.    Connection con;
  44.  
  45.    public void init() {
  46.       resize(350,325);
  47.  
  48.  
  49.       try {
  50.          // get parameter values from the html page
  51.          String server = getParameter("server");
  52.          String port = getParameter("port");
  53.  
  54.          // construct the URL ( sample is the database name )
  55.          String url = "jdbc:msql://" + server + ":" + port + "/UserBase";
  56.  
  57.          String userid = getParameter("userid");
  58.          String password = getParameter("password");
  59.  
  60.          // connect to database with userid and password
  61.          con = DriverManager.getConnection(url, userid, password );
  62.  
  63.       } catch( Exception e ) {
  64.          e.printStackTrace();
  65.       }
  66.    }
  67.  
  68.    public void paint(Graphics g) {
  69.       try {
  70.          // retrieve data from database
  71.          g.drawString("First, let's retrieve some data from the database...", 10, 10);
  72.          Statement stmt = con.createStatement();
  73.          ResultSet rs = stmt.executeQuery("SELECT * from userpassword order by Userid");
  74.          g.drawString("Received results:", 10, 25);
  75.  
  76.          // display the result set
  77.          // rs.next() returns false when there are no more rows
  78.          int y = 50;
  79.          int i = 0;
  80.          while (rs.next() && (i<4)) {
  81.             i++;
  82.             String a= rs.getString(1);
  83.             String str = rs.getString(2);
  84.             String oneLine = "Userid= " + a + " Password= " + str;
  85.             g.drawString(oneLine, 20, y );
  86.             y = y + 20;
  87.  
  88.          }
  89.          stmt.close();
  90.  
  91.          // update the database
  92.          g.drawString("Update...", 10, 200);
  93.          stmt = con.createStatement();
  94.          int rowsUpdated = stmt.executeUpdate("UPDATE userpassword set Password = 'user01' where Userid = 'user01'");
  95.  
  96.          // display the number of rows updated
  97.          String msg = "Updated " + rowsUpdated;
  98.  
  99.          if (1 == rowsUpdated)
  100.             msg = msg +" row.";
  101.          else
  102.             msg = msg +" rows.";
  103.          y = y + 40;
  104.          g.drawString(msg, 20, y);
  105.  
  106.          stmt.close();
  107.       } catch( Exception e ) {
  108.     System.err.println("Exception!");
  109.          e.printStackTrace();
  110.       }
  111.    }
  112. }
  113.