Reading Records from the Database

When the page containing the simpledao.java applet first appears on the screen, the applet’s start method is called. In this sample, this method creates the form, moves to the first field in the recordset, and then displays the field values.

public void start()
{
	// Create the form, passing it the recordset.
	m_form = new SimpleForm(recordset);

	// Add it to the applet.
	add("Center", m_form);

	// Get the first record in the recordset.
	recordset.MoveFirst();

	// Display that record.
	m_form.showData();

}

Reading data and displaying the results is done in the SimpleForm class. The fields on the form are created in the SimpleForm constructor. The showData method displays the value for each field in the recordset in the corresponding field on the form.

// Show the data in the form.
void showData()
{
	// Get the fields in the recordset.
	Fields fields = m_recordset.getFields();
	_Field f;

	// Create a Variant to get the value of each field.
	Variant value;

	// Get the first field.
	f = fields.getItem(name1);
	// Get its value.
	value = f.getValue();
	// Set the field in the form.
	field1.setText(value.toString());

	// Repeat for the other fields.
	f = fields.getItem(name2);
	value = f.getValue();
	field2.setText(value.toString());

	f = fields.getItem(name3);
	value = f.getValue();
	field3.setText(value.toString());

	f = fields.getItem(name4);
	value = f.getValue();
	field4.setText(value.toString());

	f = fields.getItem(name5);
	value = f.getValue();
	field5.setText(value.toString());

	f = fields.getItem(name6);
	value = f.getValue();
	field6.setText(value.toString());

	f = fields.getItem(name7);
	value = f.getValue();
	field7.setState(value.getBoolean());
}

The getItem method is used to retrieve a field from the fields collection. The Java Type Library Wizard generated the getItem method from the declaration of the Item method. All methods declared with the propget or propput attributes are exposed with the get or put prefixes attached to their names. The following Object Description Language (ODL) for the Item method shows the Item method declared with propget.

[id(00000000), propget, hidden]
HRESULT _stdcall Item(
                [in] VARIANT Index, 
                [out, retval] Field** ppfld);

Note To browse the DAO version 3.5 object library, use the OLE\COM Object Viewer. In Microsoft Developer Studio, click OLE\COM Object Viewer on the Tools menu. In the OLE\COM Object Viewer, click View TypeLib on the File menu. In the File Open dialog box, select \Program Files\Common Files\Microsoft Shared\DAO\dao350.dll.

If you are familiar with using DAO from within Visual Basic, you will not recognize Item as a property of the Fields collection. The Java expression:

Fields.getItem(name5)

is equivalent to the Visual Basic expression:

Fields(name5).

Because the Item method is marked as hidden, Visual Basic will not display it as a property of the Fields collection. Also, because Item is the default property, as identified by id(00000000), it is not necessary to explicitly reference it in Visual Basic.

The Item method takes a variant parameter that can be either the name or the ordinal position of the Field object to return. In this sample, the variant variables that contain the names of the fields (name1..name5) are initialized in the constructor for SimpleForm.

The variables fields and f are not explicitly released at the end of the method. The function relies on Java’s garbage collection to clean things up. In this example, it is fine to let Java clean things up. However, there may be instances when you want to explicitly release an object. The following code uses the ComLib object found in com.ms.com to release the fields object.

ComLib.release(fields);