IBM Books

Embedded SQL Programming Guide


Creating and Running JDBC Applets and Applications

Whether you're writing an application or applet, you would typically call JDBC APIs to:

  1. Import the appropriate Java packages and classes (java.sql.*).

  2. Load the appropriate JDBC driver (COM.ibm.db2.jdbc.app.DB2Driver for applications; COM.ibm.db2.jdbc.net.DB2Driver for applets).

  3. Connect to the database, specifying the location with a URL (as defined in Sun's JDBC specification) and using the db2 subprotocol. For applets, you must also provide the userid, password, host name, and the port number for the applet server; for applications, the Client Application Enabler provides the required values.

  4. Pass SQL statements to the database.

  5. Receive the results.

  6. Close the connection.

After coding your program, compile it as you would any other Java program. You don't need to perform any special precompile or bind steps.

Distributing and Running a JDBC Applet

Like other Java applets, you distribute your JDBC applet over the World Wide Web (WWW). Typically you would imbed the applet in a hypertext markup language (HTML) page. For example, to call the sample applet DB2Applt.java, (provided in sqllib/samples/java) you might use the tag:

   <applet code="DB2Applt.class" width=325 height=275 archive="db2java.zip">

To run your applet, you need only a Java-enabled Web browser on the client machine. When you load your HTML page, the applet tag down loads the Java applet to your machine which then downloads the Java class files, including the COM.ibm.db2.jdbc.net class which is DB2's JDBC driver. When your applet calls the JDBC API to connect to DB2, the JDBC driver establishes separate communications with the DB2 database through the JDBC applet server residing on the Web server.

For your applets to run, you need to ensure that the correct files are installed in the proper places, as follows:

  1. Install DB2 (server or client) on the same machine as your Web server. The Java applet and JDBC support is installed as part of DB2.

  2. Create your own HTML file, or use a modified version of the supplied DB2Applt.html file and move it to the same directory as your class file. Copy your .class file, or for the sample, sqllib/samples/java/samples.zip and the sqllib/java/db2java.zip file to this directory.

  3. Start DB2's JDBC applet server on your Web server by typing:
       db2jstrt portno
    

    where portno is an unused TCP/IP port number that applets can use.

  4. On your client machine, start your Web browser and load your HTML file to run your applet.

Distributing and Running a JDBC Application

Distribute your JDBC application as you would any other Java application. As the application uses DB2's CAE to communicate with the DB2 server, you have no special security concerns; authority verification is performed by the CAE.

To run your application on a client machine, you must also have installed on that machine:

Start your application from the GUI or command line, like any other application.

A sample application, DB2Appl.java, is provided in the sqllib/samples/java directory. If you have created the SAMPLE database, you can also run the sample by adding samples/java/samples.zip to your CLASSPATH environment variable, changing to the sqllib/samples/java directory, and entering the following command:

   java DB2Appl

See SQL Reference for information on the SAMPLE database.

Extensions

DB2 Universal Database supports the JDK Version 1.1 JDBC specification. You may have to modify applications and applets that are written to the JDBC Version 1.0 to work properly with DB2 Version 5.

There are also special considerations for graphical and large objects (LOBs).

Using Graphical and Large Objects

The JDBC specification does not explicitly mention large objects (LOBs) or graphic types.

Treat LOBs as the corresponding LONGVAR type. Because LOB types are declared in SQL with a maximum length, ensure that you do not return arrays or strings longer than the declared limit. This consideration applies to SQL string types as well.

For GRAPHIC and DBCLOB types, treat them as the corresponding CHAR types. The following JDBC APIs behave as described below:

setString
Converts from Unicode to database format.1

setAsciiStream
Converts from local code page to database format.2

setUnicodeStream
Converts from Unicode to database format.1

getString
Converts from database format to Unicode.1

getAsciiStream
Converts from database format to local code page format.2

getUnicodeStream
Converts from database format to Unicode.1
Notes:
  1. The DB2 client first converts the data from the database format to the client format; DB2 then converts the data to Unicode
  2. The DB2 client performs this type conversion. See your DB2 client information for supported code pages and conversions.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]