Connecting to the Database

The init method is the entry point for the simpledao.java applet. It instantiates the dao_dbengine class, and then opens the database and the recordset using the related parameter values.

public void init()
{
	if (!m_fStandAlone)
		GetParameters(null);
   // If you use a ResourceWizard-generated "control creator" class to
   // arrange controls in your applet, you may want to call its
   // CreateControls method from within this method. Remove the following
   // call to resize before adding the call to CreateControls;
   // CreateControls does its own resizing.
   //----------------------------------------------------------------------
	resize(320, 200);
	// Set the name of the database (parameter).
	if (m_fStandAlone)
		// Use the value on the command line for the MDB filename.
		filename = m_Database;
	else
	try {
		// Otherwise, generate it relative to the applet.
		java.net.URL fn;
		fn = new java.net.URL(getDocumentBase(), m_Database);
		// Strip away the "file:/" from the URL.
		filename = fn.getFile().substring(1);
	} catch(Exception e) {
		showStatus("Error: " + e.getMessage());
	}
    _DBEngine idbengine = (_DBEngine) dao_dbengine.create();
    // Open the database.
   Variant vExclusive = new Variant();
   Variant vReadOnly = new Variant();
   Variant vConnect = new Variant();
   vExclusive.putBoolean(false);
   vReadOnly.putBoolean(readOnly);
   vConnect.putString("");
   database = idbengine.OpenDatabase(filename, vExclusive, vReadOnly, vConnect);
    // Create a new recordset.
   Variant vOpenType = new Variant();
   Variant vOptions = new Variant();
   vOpenType.putShort((short)RecordsetTypeEnum.dbOpenDynaset);
   vOptions.putShort((short)(readOnly ? RecordsetOptionEnum.dbReadOnly : 0));
   recordset = database._30_OpenRecordset(m_Recordset, vOpenType, vOptions);
}

For each variant parameter for the OpenDatabase and OpenRecordset methods, there is a corresponding variant variable. The variant data type maps to the Java com.ms.com.variant class. Use the methods of the class to set the contents of the variable.

In Java, optional parameters are exposed as ordinary parameters. Therefore, a variant object must be specified even if the parameter is declared as optional in the component’s type library. It is possible to omit a specific value for a parameter by using the noParam method of the Java variant class. The noParam method makes the variant object act as a placeholder for a missing optional parameter. For example, the following code demonstrates how to call the OpenDatabase method without specifying values for the optional parameters vExclusive, vReadOnly, and vConnect.

Variant vOptParam = new Variant();
vOptParam.noParam();
database = idbengine.OpenDatabase(filename, vOptParam, vOptParam, vOptParam);