home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / visualj / msdev / samples / microsoft / rdosample / simplerdo.java < prev    next >
Encoding:
Java Source  |  1996-08-27  |  11.9 KB  |  416 lines

  1. //******************************************************************************
  2. // simplerdo.java:    Applet
  3. //
  4. //******************************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7. import simplerdoframe;
  8.  
  9. import msrdo32.*;
  10. import com.ms.com.Variant;
  11.  
  12. // SimpleForm is the data entry form for the sample
  13. class SimpleForm extends Panel
  14. {
  15.     // Access to the data
  16.     rdoResultset m_resultset;
  17.  
  18.     // The fields in the form
  19.     TextField field1;
  20.     TextField field2;
  21.     TextField field3;
  22.     TextField field4;
  23.     TextField field5;
  24.     TextField field6;
  25.     Checkbox field7;
  26.  
  27.     // The names of the resultset columns
  28.     Variant name1;
  29.     Variant name2;
  30.     Variant name3;
  31.     Variant name4;
  32.     Variant name5;
  33.     Variant name6;
  34.     Variant name7;
  35.  
  36.     // Create the form
  37.     SimpleForm(rdoResultset r)
  38.     {
  39.         // Set the recordset
  40.         m_resultset = r;
  41.  
  42.         // Create panels for the labels and fields
  43.         Panel labels = new Panel();
  44.         Panel fields = new Panel();
  45.  
  46.         // Set those panels as a grid, one column wide
  47.         labels.setLayout(new GridLayout(0, 1));
  48.         fields.setLayout(new GridLayout(0, 1));
  49.  
  50.         // Set the form as labels to the left, fields to the right
  51.         setLayout(new FlowLayout());
  52.         add(labels);
  53.         add(fields);
  54.  
  55.         // Set the first label and field
  56.         //    Create the label and add it into the form
  57.         labels.add(new Label("Full Name"));
  58.         //    Create the field and add it into the form
  59.         fields.add(field1 = new TextField(25));
  60.         //    Disable the field since the access is read-only
  61.         field1.disable();
  62.         //    Create the field name for use with getItem
  63.         name1 = new Variant();
  64.         name1.putString("Name");
  65.  
  66.         // Repeat for the other labels and fields
  67.         labels.add(new Label("E-Mail Name"));
  68.         fields.add(field2 = new TextField(10));
  69.         field2.disable();
  70.         name2 = new Variant();
  71.         name2.putString("Email");
  72.         
  73.         labels.add(new Label("Phone Extension"));
  74.         fields.add(field3 = new TextField(12));
  75.         field3.disable();
  76.         name3 = new Variant();
  77.         name3.putString("Phone");
  78.         
  79.         labels.add(new Label("Office Location"));
  80.         fields.add(field4 = new TextField(15));
  81.         field4.disable();
  82.         name4 = new Variant();
  83.         name4.putString("Location");
  84.         
  85.         labels.add(new Label("Manager"));
  86.         fields.add(field5 = new TextField(10));
  87.         field5.disable();
  88.         name5 = new Variant();
  89.         name5.putString("Manager");
  90.         
  91.         labels.add(new Label("Date of Hire"));
  92.         fields.add(field6 = new TextField(10));
  93.         field6.disable();
  94.         name6 = new Variant();
  95.         name6.putString("HireDate");
  96.         
  97.         labels.add(new Label("Certified"));
  98.         fields.add(field7 = new Checkbox());
  99.         field7.disable();
  100.         name7 = new Variant();
  101.         name7.putString("Certified");
  102.     }
  103.  
  104.     // Show the data in the form
  105.     void showData()
  106.     {
  107.         // Get the columns in the resultset
  108.         rdoColumns columns = m_resultset.getrdoColumns();
  109.         rdoColumn c;
  110.  
  111.         // Create a Variant to get the value of each column
  112.         Variant value;
  113.  
  114.         // Get the first column
  115.         c = columns.getItem(name1);
  116.         // Get its value
  117.         value = c.getValue();
  118.         // Set the field in the form
  119.         field1.setText(value.toString());
  120.     
  121.         // Repeat for the other columns
  122.         c = columns.getItem(name2);
  123.         value = c.getValue();
  124.         field2.setText(value.toString());
  125.  
  126.         c = columns.getItem(name3);
  127.         value = c.getValue();
  128.         field3.setText(value.toString());
  129.  
  130.         c = columns.getItem(name4);
  131.         value = c.getValue();
  132.         field4.setText(value.toString());
  133.  
  134.         c = columns.getItem(name5);
  135.         value = c.getValue();
  136.         field5.setText(value.toString());
  137.  
  138.         c = columns.getItem(name6);
  139.         value = c.getValue();
  140.         field6.setText(value.toString());
  141.  
  142.         c = columns.getItem(name7);
  143.         value = c.getValue();
  144.         field7.setState(value.getBoolean());
  145.     }
  146. }
  147.  
  148. //==============================================================================
  149. // Main Class for applet simplerdo
  150. //
  151. //==============================================================================
  152. public class simplerdo extends Applet
  153. {
  154.     // RDO objects
  155.     //    The database engine
  156.     public static rdoEngine m_Engine = new rdoEngine();
  157.     //    The interface to the database engine
  158.     public static _rdoEngine m_IEngine;
  159.     //    The connection to the ODBC data source
  160.     public static rdoConnection m_IConnection;
  161.     //    The environment for this ODBC session
  162.     public static rdoEnvironment m_IEnvironment;
  163.     //    The SQL statement for this query
  164.     public static rdoPreparedStatement m_IStatement;
  165.     //  The resultset from the query
  166.     public static rdoResultset m_IResultSet;
  167.  
  168.     // Toolbar
  169.     Panel m_toolbar;
  170.     Button m_first;
  171.     Button m_prev;
  172.     Button m_next;
  173.     Button m_last;
  174.  
  175.     // Data Entry Form
  176.     SimpleForm m_form;
  177.  
  178.     // STANDALONE APPLICATION SUPPORT:
  179.     //        m_fStandAlone will be set to true if applet is run standalone
  180.     //--------------------------------------------------------------------------
  181.     boolean m_fStandAlone = false;
  182.  
  183.     
  184.     
  185.     // STANDALONE APPLICATION SUPPORT
  186.     //     The main() method acts as the applet's entry point when it is run
  187.     // as a standalone application. It is ignored if the applet is run from
  188.     // within an HTML page.
  189.     //--------------------------------------------------------------------------
  190.     public static void main(String args[])
  191.     {
  192.         // Create Toplevel Window to contain applet simpledao
  193.         //----------------------------------------------------------------------
  194.         simplerdoframe frame = new simplerdoframe("simplerdo");
  195.  
  196.         // Must show Frame before we size it so insets() will return valid values
  197.         //----------------------------------------------------------------------
  198.         frame.show();
  199.         frame.hide();
  200.         frame.resize(frame.insets().left + frame.insets().right  + 320,
  201.                      frame.insets().top  + frame.insets().bottom + 240);
  202.  
  203.         // The following code starts the applet running within the frame window.
  204.         // It also calls GetParameters() to retrieve parameter values from the
  205.         // command line, and sets m_fStandAlone to true to prevent init() from
  206.         // trying to get them from the HTML page.
  207.         //----------------------------------------------------------------------
  208.         simplerdo applet_simplerdo = new simplerdo();
  209.  
  210.         frame.add("Center", applet_simplerdo);
  211.         applet_simplerdo.m_fStandAlone = true;
  212.         applet_simplerdo.init();
  213.         applet_simplerdo.start();
  214.         frame.show();
  215.     }
  216.  
  217.     // simpledao Class Constructor
  218.     //--------------------------------------------------------------------------
  219.     public simplerdo()
  220.     {
  221.         // Create the toolbar and add it to the applet
  222.         m_toolbar = new Panel();
  223.         setLayout(new BorderLayout());
  224.         add("North", m_toolbar);
  225.         m_toolbar.add(m_first = new Button("First"));
  226.         m_toolbar.add(m_prev = new Button("Prev"));
  227.         m_toolbar.add(m_next = new Button("Next"));
  228.         m_toolbar.add(m_last = new Button("Last"));
  229.     }
  230.  
  231.     // APPLET INFO SUPPORT:
  232.     //        The getAppletInfo() method returns a string describing the applet's
  233.     // author, copyright date, or miscellaneous information.
  234.     //--------------------------------------------------------------------------
  235.     public String getAppletInfo()
  236.     {
  237.         return "Name: simplerdo\r\n" +
  238.                "Created with Microsoft Visual J++ Version 1.0";
  239.     }
  240.  
  241.     
  242.     // The init() method is called by the AWT when an applet is first loaded or
  243.     // reloaded.  Override this method to perform whatever initialization your
  244.     // applet needs, such as initializing data structures, loading images or
  245.     // fonts, creating frame windows, setting the layout manager, or adding UI
  246.     // components.
  247.     //--------------------------------------------------------------------------
  248.     public void init()
  249.     {
  250.         // If you use a ResourceWizard-generated "control creator" class to
  251.         // arrange controls in your applet, you may want to call its
  252.         // CreateControls() method from within this method. Remove the following
  253.         // call to resize() before adding the call to CreateControls();
  254.         // CreateControls() does its own resizing.
  255.         //----------------------------------------------------------------------
  256.         resize(320, 240);
  257.  
  258.     }
  259.  
  260.     // Place additional applet clean up code here.  destroy() is called when
  261.     // when you applet is terminating and being unloaded.
  262.     //-------------------------------------------------------------------------
  263.     public void destroy()
  264.     {
  265.     }
  266.  
  267.     // simpledao Paint Handler
  268.     //--------------------------------------------------------------------------
  269.     public void paint(Graphics g)
  270.     {
  271.     }
  272.  
  273.     //        The start() method is called when the page containing the applet
  274.     // first appears on the screen. The AppletWizard's initial implementation
  275.     // of this method starts execution of the applet's thread.
  276.     //--------------------------------------------------------------------------
  277.     public void start()
  278.     {
  279.         // Setup Variants for optional parameters
  280.         Variant v1 = new Variant();
  281.         Variant v2 = new Variant();
  282.         Variant v3 = new Variant();
  283.         Variant v4 = new Variant();
  284.         Variant v5 = new Variant();
  285.         Variant v6 = new Variant();
  286.         Variant v7 = new Variant();
  287.  
  288.         // Cast the database engine to the interface type
  289.         m_IEngine = (_rdoEngine) m_Engine;
  290.  
  291.         // Create the environment for the default user
  292.         m_IEnvironment = m_IEngine.rdoCreateEnvironment("", "", "");
  293.  
  294.         // Set optional parameter values
  295.         v1.putInt(PromptConstants.rdDriverNoPrompt);
  296.         v2.putBoolean(false);
  297.         v3.putString("DSN=Sample Database;UID=;PWD=;DATABASE=Sample Database");
  298.  
  299.         // Open a connection to the data source
  300.         m_IConnection = m_IEnvironment.OpenConnection("",v1,v2,v3);
  301.         
  302.         // Set the Prepared statement
  303.         v7.putString("");
  304.         m_IStatement = m_IConnection.CreatePreparedStatement("Statement 1",v7);
  305.         
  306.         // Set optional parameters
  307.         v4.putInt(ResultsetTypeConstants.rdOpenKeyset);
  308.         v5.putInt(LockTypeConstants.rdConcurReadOnly);
  309.         v6.putInt(0);
  310.  
  311.         // Set the SQL statement for the query
  312.         m_IStatement.putSQL("SELECT * FROM Employees");
  313.  
  314.         // Open the resultset for the query
  315.         m_IResultSet = m_IStatement.OpenResultset(v4,v5,v6);
  316.  
  317.         // Create the form, passing the resultset
  318.         m_form = new SimpleForm(m_IResultSet);
  319.         
  320.         // Add the form to the applet
  321.         add("Center", m_form);
  322.  
  323.         // Get the first row in the resultset
  324.         m_IResultSet.MoveFirst();
  325.  
  326.         // Display that row
  327.         m_form.showData();
  328.     }
  329.     
  330.     //        The stop() method is called when the page containing the applet is
  331.     // no longer on the screen. The AppletWizard's initial implementation of
  332.     // this method stops execution of the applet's thread.
  333.     //--------------------------------------------------------------------------
  334.     public void stop()
  335.     {
  336.         // Close the resultset
  337.         m_IResultSet.Close();
  338.         m_IResultSet = null;
  339.  
  340.         // And remove the form
  341.         remove(m_form);
  342.         m_form = null;
  343.     }
  344.  
  345.     public boolean action(Event  evt, Object  what)
  346.     {
  347.         // Handle click on the First button
  348.         if (evt.target == m_first)
  349.         {
  350.             // Update the status bar, if available
  351.             if (! m_fStandAlone) showStatus ("Getting first record");
  352.  
  353.             // Move to the requested row
  354.             m_IResultSet.MoveFirst();
  355.  
  356.             // Show that row
  357.             m_form.showData();
  358.  
  359.             // Event handled
  360.             return true;
  361.         }
  362.  
  363.         // Handle click on the Prev button
  364.         if (evt.target == m_prev)
  365.         {
  366.             // Update the status bar, if available
  367.             if (! m_fStandAlone) showStatus ("Getting previous record");
  368.  
  369.             // Move to the requested row
  370.             m_IResultSet.MovePrevious();
  371.  
  372.             // Show that row
  373.             m_form.showData();
  374.  
  375.             // Event handled
  376.             return true;
  377.         }
  378.         
  379.         // Handle click on the Next button
  380.         if (evt.target == m_next)
  381.         {
  382.             // Update the status bar, if available
  383.             if (! m_fStandAlone) showStatus ("Getting next record");
  384.  
  385.             // Move to the requested row
  386.             m_IResultSet.MoveNext();
  387.  
  388.             // Show that row
  389.             m_form.showData();
  390.  
  391.             // Event handled
  392.             return true;
  393.         }
  394.         
  395.         // Handle click on the Last button
  396.         if (evt.target == m_last)
  397.         {
  398.             // Update the status bar, if available
  399.             if (! m_fStandAlone) showStatus ("Getting last record");
  400.  
  401.             // Move to the requested row
  402.             m_IResultSet.MoveLast();
  403.  
  404.             // Show that row
  405.             m_form.showData();
  406.  
  407.             // Event handled
  408.             return true;
  409.         }
  410.         
  411.         // Event not handled
  412.         return false;
  413.     }
  414.  
  415. }
  416.