A Quick Start: Develop and Run SQLJ Programs Skill Level: Introductory
Total Page:16
File Type:pdf, Size:1020Kb
A quick start: Develop and run SQLJ programs Skill Level: Introductory Yongli An ([email protected]) DB2 UDB Performance Engineer IBM 02 Dec 2004 Learn how to quickly start using WebSphere Studio Application Developer and DB2 Universal Database to develop SQLJ applications following these very detailed steps using a simple servlet application. This tutorial also provides some basic information about these IBM products and SQLJ-related information. Section 1. Before you start About this tutorial This tutorial is written for those who are interested in coding, testing, and running SQLJ applications using WebSphere Studio Application Developer and DB2 Universal Database. It is also for those who are interested in running their applications in a WebSphere Application Server and DB2 UDB environment. If you are looking for a quick start to have some practical experience, and you don't want to sift through the manuals, this tutorial is for you. For users who have DB2, and one or both of WebSphere Application Server and WebSphere Studio Application Developer installed, he can make use of the ZIP file that comes with this tutorial to get firsthand experience, by following the steps described in this tutorial. Prerequisites In order to do the hands-on exercise to get the quick taste of running SQLJ in WebSphere and DB2, the minimum requirements include having an installation of WebSphere Application Server V5.1, DB2 UDB V8.1 FixPack 4 and WebSphere Studio Application Developer V5.1.2. Each product has the trial version. Users can refer to the individual user manual and instructions that come with the downloaded package to install each product. This tutorial is based on the version of each product A quick start: Develop and run SQLJ programs © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 1 of 30 developerWorks® ibm.com/developerWorks for Windows 2000. Here is the list of software I used: • Microsoft Windows 2000, Service Pack 3 • WebSphere Application Server V5.1 • DB2 Universal Database V8.1 FixPack 4 • WebSphere Studio Application Developer V5.1.2 To run the examples or sample code in this tutorial, you'll need to download the souce file . Section 2. Introduction Introduction SQLJ is a set of programming extensions that allow a programmer using the Java programming language to embed statements that provide SQL (Structured Query Language) database requests. The Java language has been getting increasingly popular. As a result, accessing data in a database from a Java program is getting used more and more widely. There are two standard methods to access relational data in a database: using SQLJ and JDBC. With the latest WebSphere Application Server V5 and DB2 UDB V8 supporting running SQLJ programs, and WebSphere Studio Application Developer V5 supporting developing SQLJ programs, users are now offered a set of products to support the usage of SQLJ. This tutorial uses a simple servlet application accessing a DB2 database through SQLJ to demonstrate how easily the user can start developing SQLJ applications using these IBM products. Along the way, some basic information about these IBM products is also introduced. Section 3. JDBC drivers and DB2 UDB V8 JDBC drivers and DB2 UDB V8 This section discusses some general information about JDBC drivers, and the drivers that are supported in DB2 UDB V8. A quick start: Develop and run SQLJ programs Page 2 of 30 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks® JDBC drivers in general In general, Java applications use JDBC for dynamic SQL and they use SQLJ for static SQL. However, because SQLJ can inter-operate with JDBC, an application program can use JDBC and SQLJ within the same unit of work. DB2 Universal Database provides driver support for client applications and applets that are written in Java using JDBC, and for embedded SQL for Java (SQLJ). JDBC is an application programming interface (API) that Java applications use to access relational databases. DB2 UDB support for JDBC lets you write Java applications that access local DB2 data or remote relational data on a server that supports DRDA. SQLJ provides support for embedded static SQL in Java applications. SQLJ was initially developed by IBM, Oracle, and Tandem to complement the dynamic SQL JDBC model with a static SQL model. According to JDBC specifications, there are four types of JDBC driver architectures, as described below. Type 1 : Type 1 includes drivers that implement the JDBC API as a mapping to another data access API, such as Open Database Connectivity (ODBC). Drivers of this type are generally dependent on a native library, which limits their portability. The JDBC-ODBC Bridge driver is an example of a type 1 driver. Type 2 : Type 2 includes drivers that are written partly in the Java programming language and partly in native code. The drivers use a native client library specific to the data source to which they connect. Because of the native code, their portability is limited. Type 3: Type 3 includes drivers that use a pure Java client and communicate with a server using a database-independent protocol. The server then communicates the client's requests to the data source. Type 4: Type 4 includes drivers that are pure Java and implement the network protocol for a specific data source. The client connects directly to the data source. JDBC drivers in DB2 UDB V8 DB2 UDB V8 supports a type 2 driver and a driver that combines type 2 and type 4 JDBC implementations. DB2 UDB V8 also supports a type 3 driver, although this driver is deprecated. The JDBC drivers in previous releases of DB2 UDB for Linux, UNIX, and Windows were built on DB2 CLI (Call Level Interface). The DB2 UDB V8 type 2 and type 3 drivers continue to use the DB2 CLI interface to communicate with DB2 UDB servers. DB2 UDB V8 adds a new DB2 Universal JDBC Driver that is written completely in Java. Below are the drivers that are supported in DB2 UDB V8: DB2 JDBC type 2 driver for Linux, UNIX, and Windows A quick start: Develop and run SQLJ programs © Copyright IBM Corporation 1994, 2008. All rights reserved. Page 3 of 30 developerWorks® ibm.com/developerWorks The DB2 JDBC type 2 driver lets Java applications make calls to DB2 through JDBC. Calls to the DB2 JDBC type 2 driver are translated to Java native methods. The Java applications that use this driver must run on a DB2 client, through which JDBC requests flow to the DB2 server. DB2 Connect Version 8 must be installed before the DB2 JDBC application driver can be used to access DB2 UDB for iSeries data sources or data sources in the DB2 for OS/390 or z/OS environments. The DB2 JDBC type 2 driver supports most JDBC and SQLJ functions that are described in the JDBC 1.2 specification, and some of the methods that are described in the JDBC 2.0 specification. DB2 JDBC type 3 driver for Linux, UNIX, and Windows (deprecated) The DB2 JDBC type 3 driver, which is also known as the applet or net driver, consists of a JDBC client and a JDBC server. The DB2 JDBC applet driver can be loaded by a Web browser along with the applet, or the applet driver can be used in standalone Java applications. When the applet requests a connection to a DB2 database server, the applet driver opens a TCP/IP socket to the DB2 JDBC applet server on the machine where the Web server is running. After a connection is set up, the applet driver sends each of the subsequent database access requests from the applet to the JDBC server through the TCP/IP connection. The JDBC server then makes corresponding DB2 calls to perform the task. On completion, the JDBC server sends the results back to the JDBC client through the connection. The JDBC server process is db2jd. DB2 Universal JDBC driver (type 2 and type 4) The DB2 Universal JDBC Driver is a single driver that includes JDBC type 2 and JDBC type 4 behavior, as well as SQLJ support. When an application loads the DB2 Universal JDBC Driver, a single driver instance is loaded for type 2 and type 4 implementations. The application can make type 2 and type 4 connections using this single driver instance. The type 2 and type 4 connections can be made concurrently. DB2 Universal JDBC Driver type 2 driver behavior is referred to as DB2 Universal JDBC Type 2 Connectivity. DB2 Universal JDBC type 4 driver behavior is referred to as DB2 Universal JDBC Type 4 Connectivity. The Universal Driver is one of the biggest changes in DB2 UDB V8 and it is an architecture-neutral JDBC driver for distributed and local DB2 access. The following tables list the JDBC interfaces and indicate which drivers supports them, the drivers and their supported platforms. Table 1. JDBC drivers for DB2 UDB JDBC driver name Associated DB2 UDB DB2 Universal JDBC Drivercode DB2 UDB for Linux, UNIX, and Windows or DB2 UDB for z/OS JDBC/SQLJ 2.0 Driver for OS/390 DB2 UDB for z/OS DB2 JDBC Type 2 driver for Linux, DB2 UDB for Linux, UNIX, and Windows UNIX, and Windows A quick start: Develop and run SQLJ programs Page 4 of 30 © Copyright IBM Corporation 1994, 2008. All rights reserved. ibm.com/developerWorks developerWorks® Installing the DB2 Universal JDBC Driver If you select JDBC support during the installation of any of the DB2 UDB for Linux, UNIX, and Windows products, the installation program performs some of the installation steps for the DB2 Universal JDBC Driver. The DB2 UDB Version 8 for Windows installation program performs the following tasks: • Installs the db2jcc.jar and sqlj.zip files and adds them to the system CLASSPATH • Installs file db2jcct2.dll, which is required for Universal Type 2 Connectivity, in the sqllib\bin directory Section 4.