Accessing Microsoft Active Data Objects (ADO) from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access Microsoft Active Data Objects (ADO) from Java. J-Integra® for COM is a Java interoperability component that bridges Java and Microsoft Active Data Objects (ADO). It provides bi-directional access of Java objects and COM components.

Contents

  1. Introduction
  2. Run the Java Client on Local Windows Machine
    1. Configure Your Environment
    2. Create a Database
    3. Generate the Java Proxies for ADO
    4. Create the Example
    5. Compile and Run the Example
  3. Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc
  4. More About ADO Programming

1 Introduction

This example shows you how to programmatically automate ADO from Java using the COM API that ADO exposes, in order to execute an ADO query in Java. You can run the Java client on a Windows machine to access its local ADO, or run the Java client on a non-Windows machine (such as Linux) to access ADO installed on a remote Windows machine.

We assume that you are familiar with Java -- no COM knowledge should be required. We assume you have downloaded and expanded the J-Integra® kit from http://j-integra.intrinsyc.com/ and installed it correctly.

2 Run the Java Client on Local Windows Machine

You can try this example on local Windows machine first to get a feel for how easy it is to use J-Integra® to access ADO from Java. Once you make it working on local machine, you can then try to run the Java client on a non-Windows machine to remotely access ADO on another Windows machine.

2.1 Configure Your Environment

This example assumes that you have installed

We will be performing this example under C:\pure. Create that directory, and an msado15 directory under it. Update your path environment variable to include the JDK and J-Integra® bin directories, and update your CLASSPATH environment variable to include the J-Integra® runtime:

C:\>mkdir pure
C:\>cd pure
C:\pure>mkdir msado15
C:\pure>set PATH=C:\jintegra\bin;C:\j2sdk1.4.2_01\bin;
C:\pure>set CLASSPATH=.;C:\jintegra\lib\jintegra.jar

2.2 Create a Database

You can click here to download this Microsoft Access database products.mdb, or create your own products.mdb. Put products.mdb in your C:\pure directory:

Accessing Microsoft Active Data Objects (ADO) from Java: Create a Database

2.3 Generate the Java Proxies for ADO

Type Libraries contain information about COM Components. We will be using the information contained in the type library of ADO to generate pure Java proxies which can be used to access ADO from a standard JVM.

IMPORTANT: You will need to download the ADO proxy files from the Knowledge Base. There is a glitch in the ADO type library which causes com2java to generate Java proxies which cause some of the ADO methods to fail. The file you need to download is msado15.zip. Unzip and copy msado15.jar to your hard drive and place it in your CLASSPATH before running the rest of the example. Normally you would need to use com2java tool as shown below:

  1. Start the com2java tool:
    Accessing Microsoft Active Data Objects (ADO) from Java: Run com2java

    If you get an error about the command not being found, check that you set your path environment variable correctly above.
  2. Select Options... from the Settings menu to check the options and click OK..
    Accessing Microsoft Active Data Objects (ADO) from Java: Set com2java options
  3. Select C:\Program Files\Common Files\System\ado\msado15.dll as the type library
  4. Enter c:\pure into the Output Dir field
  5. Enter msado15 into the Java Package field
  6. Click on Generate Proxies to generate the following proxies:

    Accessing Microsoft Active Data Objects (ADO) from Java: Generate proxies

    Take a look at some of the generated files to get a feel for what is being generated.

    You will notice that comments are included in the generated files in order to make them easier to understand. You may wish to use the javadoc documentation generation tool on the files (the comments are compatible with that tool).

2.4 Create the example

Create the file c:\pure\AdoExample.java, by cutting and pasting from your Web browser.

public class AdoExample {

  public static void main(java.lang.String[] args) {

    // DCOM authentication: Make sure NT Domain, NT User, NT Password are valid credentials.
    // Uncomment this line if AdoExample.java remotely accesses ADO:
    // com.linar.jintegra.AuthInfo.setDefault("NT Domain", "NT User", "NT Password");

    // Make sure the path to your .mdb file is correct.
    String dsn = "driver=Microsoft Access Driver (*.mdb);dbq=C:\\pure\\products.mdb";

    try {
      // Specify host name or IP address of ADO machine as parameter if
      // AdoExample.java remotely accesses ADO.
      // msado15.Connection c = new msado15.Connection("123.456.789.0");
      msado15.Connection c = new msado15.Connection();

      c.open(dsn, null, null, -1);
      Object[] recordsEffected = { null };
      msado15._Recordset rs = c.execute("SELECT * FROM Products", recordsEffected, -1);

      if (rs==null) {
        System.out.println("Recordset is empty.");
      } else {
        while (!rs.isEOF()) {
          msado15.Fields fields = rs.getFields();
          int fieldCount = fields.getCount();
          for (int i = 0; i < fieldCount; i++) {
            msado15.Field field = fields.getItem(new Integer(i));
            System.out.println(field.getName() + " = " + field.getValue());
          }
          rs.moveNext();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // Release all remote objects that haven't already been garbage collected.
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
}

Compile it using C:\pure>javac AdoExample.java

If you get a The name specified is not recognized as an internal or external command ... error, then it is likely that you did not set you path environment variable correctly (see the first section of this example)

If you get a AdoExample.java:1: Package msado15 not found in import... error, then your CLASSPATH environment variable does not correctly include a dot: "."

If you get a Package com.linar.jintegra not found in import... error, then your CLASSPATH environment variable does not correctly include jintegra.jar

If you get an error like error: File .\msado15\Connection.java does not contain type msado15.Connection as expected. Please adjust the class path so that the file does not appear in the package msado15, then it is likely that you did not specify the package msado15 when generating the proxy files from the type library.

2.5 Run the example

On the Java client machine, make sure your CLASSPATH and PATH environment variables are set up according to J-Integra® installation instructions. Compile and run the example in  J-Integra®'s native mode (you need to use DCOM mode if remotely accessing ADO):

java -DJINTEGRA_NATIVE_MODE AdoExample

Accessing Microsoft Active Data Objects (ADO) from Java: Run example

3 Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

You can also run the Java client on a remote machine, such as Linux, Solaris, UNIX and AIX. For instance, if you run it on a Linux machine, then you must do the following:

  1. Move the com2java tool to the Windows machine to generate the Java proxies from ADO, and then move the Java proxies from the Windows machine to the Linux machine.
  2. Install J-Integra® (the jintegra.jar file) on the Linux machine and include the jintegra.jar file and generated Java proxies in CLASSPATH.
  3. Use DCOMCNFG to configure ADO on the Windows machine.
  4. Pass correct login credentials to com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");
    Refer to Configuring DCOM for Remote Access for more information about AuthInfo.setDefault.
  5. Pass the IP address or computer name of the ADO machine to the constructor of Connection object in AdoExample.java:
    msado15.Connection c = new msado15.Connection("123.456.789.0");
  6. Move AdoExample.java to the Linux machine. Compile and run it in DCOM mode without using DJINTEGRA_NATIVE_MODE property:
    java AdoExample

4 More about ADO programming

We do not provide the documentation of the generated Java proxies since the Java proxies are just mapped from the programming API of the COM component. For more information about ADO programming, please refer to the ADO Programmer's Reference. The quickest way to start programming Java-to-ADO is to find a VB example first, and then map the VB example to Java.