Generating a Java Database Applet or Application

 

 

 

 

 

 

Code which uses Microsoft's Data Access Objects (DAO), ActiveX Data Objects (ADO), or Remote Data Objects (RDO) is converted to Java code which uses the Diamond Edge ADO library built on top of JDBC (Java Database Connectivity). Prior to generating the Java Applet, test your VB form to ensure it functions as desired.

Figure 12: Java Applet Displaying Data from ODBC Data Source

NOTE: The JDBC data source and database names you assign to an applet must exactly match the ODBC data source and database names used to test the Visual Basic form prior to converting it to a Java applet. For example, if you use an ODBC data source named "dmv " to test the Visual Basic form, you must use "dmv" as the JDBC data source name for the applet converted from the VB form.



Converting VB Code into Java Source Code

After you test the form to make sure the generated VB data access code works, click the Make Java button on the VB Converter toolbar to generate Java source code including the corresponding JDBC data access code.

When you click the Make Java toolbar button, VB Converter immediately converts your VB form into Java source code and then automatically compiles the source code into Java byte code using the Java compiler specified in the VB Converter Options dialog.

During conversion, the progress window is displayed which lists each original source file that is converted into Java and reports any associated errors. Java compilation errors are also listed in the window. Each error message also lists the line number in the file. This line number is for the physical disk file which for Forms starts with the resource information which precedes the actual source code.

Several types of files are used when converting your form into a Java applet. For example, if you convert a VB form named Applet1 into a Java applet, the following types of files are generated:

Filename.................File Description

ApplPrj.vbp............Original VB Project file
Applet1.frm.............Original VB Form file
Applet1.frx..............VB Binary Data File
Applet1.java............Java Source Code for the Form
Applet1.class...........Java Byte Code for the Form
ApplPrj.class...........Java Byte Code for application / applet
ApplPrj.html............Sample HTML File

Note: The above assumes that the file name and project or form name is the same. The Java file names are based on the name of the Form or Project.



Troubleshooting Java Conversion Problems

Before you use the Make Java button, check to make sure your code includes only supported VB statements that can be automatically converted into Java syntax. If your VB code includes unsupported statements, you will receive one or more syntax error messages that identify the problem(s). After you modify your VB code, click the Make Java button again to convert it into Java. Refer to the Help file for information on supported VB language statements and features.

Certain types of Java syntax errors are not reported until the Java compiler is invoked. So you may see errors from the Java compilation in the report generation window.



Running the Generated Java Application

After you've converted your VB code into Java byte code, click the Run Java toolbar button to run the Java application. The generated Java application retrieves data using the diamondedge.ado classes which are build on top of the java.sql API, more commonly known as JDBC. The diamondedge.ado library is a 100% pure Java implementation of Microsoft's Active Data Objects (ADO).

NOTE: To run a Java applet that connects to a JDBC data source from an HTML page and view it in a Web browser, the browser must support JDK version 1.1 (or higher), which includes JDBC support. If your browser does not support JDBC, you can run the applet in the Applet Viewer program which is bundled with the Java SDK from Sun.



Viewing the Generated Java Data Access Code

You can view the Java source code that is automatically generated in an ASCII text editor such as Windows Notepad. Comparing the generated Java code to your original VB code provides an easy way to begin learning Java syntax. The code is located in the output directory specified in the Options dialog. By default this is in the Java directory underneath the "install directory."

For example, compare the following VB code, which is used to access the sample DAO dmv ODBC data source used in this documentation to the corresponding ADO data access code on the next page.



Visual Basic GetRecordSet Procedure

Set db = OpenDatabase("", False, False, ";DSN=dmv;UID=ADMIN;PWD=******")
Set rs = db.OpenRecordset("SELECT * FROM Driver")

While (Not rs.EOF)
     For ct = 0 To rs.Fields.Count - 1

         If (IsNull(rs.Fields(ct).Value) = False) Then
             szField = CStr(rs.Fields(ct).Value)

             `TODO: Your Recordset Manipulation Code

         End If

     Next ct
     rs.MoveNext

Wend

rs.Close
db.Close



JDBC Data Access Code

db = Connection.OpenConnection( "jdbc:odbc:dmv", "ADMIN", null, "sun.jdbc.odbc.JdbcOdbcDriver" );
rs = MyDatabase.Execute("SELECT * FROM Driver");

while ((!rs.getEOF()))
{
  for (ct = 0; ct <= rs.getFields().size() - 1; ct++)
  {
   if (rs.getFields().getItem(i).getValue().isNull() == false)
    {
      szField = String.valueOf(rs.getFields().getItem(ct).toString());

      //TODO: Your Recordset Manipulation Code
    }
  }
  rs.MoveNext();
}

rs.Close();
db.Close();