Pages

Tuesday 10 April 2018

Accessing ActiveX Data Objects (ADO) With Visual J++

Like Visual C++, Visual J++ offers a number of ways to access ActiveX Data Objects. By far the easiest and most powerful is to use the Windows Foundation Classes, which expose the ADO objects and their members.

Referencing ActiveX Data Objects:-
To use the ActiveX Data Objects within your Visual J++ application through the WFC, you must import the type library with the following statement:
import com.ms.wfc.data.*;

Creating ActiveX Data Objects:-
In order to create an ActiveX Data Object in Visual J++, you must first create a variable to reference that object, as follows:

// define a variable which will be used as a reference to the
// Connection object
Connection con;
// define a variable which will be used as a reference to the
// Recordset object
Recordset rst;

Next, you can create a new instance of an ActiveX Data Object by using the new keyword and assigning it to the variable reference you just defined:

' create a new instance of an ADO Connection object
con = new Connection( );
' create a new instance of an ADO Recordset object
rst = new Recordset( );

These last two steps could be combined into one step with the following code (this is one of the beauties of Java):

// define a variable which will be used as a reference to the
// Connection object and create a new instance for that variable
Connection con = new Connection( );

// define a variable which will be used as a reference to the// Recordset object and create a new instance for that variable
Recordset rst = new Recordset( );

As in any language, it is always a good idea to remove instances of objects that are no longer being used. You can do this in Java with the following lines of code:

' remove the objects
con = null;
rst = null;

Using ADO with Visual J++: An Example:-Example 3-3 illustrates how an ActiveX Data Objects application may be written for the Visual J++ development environment. To create this project, open a Visual J++ Console Application project, and simply replace the code within the Class1.java file with the code from Example 3-3.

If you are having difficulty running this example, remember to have the Biblio.mdb file in the C:\Program Files\Microsoft Visual Studio\VB98 directory, or have the correct directory for the Access database entered in the source code that you run.

Example 3-3. A Simple Visual J++ Example:-

import com.ms.wfc.data.*;
public class Class1
{
public static void main(String args[]) {
// define our variables which will be used as references to the
// Connection and Recordset objects
Connection con = new Connection( );
Recordset rst = new Recordset( );
// create two strings for use with the creation of a connection
// and a recordset
String sConString;
String sSQLString;
// create temporary variables for Execute method call
long lRecordsAffected;
int nCmdType;
// create a new instance of an ADO Connection object
System.out.println("Connection object created.\n");
// open the BiblioDSN data source with the Connection object
sConString = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=C:\\Program Files\\" +
"Microsoft Visual Studio\\" +
"VB98\\Biblio.mdb";
con.open(sConString);
System.out.println("Connection opened.\n");
// create a Recordset object from a SQL string
sSQLString = "SELECT TOP 10 Author FROM Authors";
rst = con.execute(sSQLString);
System.out.println("SQL statement processed.\n");
// retrieve all the data within the Recordset object
System.out.println("Getting data now...\n\n");
while (!rst.getEOF( )) {
System.out.println(rst.getField("Author").getValue( ));
rst.moveNext( );
}
System.out.println("\nEnd of data.\n");
// close and remove the Recordset object from memory
rst.close( );
rst = null;
System.out.println("Closed and removed " +
"Recordset object from memory.\n");
// close and remove the Connection object from memory
con.close( );
con = null;
System.out.println("Closed and removed " +
"Connection object from memory.\n");
}
}

Notice that with the WFC, the implementation of ADO is just as easy as the implementation of ADO within Visual Basic.

No comments:

Post a Comment