home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / MICROSOFT / DAOSAMPLE / SIMPLEDAO.JAVA < prev    next >
Encoding:
Java Source  |  1996-12-06  |  16.0 KB  |  518 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 dao350.*;
  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.     // Database and Recordset variables
  170.     protected Database database;
  171.     protected boolean readOnly = false;
  172.  
  173.     // STANDALONE APPLICATION SUPPORT:
  174.     //        m_fStandAlone will be set to true if applet is run standalone
  175.     //--------------------------------------------------------------------------
  176.     boolean m_fStandAlone = false;
  177.  
  178.     // PARAMETER SUPPORT:
  179.     //        Parameters allow an HTML author to pass information to the applet;
  180.     // the HTML author specifies them using the <PARAM> tag within the <APPLET>
  181.     // tag.  The following variables are used to store the values of the
  182.     // parameters.
  183.     //--------------------------------------------------------------------------
  184.  
  185.     // Members for applet parameters
  186.     // <type>       <MemberVar>    = <Default Value>
  187.     //--------------------------------------------------------------------------
  188.     private String m_Database = "Sample Database.mdb";
  189.     private String m_Recordset = "Employees";
  190.  
  191.     // Parameter names.  To change a name of a parameter, you need only make
  192.     // a single change.  Simply modify the value of the parameter string below.
  193.     //--------------------------------------------------------------------------
  194.     private final String PARAM_Database = "Database";
  195.     private final String PARAM_Recordset = "Recordset";
  196.  
  197.     // STANDALONE APPLICATION SUPPORT
  198.     //     The GetParameter() method is a replacement for the getParameter() method
  199.     // defined by Applet. This method returns the value of the specified parameter;
  200.     // unlike the original getParameter() method, this method works when the applet
  201.     // is run as a standalone application, as well as when run within an HTML page.
  202.     // This method is called by GetParameters().
  203.     //---------------------------------------------------------------------------
  204.     String GetParameter(String strName, String args[])
  205.     {
  206.         if (args == null)
  207.         {
  208.             // Running within an HTML page, so call original getParameter().
  209.             //-------------------------------------------------------------------
  210.             return getParameter(strName);
  211.         }
  212.  
  213.         // Running as standalone application, so parameter values are obtained from
  214.         // the command line. The user specifies them as follows:
  215.         //
  216.         //    JView simpledao param1=<val> param2=<"val with spaces"> ...
  217.         //-----------------------------------------------------------------------
  218.         int    i;
  219.         String strArg    = strName + "=";
  220.         String strValue = null;
  221.  
  222.         for (i = 0; i < args.length; i++)
  223.         {
  224.             if (strArg.equalsIgnoreCase(args[i].substring(0, strArg.length())))
  225.             {
  226.                 // Found matching parameter on command line, so extract its value.
  227.                 // If in double quotes, remove the quotes.
  228.                 //---------------------------------------------------------------
  229.                 strValue= args[i].substring(strArg.length());
  230.                 if (strValue.startsWith("\""))
  231.                 {
  232.                     strValue = strValue.substring(1);
  233.                     if (strValue.endsWith("\""))
  234.                         strValue = strValue.substring(0, strValue.length() - 1);
  235.                 }
  236.             }
  237.         }
  238.  
  239.         return strValue;
  240.     }
  241.  
  242.     // STANDALONE APPLICATION SUPPORT
  243.     //     The GetParameters() method retrieves the values of each of the applet's
  244.     // parameters and stores them in variables. This method works both when the
  245.     // applet is run as a standalone application and when it's run within an HTML
  246.     // page.  When the applet is run as a standalone application, this method is
  247.     // called by the main() method, which passes it the command-line arguments.
  248.     // When the applet is run within an HTML page, this method is called by the
  249.     // init() method with args == null.
  250.     //---------------------------------------------------------------------------
  251.     void GetParameters(String args[])
  252.     {
  253.         // Query values of all Parameters
  254.         //--------------------------------------------------------------
  255.         String param;
  256.  
  257.         // Database: Parameter description
  258.         //--------------------------------------------------------------
  259.         param = GetParameter(PARAM_Database, args);
  260.         if (param != null)
  261.             m_Database = param;
  262.  
  263.         // Recordset: Parameter description
  264.         //--------------------------------------------------------------
  265.         param = GetParameter(PARAM_Recordset, args);
  266.         if (param != null)
  267.             m_Recordset = param;
  268.  
  269.     }
  270.  
  271.     // STANDALONE APPLICATION SUPPORT
  272.     //     The main() method acts as the applet's entry point when it is run
  273.     // as a standalone application. It is ignored if the applet is run from
  274.     // within an HTML page.
  275.     //--------------------------------------------------------------------------
  276.     public static void main(String args[])
  277.     {
  278.         // Create Toplevel Window to contain applet simpledao
  279.         //----------------------------------------------------------------------
  280.         simpledaoframe frame = new simpledaoframe("simpledao");
  281.  
  282.         // Must show Frame before we size it so insets() will return valid values
  283.         //----------------------------------------------------------------------
  284.         frame.show();
  285.         frame.hide();
  286.         frame.resize(frame.insets().left + frame.insets().right  + 320,
  287.                      frame.insets().top  + frame.insets().bottom + 240);
  288.  
  289.         // The following code starts the applet running within the frame window.
  290.         // It also calls GetParameters() to retrieve parameter values from the
  291.         // command line, and sets m_fStandAlone to true to prevent init() from
  292.         // trying to get them from the HTML page.
  293.         //----------------------------------------------------------------------
  294.         simpledao applet_simpledao = new simpledao();
  295.  
  296.         frame.add("Center", applet_simpledao);
  297.         applet_simpledao.m_fStandAlone = true;
  298.         applet_simpledao.GetParameters(args);
  299.         applet_simpledao.init();
  300.         applet_simpledao.start();
  301.         frame.show();
  302.     }
  303.  
  304.     // simpledao Class Constructor
  305.     //--------------------------------------------------------------------------
  306.     public simpledao()
  307.     {
  308.         // Create the toolbar and add it to the applet
  309.         m_toolbar = new Panel();
  310.         add("North", m_toolbar);
  311.         m_toolbar.add(m_first = new Button("First"));
  312.         m_toolbar.add(m_prev = new Button("Prev"));
  313.         m_toolbar.add(m_next = new Button("Next"));
  314.         m_toolbar.add(m_last = new Button("Last"));
  315.     }
  316.  
  317.     // APPLET INFO SUPPORT:
  318.     //        The getAppletInfo() method returns a string describing the applet's
  319.     // author, copyright date, or miscellaneous information.
  320.     //--------------------------------------------------------------------------
  321.     public String getAppletInfo()
  322.     {
  323.         return "Name: simpledao\r\n" +
  324.                "Created with Microsoft Visual J++ Version 1.1";
  325.     }
  326.  
  327.     // PARAMETER SUPPORT
  328.     //        The getParameterInfo() method returns an array of strings describing
  329.     // the parameters understood by this applet.
  330.     //
  331.     // simpledao Parameter Information:
  332.     //  { "Name", "Type", "Description" },
  333.     //--------------------------------------------------------------------------
  334.     public String[][] getParameterInfo()
  335.     {
  336.         String[][] info =
  337.         {
  338.             { PARAM_Database, "String", "Name of the Database" },
  339.             { PARAM_Recordset, "String", "Name of the Recordset" },
  340.         };
  341.         return info;        
  342.     }
  343.  
  344.     // The init() method is called by the AWT when an applet is first loaded or
  345.     // reloaded.  Override this method to perform whatever initialization your
  346.     // applet needs, such as initializing data structures, loading images or
  347.     // fonts, creating frame windows, setting the layout manager, or adding UI
  348.     // components.
  349.     //--------------------------------------------------------------------------
  350.     public void init()
  351.     {
  352.         if (!m_fStandAlone)
  353.             GetParameters(null);
  354.         // If you use a ResourceWizard-generated "control creator" class to
  355.         // arrange controls in your applet, you may want to call its
  356.         // CreateControls() method from within this method. Remove the following
  357.         // call to resize() before adding the call to CreateControls();
  358.         // CreateControls() does its own resizing.
  359.         //----------------------------------------------------------------------
  360.         resize(320, 200);
  361.  
  362.         // Set the name of the database (parameter)
  363.         if (m_fStandAlone)
  364.             // use the value on the command line for the MDB filename
  365.             filename = m_Database;
  366.         else
  367.         try {
  368.             // otherwise generate it relative to the applet
  369.             java.net.URL fn;
  370.             fn = new java.net.URL(getDocumentBase(), m_Database);
  371.             // strip away the "file:/" from the URL
  372.             filename = fn.getFile().substring(1);
  373.         } catch(Exception e) {
  374.             showStatus("Error: " + e.getMessage());
  375.         }
  376.  
  377.         _DBEngine idbengine = (_DBEngine) dao_dbengine.create();
  378.  
  379.         // Open database
  380.         Variant vExclusive = new Variant();
  381.         Variant vReadOnly = new Variant();
  382.         Variant vConnect = new Variant();
  383.         vExclusive.putBoolean(false);
  384.         vReadOnly.putBoolean(readOnly);
  385.         vConnect.putString("");
  386.         database = idbengine.OpenDatabase(filename, vExclusive, vReadOnly, vConnect);
  387.  
  388.         // Create a new recordset
  389.         Variant vOpenType = new Variant();
  390.         Variant vOptions = new Variant();
  391.         vOpenType.putShort((short)RecordsetTypeEnum.dbOpenDynaset);
  392.         vOptions.putShort((short)(readOnly ? RecordsetOptionEnum.dbReadOnly : 0));
  393.         recordset = database._30_OpenRecordset(m_Recordset, vOpenType, vOptions);
  394.  
  395.     }
  396.  
  397.     // Place additional applet clean up code here.  destroy() is called when
  398.     // when you applet is terminating and being unloaded.
  399.     //-------------------------------------------------------------------------
  400.     public void destroy()
  401.     {
  402.     }
  403.  
  404.     // simpledao Paint Handler
  405.     //--------------------------------------------------------------------------
  406.     public void paint(Graphics g)
  407.     {
  408.     }
  409.  
  410.     //        The start() method is called when the page containing the applet
  411.     // first appears on the screen. The AppletWizard's initial implementation
  412.     // of this method starts execution of the applet's thread.
  413.     //--------------------------------------------------------------------------
  414.     public void start()
  415.     {
  416.         // Create the form, passing it the recordset
  417.         m_form = new SimpleForm(recordset);
  418.  
  419.         // Add it to the applet
  420.         add("Center", m_form);
  421.  
  422.         // Get the first record in the recordset
  423.         recordset.MoveFirst();
  424.  
  425.         // Display that record
  426.         m_form.showData();
  427.  
  428.     }
  429.     
  430.     //        The stop() method is called when the page containing the applet is
  431.     // no longer on the screen. The AppletWizard's initial implementation of
  432.     // this method stops execution of the applet's thread.
  433.     //--------------------------------------------------------------------------
  434.     public void stop()
  435.     {
  436.         // Close the recordset
  437.         recordset.Close();
  438.         recordset = null;
  439.  
  440.         // Remove the form
  441.         remove(m_form);
  442.         m_form = null;
  443.     }
  444.  
  445.  
  446.     public boolean action(Event  evt, Object  what)
  447.     {
  448.         // Handle click on the First button
  449.         if (evt.target == m_first)
  450.         {
  451.             // Update the status bar, if available
  452.             if (! m_fStandAlone) showStatus ("Getting first record");
  453.  
  454.             // Move to the requested record
  455.             recordset.MoveFirst();
  456.  
  457.             // Show that record
  458.             m_form.showData();
  459.  
  460.             // Event handled
  461.             return true;
  462.         }
  463.         
  464.         // Handle click on the Prev button
  465.         if (evt.target == m_prev)
  466.         {
  467.             // Update the status bar, if available
  468.             if (! m_fStandAlone) showStatus ("Getting previous record");
  469.  
  470.             // Move to the requested record
  471.             recordset.MovePrevious();
  472.  
  473.             // Show that record
  474.             m_form.showData();
  475.  
  476.             // Event handled
  477.             return true;
  478.         }
  479.         
  480.         // Handle click on the Next button
  481.         if (evt.target == m_next)
  482.         {
  483.             // Update the status bar, if available
  484.             if (! m_fStandAlone) showStatus ("Getting next record");
  485.  
  486.             // Move to the requested record
  487.             recordset.MoveNext();
  488.  
  489.             // Show that record
  490.             m_form.showData();
  491.  
  492.             // Event handled
  493.             return true;
  494.         }
  495.         
  496.         // Handle click on the Last button
  497.         if (evt.target == m_last)
  498.         {
  499.             // Update the status bar, if available
  500.             if (! m_fStandAlone) showStatus ("Getting last record");
  501.  
  502.             // Move to the requested record
  503.             int SYNCHRONOUS = 0;
  504.             recordset.MoveLast(SYNCHRONOUS);
  505.  
  506.             // Show that record
  507.             m_form.showData();
  508.  
  509.             // Event handled
  510.             return true;
  511.         }
  512.         
  513.         // Event not handled
  514.         return false;
  515.     }
  516.  
  517. }
  518.