Both applications and applets typically perform the following tasks:
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.
The following sample program, DB2Appl.java , demonstrates how to code a JDBC program for DB2.
import java.sql.*; (1) class DB2Appl { static { try { Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance(); } catch (Exception e) { System.out.println(e); } } public static void main(String argv[]) { Connection con = null; (2) // URL is jdbc:db2:dbname String url = "jdbc:db2:sample"; (3) try { if (argv.length == 0) { // connect with default id/password con = DriverManager.getConnection(url); } else if (argv.length == 2) { String userid = argv[0]; String passwd = argv[1]; // connect with user-provided username and password con = DriverManager.getConnection(url, userid, passwd); (4) } else { System.out.println("Usage: java DB2Appl [username password]"); System.exit(0); } // retrieve data from the database System.out.println("Retrieve some data from the database..."); Statement stmt = con.createStatement(); (5) ResultSet rs = stmt.executeQuery("SELECT * from employee"); (6) System.out.println("Received results:"); // display the result set // rs.next() returns false when there are no more rows while (rs.next()) { (7) String a = rs.getString(1); (8) String str = rs.getString(2); System.out.print(" empno= " + a); System.out.print(" firstname= " + str); System.out.print(""); } rs.close(); stmt.close(); // update the database System.out.println("Update the database... "); stmt = con.createStatement(); int rowsUpdated = stmt.executeUpdate("UPDATE employee SET firstnme = 'SHILI' where empno = '000010'");(9) System.out.print("Changed "+rowsUpdated); if (1 == rowsUpdated) System.out.println(" row."); else System.out.println(" rows."); stmt.close(); con.close(); } catch( Exception e ) { System.out.println(e); } } }
Distribute your JDBC application as you would any other Java application. As the application uses the DB2 client to communicate with the DB2 server, you have no special security concerns; authority verification is performed by the DB2 client.
To run your application on a client machine, you must install on that machine:
To build your application, you must also install the JDK for your operating system. For information on setting up your Java environment, building DB2 Java applications, and running DB2 Java applications, refer to the Application Building Guide.
Like other Java applets, you distribute your JDBC applet over the network (intranet or Internet). Typically you would embed 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 following <APPLET> tag:
<applet code="DB2Applt.class" width=325 height=275 archive="db2java.zip"> <param name="server" value="webhost"> <param name="port" value="6789"> </applet>
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 instructs your browser to download the Java applet and the db2java.zip class library, which includes the DB2 JDBC driver implemented by the COM.ibm.db2.jdbc.net class. 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 running on the Web server.
Note: | To ensure that the Web browser downloads db2java.zip from the server, ensure that the CLASSPATH environment variable on the client does not include db2java.zip. Your applet may not function correctly if the client uses a local version of db2java.zip. |
For information on building and distributing Java applets, refer to the Application Building Guide.
JDBC 2.0 is the latest version of JDBC from Sun. This version of JDBC has two defined parts: the core API, and the Optional Package API. For information on the JDBC specification, see the DB2 Universal Database Java Web site .
For information on installing the JDBC 2.0 drivers for your operating system, refer to the Application Building Guide.
The DB2 JDBC 2.0 driver supports the JDBC 2.0 core API, however, it does not support all of the features defined in the specification. The DB2 JDBC 2.0 driver supports the following features of the JDBC 2.0 core API:
Note: | DB2 does not support the use of java.sql.Blob or java.sql.Clob in stored procedures, UDFs, or methods. |
The DB2 JDBC 2.0 driver does not support the following features:
The DB2 JDBC 2.0 driver supports the following features of the JDBC 2.0 Optional Package API:
DB2 provides the following support for the Javing Naming and Directory Interface (JNDI) for naming databases:
Hashtable env = new Hashtable( 5 ); env.put( "java.naming.factory.initial", "COM.ibm.db2.jndi.DB2InitialContextFactory" ); Context ctx = new InitialContext( env );
DB2ConnectionPoolDataSource and DB2PooledConnection provide the hooks necessary for you to implement your own connection pooling module, as follows:
DB2 supports the Java Transaction APIs (JTA) through the DB2 JDBC application driver. DB2 does not provide JTA support with the DB2 JDBC net driver.
This version of the specification is backward compatible with the previous version (1.22). However, the DB2 JDBC 1.22 driver supports LOB types as an extension of the JDBC 1.22 specification, and this extension is not part of the new specification's backward compatibility. This means that existing JDBC applications that rely on the LOB support of the JDBC 1.22 driver may not work with the new driver. For information on the DB2 JDBC 1.22 driver support for LOBs and graphic types, see Using LOBs and Graphical Objects With JDBC 1.22. To fix the problem, consider modifying the application to take advantage of the LOB support offered by the JDBC 2.0 driver.
Note: | You cannot use the DB2 JDBC 2.0 driver support for LOB and graphic types in stored procedures or UDFs. To use LOB or graphic types in stored procedures or UDFs, you must use the JDBC 1.22 driver support. |
However, this solution may not be practical for every situation. As a workaround, you can set the keyword JDBCVERSION to "122" to tell the JDBC 2.0 driver to use the 1.22 version of LOB support. The default is "200" to tell the JDBC 2.0 driver to use the 2.0 version of LOB support. You can set this keyword in db2cli.ini, or as a connection attribute in the getConnection property argument.
Note: | If you use the JDBC 1.22 driver, the JDBCVERSION keyword does not affect LOB support for JDBC. |