The ActiveX Data Objects (ADO) object model provides a way to access data from your Visual J++ applications. You can use it to access any data source that is compatible with Open Database Connectivity (ODBC). ADO is also accessible from a variety of programming languages, such as Microsoft Visual Basic and Microsoft Visual InterDev, and from applications, such as Microsoft Access and Microsoft Excel. It is these robust capabilities that makes ADO such an attractive database object model.
Because many applications, scripting languages, and macro languages use some aspect of Microsoft Visual Basic, the examples provided in the ADO documentation demonstrate how to access ADO using Visual Basic syntax. Although the ADO object model provides a consistent way to access data from any programming language, there are some important differences in how ADO is used with Visual Basic versus Visual J++. In addition to syntax differences, there are also slight differences in how to perform operations in ADO. To use the examples provided in the ADO documentation from within Visual J++, it is important to understand how to translate these examples into Visual J++ syntax.
Consider the following points when translating the examples from Visual Basic to Visual J++:
If you do not want to import the entire com.ms.wfc.data and com.ms.wfc.data.ui packages into your class file, you must include the package name each time you use a class or an enumerated value. The following code example demonstrates how to do this:
public void GetData()
{
com.ms.wfc.data.Connection cnn = new com.ms.wfc.data.Connection();
}
Dim cnn as Connection
Dim cmd as Command
Dim rs as Recordset
Set cnn = new Connection
Set cmd = new Command
Set rs = new Recordset
In Visual J++, creating an ADO object is the same as creating other objects. For example, you use the following syntax to define the objects shown in the preceding example:
Connection cnn = new Connection();
Command cmd = new Command();
Recordset rs = new Recordset();
Dim cnn as Connection
Set cnn = new Connection
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb"
To specify a path to a database in Visual J++, you use two backslashes (\\). In addition, you use two backslashes when inserting the backslash character into a string. The following example illustrates setting the connection string for a Connection object:
Connection cnn = new Connection();
cnn.setConnectionString("Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=C:\\Program Files\\Microsoft Visual Studio\\VB98\\Nwind.mdb"
Dim Sub Simple()
Dim rs as Recordset
If rs.EditMode == adEditAdd Then
//Do something here
End If
End Sub
In Visual J++, the constants used with ADO are found in the classes defined in the com.ms.wfc.data.AdoEnums class. In addition to omitting the ad prefix from the constant, Visual J++ ADO constants are also uppercase. The following example illustrates how to use an ADO constant in Visual J++:
public void Simple()
{
Recordset rs = new Recordset();
if (rs.getEditMode() == AdoEnums.EditMode.ADD)
{
//Do something here
}
}
Public Sub OptionalP()
Dim cnn as Connection
Set cnn = New Connection
'Only the connection string parameter is used.
cnn.Open "DSN=Pubs;UID=sa;PWD=pwd;", , ,_
ConnectOptionEnum.adConnectAsync
End Sub
Visual J++ does not support optional parameters. Instead, it provides overloaded versions of methods. You call a method based on the type of information provided in the method parameters. For example, in the Connection.open method, there are six overloaded versions. However, if the overloaded methods do not provide the signature you need, you can call the Connection.open method in a similar way to the preceding Visual Basic code. You specify null for those parameters that you do not want to provide values for. For non-object parameter types, you can pass an empty or zero value. The following example demonstrates how to call the Connection.open method in a way that is similar to how the Visual Basic version is called:
public void OptionalP()
{
Connection cnn = new Connection();
/* To avoid the username and password parameters, pass null
values to the open method.*/
cnn.open("DSN=Pubs;UID=sa;PWD=pwd;", null, null,
AdoEnums.ConnectOption.ASYNCCONNECT);
}
Public Sub GetData()
//This code assumes a valid connection to a data source
Dim rs as Recordset
Set rs = new Recordset
rs!FirstName = "George"
rs.Update
End Sub
To access the value of a field in Visual J++, you specify the Field object directly and use either the getValue or setValue method. If you know the field's data type, you can also set and get the value using the data type-specific methods of the Field object. For example, you can call the setBoolean method to assign a boolean value to the field. The following example demonstrates how to set the value of a field in Visual J++:
public void GetData()
{
//This code assumes a valid connection to a data source
Recordset rs = new Recordset();
rs.getField("FirstName").setString("George");
}
'The following is the Visual Basic way of connecting to a database 'through code.
Public Sub DoData()
Dim cnn as Connection
Dim cmd as Command
Dim rs as Recordset
Set cnn = New Connection
Set cmd = New Command
Set rs = New Recordset
cnn.Open "DSN=Pubs;UID=sa;PWD=pwd;"
cmd.ActiveConnection = cnn
cmd.CommandText = "Select * From Customers"
rs.Open cmd, , adOpenKeyset, adLockOptimistic
'Display the number of records in the recordset
MsgBox "Record Count = " + rs.RecordCount
End Sub
/*The following code is the Visual J++ way of performing the code above using the DataSource class*/
public void DoData()
{
DataSource ds = new DataSource();
ds.setConnectionString("DSN=Pubs;UID=sa;PWD=pwd;");
ds.setCommandText("Select * FROM Customers");
ds.begin();
'//Display the number of records in the recordset
MessageBox.show("Record Count = " +
String.valueOf(ds.getRecordset().getRecordCount()));
}