Accessing Microsoft Active Data Objects (ADO) from Java |
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.
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.
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.
This example assumes that you have installed
the JDK under C:\j2sdk1.4.2_01
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
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:
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:
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).
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.
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
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:
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.