In order to use the programming samples that are provided, you need a framework in which to plug in the code. Rather than repeat the same framework code for each sample, the necessary code is included here.
Note These programming samples assume that you are familiar with the basic concepts of Visual J++, such as creating a project, adding a new class to the project, and compiling your code. If you are unfamiliar with these concepts, use the online help to view the entries for these concepts.
The framework code consists of three main sections:
Note It is recommended that you close and release the ADO/WFC components before your application terminates. Otherwise, the VM may end up with large cached data sets on its heap, which slows garbage collection and unnecessarily increases the size of the heap. Although none of the samples provided here actually require these measures, developing these habits will help you as your data applications with ADO/WFC become more sophisticated.
Use the following framework code to get your application started. This code assumes that the run
method is invoked sometime after the object is instantiated. (It does not have to be invoked from the main
method, as shown in this example.)
import com.ms.ado.*;
public class DemoFramework
{
public boolean run()
{
Connection c = new Connection();
c.setConnectionString ("dsn=aDSNName;pwd=aName;pwd=aPWD;database=aDatabase");
c.open();
c.setDefaultDatabase("aSpecificDatabase");
System.out.println ("Connection string = " + c.getConnectionString());
System.out.println ("Version = " + c.getVersion());
System.out.println ("Provider = " + c.getProvider());
System.out.println ("State = " + c.getState());
System.out.println ("Attributes = " + c.getAttributes());
// Most of the code samples will be inserted here, after
// the connection has been established successfully.
c.close();
return true;
}
public static void main(String args[])
{
new DemoFramework().run();
}
}
The first line of the framework code is an import
statement, which makes the ADO/WFC components available to the class. The next step is to establish a connection to a data source. Connections can be either durable or transitory. Durable connections actually maintain a link to the data source, across potentially several operations. Most client/server data applications are implemented with durable connections. The Internet changes this, however, with its requirements for not maintaining long-running sessions with clients. Therefore, transitory connections release any link to the data source after each operation, allowing the data source to service other requests. When you specify the ConnectionString property of the Connection object, a transitory connection can later reconnect to the data source.
The Connection object is created in the run
method, and the ConnectionString property is set to a specific system data source name (DSN) through the call to setConnectionString
. (You should replace the DSN and other parameters with values that apply to your site. The names used in this sample are simply placeholders.)
Next, the connection is opened through the open
method. By default, the open
method establishes a durable connection to the data source. If successful, the application can proceed to execute queries against the source’s data. The diagnostic lines that follow simply print out some common information about the connection: what sort of driver is being used, the attributes of the connection, and the provider. The default database is also set for the connection through the call to setDefaultDatabase
. If an update or query fails to specify a specific database when it executes, the connection will assume the command or query should be performed against the default database. This is a convenience function only.
The framework then includes a placeholder comment that indicates where you can insert code from the other samples. Remember to close durable connections when you are through with them. This has the effect of releasing certain resources that will improve the performance of your own code and allow for quicker heap reclamation. Default garbage collection can be sub-optimal when large chunks of memory are in question, such as in the case of a large result set (100K rows). This is critical when the ADO/WFC classes are being used on middle-tier application servers inside business objects.
Finally, the framework code is initiated within the main
method of the class. This is not a mandatory construct. You may want to set up your own class to initiate a connection when a certain event is triggered inside your application by an end user, such as a button click event. You can alter the code to suit your application requirements.
Compile the class and verify that you can connect to a data source at your site. The diagnostic messages will confirm whether you have established a connection. If desired, you may then remove the System.out
lines from the framework code.