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

  1. //******************************************************************************
  2. // simpledao.java:    Applet
  3. //
  4. //******************************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7. import simpledaoframe;
  8.  
  9. // Creates the DAO DBEngine use the license
  10. import dao_dbengine;
  11.  
  12. import dao3032.*;
  13. import com.ms.com.Variant;
  14.  
  15. // SimpleForm is the data entry form for the sample
  16. class SimpleForm extends Panel
  17. {
  18.     // Access to the data
  19.     Recordset m_recordset;
  20.  
  21.     // The fields in the form
  22.     TextField field1;
  23.     TextField field2;
  24.     TextField field3;
  25.     TextField field4;
  26.     TextField field5;
  27.     TextField field6;
  28.     Checkbox field7;
  29.  
  30.     // The names of the recordset fields
  31.     Variant name1;
  32.     Variant name2;
  33.     Variant name3;
  34.     Variant name4;
  35.     Variant name5;
  36.     Variant name6;
  37.     Variant name7;
  38.  
  39.     // Create the form
  40.     SimpleForm(Recordset r)
  41.     {
  42.         // Set the recordset
  43.         m_recordset = r;
  44.  
  45.         // Create panels for the labels and fields
  46.         Panel labels = new Panel();
  47.         Panel fields = new Panel();
  48.  
  49.         // Set those panels as a grid, one column wide
  50.         labels.setLayout(new GridLayout(0, 1));
  51.         fields.setLayout(new GridLayout(0, 1));
  52.  
  53.         // Set the form as labels to the left, fields to the right
  54.         setLayout(new FlowLayout());
  55.         add(labels);
  56.         add(fields);
  57.  
  58.         // Set the first label and field
  59.         //    Create the label and add it into the form
  60.         labels.add(new Label("Full Name"));
  61.         //    Create the field and add it into the form
  62.         fields.add(field1 = new TextField(25));
  63.         //    Disable the field since the access is read-only
  64.         field1.disable();
  65.         //    Create the field name for use with getItem
  66.         name1 = new Variant();
  67.         name1.putString("Name");
  68.  
  69.         // Repeat for the other labels and fields
  70.         labels.add(new Label("E-Mail Name"));
  71.         fields.add(field2 = new TextField(10));
  72.         field2.disable();
  73.         name2 = new Variant();
  74.         name2.putString("Email");
  75.         
  76.         labels.add(new Label("Phone Extension"));
  77.         fields.add(field3 = new TextField(12));
  78.         field3.disable();
  79.         name3 = new Variant();
  80.         name3.putString("Phone");
  81.         
  82.         labels.add(new Label("Office Location"));
  83.         fields.add(field4 = new TextField(15));
  84.         field4.disable();
  85.         name4 = new Variant();
  86.         name4.putString("Location");
  87.         
  88.         labels.add(new Label("Manager"));
  89.         fields.add(field5 = new TextField(10));
  90.         field5.disable();
  91.         name5 = new Variant();
  92.         name5.putString("Manager");
  93.         
  94.         labels.add(new Label("Date of Hire"));
  95.         fields.add(field6 = new TextField(10));
  96.         field6.disable();
  97.         name6 = new Variant();
  98.         name6.putString("HireDate");
  99.         
  100.         labels.add(new Label("Certified"));
  101.         fields.add(field7 = new Checkbox());
  102.         field7.disable();
  103.         name7 = new Variant();
  104.         name7.putString("Certified");
  105.     }
  106.  
  107.     // Show the data in the form
  108.     void showData()
  109.     {
  110.         // Get the fields in the recordset
  111.         Fields fields = m_recordset.getFields();
  112.         _Field f;
  113.  
  114.         // Create a Variant to get the value of each field
  115.         Variant value;
  116.  
  117.         // Get the first field
  118.         f = fields.getItem(name1);
  119.         // Get its value
  120.         value = f.getValue();
  121.         // Set the field in the form
  122.         field1.setText(value.toString());
  123.  
  124.         // Repeat for the other fields
  125.         f = fields.getItem(name2);
  126.         value = f.getValue();
  127.         field2.setText(value.toString());
  128.  
  129.         f = fields.getItem(name3);
  130.         value = f.getValue();
  131.         field3.setText(value.toString());
  132.  
  133.         f = fields.getItem(name4);
  134.         value = f.getValue();
  135.         field4.setText(value.toString());
  136.  
  137.         f = fields.getItem(name5);
  138.         value = f.getValue();
  139.         field5.setText(value.toString());
  140.  
  141.         f = fields.getItem(name6);
  142.         value = f.getValue();
  143.         field6.setText(value.toString());
  144.  
  145.         f = fields.getItem(name7);
  146.         value = f.getValue();
  147.         field7.setState(value.getBoolean());
  148.     }
  149. }
  150.  
  151. //==============================================================================
  152. // Main Class for applet simpledao
  153. //
  154. //==============================================================================
  155. public class simpledao extends Applet
  156. {
  157.     // Toolbar
  158.     Panel m_toolbar;
  159.     Button m_first;
  160.     Button m_prev;
  161.     Button m_next;
  162.     Button m_last;
  163.  
  164.     // Data Entry Form
  165.     SimpleForm m_form;
  166.     Recordset recordset;
  167.     String filename;
  168.  
  169.     // STANDALONE APPLICATION SUPPORT:
  170.     //        m_fStandAlone will be set to true if applet is run standalone
  171.     //--------------------------------------------------------------------------
  172.     boolean m_fStandAlone = false;
  173.  
  174.     // PARAMETER SUPPORT:
  175.     //        Parameters allow an HTML author to pass information to the applet;
  176.     // the HTML author specifies them using the <PARAM> tag within the <APPLET>
  177.     // tag.  The following variables are used to store the values of the
  178.     // parameters.
  179.     //--------------------------------------------------------------------------
  180.  
  181.     // Members for applet parameters
  182.     // <type>       <MemberVar>    = <Default Value>
  183.     //--------------------------------------------------------------------------
  184.     private String m_Database = "Sample Database.mdb";
  185.     private String m_Recordset = "Employees";
  186.  
  187.     // Parameter names.  To change a name of a parameter, you need only make
  188.     // a single change.  Simply modify the value of the parameter string below.
  189.     //--------------------------------------------------------------------------
  190.     private final String PARAM_Database = "Database";
  191.     private final String PARAM_Recordset = "Recordset";
  192.  
  193.     // STANDALONE APPLICATION SUPPORT
  194.     //     The GetParameter() method is a replacement for the getParameter() method
  195.     // defined by Applet. This method returns the value of the specified parameter;
  196.     // unlike the original getParameter() method, this method works when the applet
  197.     // is run as a standalone application, as well as when run within an HTML page.
  198.     // This method is called by GetParameters().
  199.     //---------------------------------------------------------------------------
  200.     String GetParameter(String strName, String args[])
  201.     {
  202.         if (args == null)
  203.         {
  204.             // Running within an HTML page, so call original getParameter().
  205.             //-------------------------------------------------------------------
  206.             return getParameter(strName);
  207.         }
  208.  
  209.         // Running as standalone application, so parameter values are obtained from
  210.         // the command line. The user specifies them as follows:
  211.         //
  212.         //    JView simpledao param1=<val> param2=<"val with spaces"> ...
  213.         //-----------------------------------------------------------------------
  214.         int    i;
  215.         String strArg    = strName + "=";
  216.         String strValue = null;
  217.  
  218.         for (i = 0; i < args.length; i++)
  219.         {
  220.             if (strArg.equalsIgnoreCase(args[i].substring(0, strArg.length())))
  221.             {
  222.                 // Found matching parameter on command line, so extract its value.
  223.                 // If in double quotes, remove the quotes.
  224.                 //---------------------------------------------------------------
  225.                 strValue= args[i].substring(strArg.length());
  226.                 if (strValue.startsWith("\""))
  227.                 {
  228.                     strValue = strValue.substring(1);
  229.                     if (strValue.endsWith("\""))
  230.                         strValue = strValue.substring(0, strValue.length() - 1);
  231.                 }
  232.             }
  233.         }
  234.  
  235.         return strValue;
  236.     }
  237.  
  238.     // STANDALONE APPLICATION SUPPORT
  239.     //     The GetParameters() method retrieves the values of each of the applet's
  240.     // parameters and stores them in variables. This method works both when the
  241.     // applet is run as a standalone application and when it's run within an HTML
  242.     // page.  When the applet is run as a standalone application, this method is
  243.     // called by the main() method, which passes it the command-line arguments.
  244.     // When the applet is run within an HTML page, this method is called by the
  245.     // init() method with args == null.
  246.     //---------------------------------------------------------------------------
  247.     void GetParameters(String args[])
  248.     {
  249.         // Query values of all Parameters
  250.         //--------------------------------------------------------------
  251.         String param;
  252.  
  253.         // Database: Parameter description
  254.         //--------------------------------------------------------------
  255.         param = GetParameter(PARAM_Database, args);
  256.         if (param != null)
  257.             m_Database = param;
  258.  
  259.         // Recordset: Parameter description
  260.         //--------------------------------------------------------------
  261.         param = GetParameter(PARAM_Recordset, args);
  262.         if (param != null)
  263.             m_Recordset = param;
  264.  
  265.     }
  266.  
  267.     // STANDALONE APPLICATION SUPPORT
  268.     //     The main() method acts as the applet's entry point when it is run
  269.     // as a standalone application. It is ignored if the applet is run from
  270.     // within an HTML page.
  271.     //--------------------------------------------------------------------------
  272.     public static void main(String args[])
  273.     {
  274.         // Create Toplevel Window to contain applet simpledao
  275.         //----------------------------------------------------------------------
  276.         simpledaoframe frame = new simpledaoframe("simpledao");
  277.  
  278.         // Must show Frame before we size it so insets() will return valid values
  279.         //----------------------------------------------------------------------
  280.         frame.show();
  281.         frame.hide();
  282.         frame.resize(frame.insets().left + frame.insets().right  + 320,
  283.                      frame.insets().top  + frame.insets().bottom + 240);
  284.  
  285.         // The following code starts the applet running within the frame window.
  286.         // It also calls GetParameters() to retrieve parameter values from the
  287.         // command line, and sets m_fStandAlone to true to prevent init() from
  288.         // trying to get them from the HTML page.
  289.         //----------------------------------------------------------------------
  290.         simpledao applet_simpledao = new simpledao();
  291.  
  292.         frame.add("Center", applet_simpledao);
  293.         applet_simpledao.m_fStandAlone = true;
  294.         applet_simpledao.GetParameters(args);
  295.         applet_simpledao.init();
  296.         applet_simpledao.start();
  297.         frame.show();
  298.     }
  299.  
  300.     // simpledao Class Constructor
  301.     //--------------------------------------------------------------------------
  302.     public simpledao()
  303.     {
  304.         // Create the toolbar and add it to the applet
  305.         m_toolbar = new Panel();
  306.         add("North", m_toolbar);
  307.         m_toolbar.add(m_first = new Button("First"));
  308.         m_toolbar.add(m_prev = new Button("Prev"));
  309.         m_toolbar.add(m_next = new Button("Next"));
  310.         m_toolbar.add(m_last = new Button("Last"));
  311.     }
  312.  
  313.     // APPLET INFO SUPPORT:
  314.     //        The getAppletInfo() method returns a string describing the applet's
  315.     // author, copyright date, or miscellaneous information.
  316.     //--------------------------------------------------------------------------
  317.     public String getAppletInfo()
  318.     {
  319.         return "Name: simpledao\r\n" +
  320.                "Author: Dan Jinguji\r\n" +
  321.                "Created with Microsoft Visual J++ Version 1.0";
  322.     }
  323.  
  324.     // PARAMETER SUPPORT
  325.     //        The getParameterInfo() method returns an array of strings describing
  326.     // the parameters understood by this applet.
  327.     //
  328.     // simpledao Parameter Information:
  329.     //  { "Name", "Type", "Description" },
  330.     //--------------------------------------------------------------------------
  331.     public String[][] getParameterInfo()
  332.     {
  333.         String[][] info =
  334.         {
  335.             { PARAM_Database, "String", "Name of the Database" },
  336.             { PARAM_Recordset, "String", "Name of the Recordset" },
  337.         };
  338.         return info;        
  339.     }
  340.  
  341.     // The init() method is called by the AWT when an applet is first loaded or
  342.     // reloaded.  Override this method to perform whatever initialization your
  343.     // applet needs, such as initializing data structures, loading images or
  344.     // fonts, creating frame windows, setting the layout manager, or adding UI
  345.     // components.
  346.     //--------------------------------------------------------------------------
  347.     public void init()
  348.     {
  349.         if (!m_fStandAlone)
  350.             GetParameters(null);
  351.         // If you use a ResourceWizard-generated "control creator" class to
  352.         // arrange controls in your applet, you may want to call its
  353.         // CreateControls() method from within this method. Remove the following
  354.         // call to resize() before adding the call to CreateControls();
  355.         // CreateControls() does its own resizing.
  356.         //----------------------------------------------------------------------
  357.         resize(320, 200);
  358.  
  359.         // Set the name of the database (parameter)
  360.         if (m_fStandAlone)
  361.             // use the value on the command line for the MDB filename
  362.             filename = m_Database;
  363.         else
  364.         try {
  365.             // otherwise generate it relative to the applet
  366.             java.net.URL fn;
  367.             fn = new java.net.URL(getDocumentBase(), m_Database);
  368.             // strip away the "file:/" from the URL
  369.             filename = fn.getFile().substring(1);
  370.         } catch(Exception e) {
  371.             showStatus("Error: " + e.getMessage());
  372.         }
  373.     }
  374.  
  375.     // Place additional applet clean up code here.  destroy() is called when
  376.     // when you applet is terminating and being unloaded.
  377.     //-------------------------------------------------------------------------
  378.     public void destroy()
  379.     {
  380.     }
  381.  
  382.     // simpledao Paint Handler
  383.     //--------------------------------------------------------------------------
  384.     public void paint(Graphics g)
  385.     {
  386.     }
  387.  
  388.     //        The start() method is called when the page containing the applet
  389.     // first appears on the screen. The AppletWizard's initial implementation
  390.     // of this method starts execution of the applet's thread.
  391.     //--------------------------------------------------------------------------
  392.     public void start()
  393.     {
  394.         // Create the database engine using the license key
  395.         _DBEngine idbengine = dao_dbengine.create();
  396.         Database database;
  397.  
  398.         // Create Variants for optional parameters
  399.         Variant v1 = new Variant();
  400.         Variant v2 = new Variant();
  401.         Variant v3 = new Variant();
  402.  
  403.         v1.putBoolean(false);
  404.         v2.putBoolean(false);
  405.         v3.putString("");
  406.  
  407.         // Open the database for non-exclusive access
  408.         database = idbengine.OpenDatabase(filename, v1, v2, v3);
  409.  
  410.         // Create Variants for optional parameters
  411.         Variant v4 = new Variant();
  412.         Variant v5 = new Variant();
  413.  
  414.         v4.putShort(Constants.dbOpenDynaset);
  415.         v5.putShort(Constants.dbReadOnly);
  416.  
  417.         // Create the recordset, a read-only dynaset
  418.         recordset = database.OpenRecordset(m_Recordset, v4, v5);
  419.  
  420.         // Create the form, passing it the recordset
  421.         m_form = new SimpleForm(recordset);
  422.  
  423.         // Add it to the applet
  424.         add("Center", m_form);
  425.  
  426.         // Get the first record in the recordset
  427.         recordset.MoveFirst();
  428.  
  429.         // Display that record
  430.         m_form.showData();
  431.  
  432.     }
  433.     
  434.     //        The stop() method is called when the page containing the applet is
  435.     // no longer on the screen. The AppletWizard's initial implementation of
  436.     // this method stops execution of the applet's thread.
  437.     //--------------------------------------------------------------------------
  438.     public void stop()
  439.     {
  440.         // Close the recordset
  441.         recordset.Close();
  442.         recordset = null;
  443.  
  444.         // Remove the form
  445.         remove(m_form);
  446.         m_form = null;
  447.     }
  448.  
  449.  
  450.     public boolean action(Event  evt, Object  what)
  451.     {
  452.         // Handle click on the First button
  453.         if (evt.target == m_first)
  454.         {
  455.             // Update the status bar, if available
  456.             if (! m_fStandAlone) showStatus ("Getting first record");
  457.  
  458.             // Move to the requested record
  459.             recordset.MoveFirst();
  460.  
  461.             // Show that record
  462.             m_form.showData();
  463.  
  464.             // Event handled
  465.             return true;
  466.         }
  467.         
  468.         // Handle click on the Prev button
  469.         if (evt.target == m_prev)
  470.         {
  471.             // Update the status bar, if available
  472.             if (! m_fStandAlone) showStatus ("Getting previous record");
  473.  
  474.             // Move to the requested record
  475.             recordset.MovePrevious();
  476.  
  477.             // Show that record
  478.             m_form.showData();
  479.  
  480.             // Event handled
  481.             return true;
  482.         }
  483.         
  484.         // Handle click on the Next button
  485.         if (evt.target == m_next)
  486.         {
  487.             // Update the status bar, if available
  488.             if (! m_fStandAlone) showStatus ("Getting next record");
  489.  
  490.             // Move to the requested record
  491.             recordset.MoveNext();
  492.  
  493.             // Show that record
  494.             m_form.showData();
  495.  
  496.             // Event handled
  497.             return true;
  498.         }
  499.         
  500.         // Handle click on the Last button
  501.         if (evt.target == m_last)
  502.         {
  503.             // Update the status bar, if available
  504.             if (! m_fStandAlone) showStatus ("Getting last record");
  505.  
  506.             // Move to the requested record
  507.             recordset.MoveLast();
  508.  
  509.             // Show that record
  510.             m_form.showData();
  511.  
  512.             // Event handled
  513.             return true;
  514.         }
  515.         
  516.         // Event not handled
  517.         return false;
  518.     }
  519.  
  520. }
  521.