Home
DB2 for OS/390 and z/OS: Ready for Java
Contents
1. File Edit View Favorites Tools Help Back gt Qo 2 A Qsearch Favorites meda C F 5 H T H WebSphere Application Server Administrative Console t Version 5 j f eo Home Save Preferences Logout Help User ID bert defined as x x number of free prepared cann statements per Servers data source Applications Datasource Helper Classname com ibm websphere rsadapter DB2 i The E Resources datastore helper that is JDBC Providers used to Generic JMS Providers parom WebSphere JMS Provi UR WebSphere MQ JMS PI functions Mail Providers Component managed Authentication Alias 7 i References Resource Environment authentication URL Providers RA uum Resource Adapters managed Security signon to the resource Environment TER Container managed Authentication Alias 7 i References System Administration authentication Troubleshooting ied for container managed signon to the resource OK Reset Cancel Figure 5 38 Setting up a datasource definition 2 Click Apply but do not immediately save the new definition Before saving go to the bottom of the new datasource screen and click Custom Properties to define the datasource properties Figure 5 39 on page 95 94 DB2 for z OS and OS 390 Ready for Java a WebSphere Administrative Console Microsoft Internet Explorer ic xi File Edit View Favorites Tools Help Back gt Q A Qsearch sgFavortes media 4
2. 4 d Figure 5 25 Create a new variable DB2JCC DRIVER PATH variable Then click New to define a new variable This takes us to a screen like Figure 5 26 on page 85 where we specify the name of the variable DB2JCC DRIVER PATH a description and its value pp p db2javadriver This is the path where the JCC driver is installed into the USS file system HFS Click Apply when done DB2 for z OS and OS 390 Ready for Java strative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Bak gt 9 A Bsearch sjFavortes media lt 4 4x 5 ig Address amp http wtsc63 itso ibm com 9080 admin secure logon do WebSphere Application Server Administrative Console Version 5 Home Save Preferences User ID bart Logout Help WebSphere Variables cel111 New Servers Applications Resources Security E Environment Configuration v Go e Substitution variables allow specifying a level of indirection for values defined in the system such as filesystem roots Variables can be defined at the server node or cell level When variables in different scopes have the same name the order of resolution is server variables then node variables then cell variables Update Web Server Plugin General Properties Value ippidb2javadriver System Administration
3. Add a String to read SQL statements from param sql SQL text public void addSql String sql addStream new StringReader sq1 Executing statements from each input stream The execute method processes all streams which had been added using one of the methods shown in Example 7 6 in sequence If an exception occurs while reading from a stream it logs the error and continues with the next stream Example 7 7 Spufi execute Read all input streams in sequence and execute SQL statements throws SQLException public void execute Iterator current streams iterator DB2 for z OS and OS 390 Ready for Java while current hasNext 1 Reader reader Reader current next try execute reader 2 catch IOException e Exception reading from the current stream Log and continue with next stream logError e 3 finally try reader close catch IOException ignored Notes on Example 7 7 on page 132 1 Loop over all input streams that had been added using addStream addFile or addSql 2 Reads and executes SQL statements from the current stream until the end of the stream has been reached 3 Log any error that occurred while reading the stream The execute Reader method is not shown here it merely reads SQL statements terminated by semicolons from the stream removing comment lines and calls execute String for each line read It
4. wil 4 Tasks DB Output Servers public class IH t public static void mainiString args try load a driver Class forName com ibm db2 jcc DB2Driver Connection con DriverManager getConnection j bartr2 bartr2 String s Statement stmt ResultSet rs stmt con createStatement i rs stmt executeQuery SELECT EMPNO from dsn871 while rs next s re getString 1 System out println Employee number Ros rs closetl stmt close catch Throwable e e printStackTrace 411 Servers Server Status Server writable Figure 5 21 The tables in DSN8710 database We click DSN8710 EMP and the window in Figure 5 22 on page 82 appears Chapter 5 Setup 81 Data IBM WebSphere Studio Application Developer File Edit Source Refactor Navigate Search Project Run Window Help ot at a jas aa Pes B 9 kd ey Data Definition x J udyclass java x 9 le 5624 6435 j public class Judyclass i 4 public static void main String args try load a driver Class forName com ibm db2 jcc DB2Drive Connection con DriverManager getConnec bartr2 bartr2 Data Definition Navigator String s DSN8710 EMP F E H F Statement stmt ResultSet rs stmt con createStatement rs stmt executeQuery SELECT EMPNO fro while rs next s rs getString 1 System out println Empl
5. 0 ee 125 7 5 Positioned UPDATE and DELETE 0 cee ee 127 7 5 1 Positioned UPDATE EEA AEE ee 127 7 5 2 Positioned DELETE 0 ce eee 128 7 6 Large objects LOBs a p ARE ttt eet eee 128 7 7 Scrollable cursors a a eee 129 7 8 A complete example Poor man s SPUFI 0 000 e eee eee 130 Chapter 8 Getting started with SQLJU 00 002 ce 141 8 1 Creating the source file 2l 142 8 2 Running the Hello application from WSAD sslssslesllee eese eee 145 8 2 1 Creating the launch configuration liiis 145 8 2 2 Specifying command line parameters lilii llis 146 8 3 Running the Hello application from Unix System Services 20 0505 147 8 4 Running the Hello application from MVS batch 0 00 0c 147 Chapter 9 The SQLJ program preparation process 00005 149 9 1 Program preparation in other languages 2 c eee eee 150 9 2 Overview of the SQLJ program preparation process 000 0c eee eee 151 9 2 1 The SQLJ translator 0 0 eee eee 152 9 2 2 More about profiles 20 ak a AETA eh 153 9 3 The DB2 profile customizer 1 0 0 0 0 154 9 3 1 Isolation levels zzi ses te eee ee ae Ge TR EEG ERE iR 155 9 3 2 Why online checking is good for you 2 0 cee eee 155 9 4 The DB2 profile binder llle 156 9 5 The DB2 profile printer raa eh a AE A E BR 156
6. WebSphere Application Server Administrative Console Version 5 User ID bart The User ID does not require a password and does not need to be a User ID of a user in the local user registry It is only used to track user specific changes to configuration data Security is NOT enabled Figure 15 13 WAS administrative console Type in your user ID and click OK Installing a new application This takes you into the administrative console application Expand the Applications tree option and click Install New Application This takes you to a window similar to Figure 15 14 on page 271 Select the Local path option and use the Browse button to find your war file or just type in the complete file name You also need to provide a context root to install your war file into We use itso for our application Click Next to continue 270 DB2 for z OS and OS 390 Ready for Java Back gt A Qusearh Favorites media 4 Baag Address ja http wtsc63 itso ibm com 9080 admin secure logon do WebSphere Application Server Administrative Console Version 5 Home Save Preferences Logout Help User ID cel111 Servers E Applications Enterprise Applications Install Neve Application Resources Local path Security Server path Environment System Administration Troubleshooting Cancel WebSphere Status i WebSphere Configuration Problems Total Workspace Files 0 Browse the
7. Applications E Resources Total 1 JDBC Providers E Scope Cell cel111 Node nd111sc63 Server ws111sc63 Generic JMS Providers C Cell cel111 Use scope settings to limit the availability of resources to a particular cell WebSphere JMS Provider node or server WebSphere MQ JMS Provider C Node di11sc63 When new items are created in this view they will be created within the current scope Mail Providers Server WS111sc63 Resource Environment Providd URL Providers Apply Resource Adapters Security Fitter Preferences neh Delete Environment System Administration Troubleshooting Hame Description E Cloudscape JDBC Driver Cloudscape JDBC Provider Figure 5 30 Create a new JDBC provider A window similar to Figure 5 31 on page 89 is shown Select User defined JDBC provider from the drop down list and click Apply We have to use this option because we use a brand new driver Once the JCC driver is GA WAS will predefine some settings for the driver and you will be able to select it from the drop down list Note that DB2 390 Local JDBC Provider RRS should not be selected This is not the JCC driver but the old current T2 driver that ships with V6 V7 DB2 for z OS and OS 390 Ready for Java 3 WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Ea Bak 9 A Qsearch Favorites media lt 4 B 5 ig Addres
8. Sample test program to retrieve all employees in the EMP sample tables whose salary is in a given range author Ulrich Gehlert public class Hello sql public static context Ctx fsql static iterator Employeelterator String String BigDecimal Load JDBC driver and initialize SQLJ connection context return The SQLJ connection context private static Ctx initialize String url String user String password Appendix B Source code of sample programs 295 throws ClassNotFoundException SQLException Class forName com ibm db2 jcc DB2Driver return new Ctx url user password false public static void main String args if args length 5 System err print1n Usage java Hello class getName url user password min lt max gt return BigDecimal min BigDecimal max Ctx ctx null EmployeeIterator iter null try ctx initialize args 0 args 1 args 2 sql ctx iter SELECT LASTNAME FIRSTNME gt SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN min AND max ORDER BY LASTNAME FIRSTNME h String lastname null String firstname null BigDecimal salary null while true sql FETCH iter INTO lastname firstname Salary h if iter endFetch break System out print n lastname firstname salary new BigDecimal args 3 new BigDecimal args 4 catch Th
9. 00 0 eee eee 269 15 13 WAS administrative console 0 0000s 270 15 14 Install a new application 00 II 271 15 15 Install a new application 2 0 000 en 272 15 16 Install new a application 3 0 00 eee 273 15 17 Install a new application 4 u a 0 00 mh 273 15 18 Install a new application 5 0 ct en 274 15 19 Install a new application 6 0 002 et nh 275 15 20 Save to master configuration llli 275 15 21 Save your changes to the master repository liliis lesen 276 15 22 Administrative Console Main menu 00 lees 276 15 23 Start the application llle 277 15 24 EmployeeListServlet result illii 278 16 1 Including the database access JSP tag libraries llle 281 16 2 New JSP File wizard ek eer m kh ed ws Ro Wain CRUS TRUST als 282 16 8 Select Tag Library 0 cece n 283 Figures xi xii DB2 for z OS and OS 390 Ready for Java Tables 2 1 Methods of a Java applet es 16 2 2 Methods of a HTTP Servlet ee 17 3 1 JDBC drivers and driver types for use with DB2 z OS and OS 390 26 3 2 JDBC Drivers and URL formats 0 0 0 0000 ccc eee 38 3 3 Differences between JDBC and SQLJ summarized 0 00000 49 6 1 DB2 column types and ResultSet getXxx methods 0000000ee 107 7 1 JDBC methods for use with LOB types 0 0000 cee eee 129 10 1 SQLJ isolation
10. llli 189 10 6 3 Declaring a context class 0 0 ee ees 189 10 6 4 Creating an instance of the context class llli eese less 190 10 6 5 Specifying which connection instance to use 00 00 ee 190 10 6 6 Using more than one context class liliis 194 10 6 7 Summary of ConnectionContext methods 0000 eee eee eee 194 10 7 Execution contexts eb Bikes e ER ug RE Et Nana 195 10 8 Interoperability between JDBC and SQLJ 0 0 0 0 ee 198 10 8 1 Converting a JDBC result set into an SQLJ iterator 2 198 10 8 2 Converting an SQLJ iterator into a JDBC result set 0 198 Chapter 11 SQLJ revisited iespiest eregi teiaa mi ee 199 Tt Introd ctloni see ea RE emet re S nip radi EE Eai 200 11 2 Creating the Employee class 0 00 cette 200 11 2 1 Implementing attributes accessors and constructors 201 11 2 2 Implementing the constructor to create new employees 208 11 2 3 Implementing the insert method 0 0 0c ccc eee eee 203 11 2 4 Creating atest driver 2l 204 11 2 5 Verifying that the program worked 000 nauan aaea 207 11 2 6 Implementing the findByPrimaryKey method 000 208 11 2 7 Implementing the delete method 2 0 00 e eee eee 210 11 2 8 Implementing the update method 0 0 eee 211 11 2 9 Implementing the findAll method llle 212 11
11. emp getFirstName printRow out Hired emp getHireDate printRow out Salary emp getSalary catch Exception e throw new ServletException e out printIn table out printIn lt body gt out printIn lt htm1 gt Prints one row of employee information to the servlet output stream The left column shows a label the right column the actual data param out The servlet response output stream param label The label to be displayed in the left column param value The value to be displayed in the right column pr private void printRow PrintWriter out String label Object value throws IOException out printIn lt tr gt out printIn lt td gt label lt td gt out printIn lt td gt value lt td gt out printIn lt tr gt EmployeePicServlet Example B 7 on page 309 shows the code for the EmployeePicServlet that is used to retrieve an employee s picture 308 DB2 for z OS and OS 390 Ready for Java Example B 7 EmployeePicServlet package com ibm itso sg246435 web import java io IOException import java io InputStream import java io OutputStream import javax servlet Servlet import javax servlet ServletException import javax servlet http HttpServlet import javax servlet http HttpServletRequest import javax servlet http HttpServletResponse import com ibm itso sg246435 sqlj Employee
12. Virtual Hosts y Name pez JCC DRIVER PATH i Specifies the symbolic name Manage WebSphere Variable representing a physical path or URL Shared Libraries root Naming i Specifies the absolute path that the symbolic name represents Description Troubleshooting Driver path for the new JCC T2 z OS driver i Provides an optional description for your administrative records ok Reset Cancel ls Figure 5 26 Create new WebSphere variable Because we made a change to the configuration we have to save the change Click Save as shown in Figure 5 27 on page 86 Chapter 5 Setup 85 86 Microsoft Internet Explorer File Edit View Favorites Tools Help EJ Bak gt A Qsearch Favorites media lt 4 Eby 5 H Address E http wtsc63 itso ibm com 9080 admin secure logon do Go um User ID bart Message s cel111 Servers amp Changes have been made to your local configuration Click Save to apply changes to the master configuration E The server may need to be restarted for these changes to todgrfect Applications Resources WebSphere Variables Security DB2JCC DRIVER PATH E Environment Substitution variables allow specifying a level of indirection for values defined in the system such as filesystem roots Variables can be defined a
13. iissiilieleelse eese 81 The columns of the sample EMP table lslseleelsleenrene 82 WAS Administrative Console llis 83 WebSpere Administrative Console llle 83 Create a new variable lllillliele n 84 Create new WebSphere variable liliis 85 Save your changes 0 0000 cect ete eee 86 Save into the Master Configuration 000 00 c eects 86 JDBC providers us ce podre dae delay a cape Benge RT RR wa n oe ates 87 Create a new JDBC provider 0000s 88 Select user defined JDBC Provider 0000 cece eee ee 89 User defined JDBC Provider variables 0000 c eee ee eee 90 User defined JDBC Provider variables 2 0000 0c eee eee tees 90 Copyright IBM Corp 2003 All rights reserved ix 5 34 Display the JDBC provider llle e 91 5 35 Select data sources l l nne 92 5 36 Create new data source 92 5 37 Setting up a datasource definition 2 93 5 38 Setting up a datasource definition 2 llle 94 5 89 Select custom properties liliis eee 95 5 40 Custom Properties window lslse s 96 5 41 Specifying the databaseName 000 cece 97 631 Newclass wizard auian tie eae e ei aa e a aA m hn 102 6 2 Running the application from WSAD asasa sasaaa aeaaea 110 6 3 ClassNotFoundException due to missing JDBC driver 5 110 6 4 Modifying the runtime classpath in the Launc
14. 3 2 3 Type 4 connectivity from a non z OS platform Here we look at another setup using the Type 4 driver In this case our application is running on a non z OS platform Figure 3 5 on page 32 As we are using a pure Java driver the application can be running on any hardware and software as long as it supports a compatible JVM To talk to DB2 for z OS and OS 390 from our Java application we use a Type 4 driver This way we can communicate directly to DB2 for z OS and OS 390 through DRDA All connections to DB2 for z OS and OS 390 come into the system through DDF since that is the DB2 address space that knows how to talk DRDA Note that the application can be an applet running in a browser Chapter 3 Accessing DB2 from Java 31 z OS image z OS image Figure 3 5 Type 4 connectivity from a non z OS platform This configuration is very common when developing applications For example when you are using WebSphere Studio Application Developer WSAD running on a workstation to develop your Java applications you can use the Type 4 driver to test your application and run it against data on a DB2 for z OS system The lower half of Figure 3 5 shows a configuration using the DB2 Connect Server When using the Type 4 driver you do not need DB2 Connect to be able access data on a DB2 for z OS and OS 390 system So you may ask yourself why is there a DB2 Connect Server in the figure if it is not required The reason is
15. Preferences expand the Team subtree CSV File Content and add sqlj to the list of file extensions To create the package and the SQLJ source file do the following 1 In the Java perspective select the project then click the New Java Package icon 2 Enter the package name com ibm itso sg246435 sq1 j and click Finish 3 Now we create the SQLJ source file for our Hello program Select the package then select File New Other Data SQLJ SQLJ File Figure 8 1 and click Next Select eo e Create a new SQLJ file and add SQLJ support to the project ms Component Test Data SQL Eclipse Modeling Framework EJB Example EMF Model Creation Wizards J2EE H Java Plug in Development Remote File Transfer Server Simple Symptom Database UML Visualization Web Web Services XML Examples BS Add SQLJ Support to Projects af SQLI File E Finish Cancel Figure 8 1 Create a new SQLJ file In the dialog that appears Figure 8 2 on page 143 enter Hello in the Name field DB2 for z OS and OS 390 Ready for Java SQLJ File EN Create a new SQLJ file Source Folder 5624 6435 Browse Package com ibm itso sg246435 sqlj Browse Name Hello Modifiers public C defaut private protected abstract Final static SQLJ Support will be added to the project Specify the SQLJ JAR file to be used to resolve SQLJ references
16. SQLJ JAR file C ASQLUIB javalsql zip Browse lt Back Next gt Cancel Figure 8 2 Create a new SQLJ file 2 Then click Finish 4 In the editor window that opens type the source code for our Hello application as shown in Example 8 1 Do not care too much about the SQLJ language constructs for now We describe them in much more detail in Chapter 10 SQLJ tutorial and reference on page 175 Example 8 1 Source code for the Hello application package com ibm itso sg246435 sqlj import java math BigDecimal import java sql SQLException Sample test program to retrieve all employees in the EMP sample tables whose salary is in a given range author Ulrich Gehlert public class Hello sql public static context Ctx 1 sql public static iterator EmployeeIterator String String BigDecimal 2 Chapter 8 Getting started with SQLJ 143 Load JDBC driver and initialize SQLJ connection context Qreturn The SQLJ connection context private static Ctx initialize String url String user String password throws ClassNotFoundException SQLException Class forName com ibm db2 jcc DB2Driver return new Ctx url user password false public static void main String args if args length 5 System err printIn Usage java Hello class getName url user password min max return BigDecimal min new BigDecimal args 3
17. Tasks Console Figure 8 5 Output from running the Hello program 8 3 Running the Hello application from Unix System Services To run the application from Unix System Services follow the same steps as in the JDBC exampe in Running the Hello application from Unix System Services on page 113 If you use a network file system to export the code make sure that your SMB server does not perform an ASCII to EBCDIC conversion for the ser files They are binary files and should not be translated 8 4 Running the Hello application from MVS batch Occasionally you may want to run your Java application in MVS batch for example if it is part of a larger job This is what the BPXBATCH utility program is for in fact BPXBATCH can be used to run any USS application or command script For more information about BPXBATCH refer to Unix System Services Command Reference SA22 7802 Unfortunately the standard output and standard error in Java System out and System err respectively cannot go to a MVS or JES data set they must be HFS files In the sample JCL see Example 8 2 below we call the TSO ocopy program to copy these to a JES data set Example 8 2 Sample JCL to run a Java application in MVS batch BARTJAVA JOB 999 POK BART JOB CLASS A MSGCLASS T NOTIFY amp SYSUID TIME 1440 REGION 0M JOBPARM SYSAFF SC63 Sample job to run a Java application in batch INIT SET JAVA java 1 SET CLASS c
18. When your statement has variable parts in our example the lower and upper limits for the employee s salary you can also construct the entire statement quickly BigDecimal min max PreparedStatement stmt conn prepareStatement SELECT LASTNAME FIRSTNME SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN min AND max Chapter 6 Getting started with JDBC 105 However for performance reasons this is not a good idea The database caches prepared queries so they can potentially be reused However the database cannot know which parts of the statement are fixed and which are variable so the statement above would never be reused except when you supplied exactly the same values for the upper and lower bounds To increase reusability SQL executed using this method can include what are called parameter markers These markers are slots within the statement that represent a value that can be passed from the program to the statement on execution Even though these values can change the statement does not have to be prepared again A parameter marker is a placeholder for the variable parts of the statement It is coded as a in the SQL statement Before the statement can be successfully executed a value must be supplied for each parameter marker The parameter markers are numbered continuously beginning with 1 6 1 4 Populating parameter markers As explained in the previous section we have to supply a value for e
19. gt isolation level it the isolation level you want the package to use for example CS Note that we use DB2 notation UR CS RS RR not JDBC notation 1 2 3 4 to specify the isolation level Note You can also specify the singlepkgname and bindoptions parameters in WSAD V5 1 when using the SQLJ tooling the same way as you specify the collection option see Figure 9 3 on page 159 9 3 2 Why online checking is good for you By default the profile customizer runs with online checking enabled if you entered a value in the URL field in the SQLJ Customization Script dialog Figure 9 3 on page 159 When online checking is enabled the SQLJ customizer queries the DB2 catalog on the target database server in order to verify whether your SQL statements will be able to run on that server Online checking will recognize errors or potential problems such as gt Misspelled names including table or view names column names function names etc gt Lack of authorization for example an INSERT statement on a table for which you the package owner do not have INSERT authority gt Data type mismatches for example trying to UPDATE a numeric column with the value of a String host variable gt Specifying a host variable of Java primitive type to receive the value of a nullable column Without online checking all of these errors will go unnoticed until bind time or even runtime Chapter 9 The SQLJ program preparati
20. gt long position String searchstr long start throws SQLException long position Clob searchstr long start throws SQLException Searches for an occurrence of a String in the CLOB Again it is important to point out that a Blob or Clob object does not actually hold the entire LOB data rather it offers methods to retrieve chunks of the LOB object or to retrieve a stream from which to read the LOB data in manageable pieces Chapter 11 SQLJ revisited 215 216 Example 11 21 shows an updated version of the getPicture method which uses a Lob object returning an InputStream which can then be used by the caller to retrieve the LOB data Also the updated version checks whether a picture has actually been found If not this is not considered an error so an SQLException will not be thrown The applications return a null value instead Example 11 21 Employee getPicture second version import java io InputStream pe Retrieves the employee s picture return An InputStream to read the picture in GIF format or lt code gt null lt code gt if no picture is available ock oko ok ok o FH Gexception SQLException A database error occurred public InputStream getPicture Ctx ctx throws SQLException Blob picture try sql ctx SELECT BMP_PHOTO INTO picture FROM DSN8710 EMP_PHOTO_RESUME WHERE EMPNO empNo h catch SQLException sqlException if sqlException getSQLState equals 02000 row not
21. throws SQLException private Object getColumnValue ResultSet rs int col throws SQLException switch rs getMetaData getColumnType col 1 case Types BLOB Convert BLOB data to a hex string Blob blob rs getBlob col if blob null return null else byte blobBuf blob getBytes 0 MAXCOLWIDTH 2 2 return toHexString blobBuf case Types CLOB Get only the first MAXCOLWIDTH characters of the CLOB column Clob clob rs getClob col if clob null return null else return clob getSubString 0 MAXCOLWIDTH 3 default All other column types return rs getObject col 4 Notes on Example 7 13 1 2 Examine the column type BLOB and CLOB columns get special treatment If it was a non NULL BLOB column read the first MAXCOLWIDTH 2 bytes of the BLOB and return a hex string representation of the data We divide by two since each byte needs two characters in hex format 3 If if was a non NULL CLOB column return the first MAXCOLWIDTH characters 4 Forall other column types simply return the object representation of the column value For example if it was an INTEGER column this will return an instance of java lang Integer or null if the column was NULL Chapter 7 JDBC revisited 137 The main method Finally we show the main method which evaluates the command line arguments sets up the input streams and starts execution of SQL statements Example 7 14 It uses several helper met
22. 15 6 Putting it together 266 In this section we combine the Servlets developed in the previous sections First we have to find a way to pass parameters to a Servlet and to retrieve the parameter values in the Servlet code To solve the first problem there are basically two mechanisms by which a Servlet or any other component creating dynamic Web content for that matter can receive parameters namely the GET method and the POST method The former works by extending the URLs while the latter works on the HTTP protocol level We will not discuss the POST method here since it is primarily used for form input To pass a parameter to a Servlet via the GET method you append the parameter names and parameter values to the URL after a preceding question mark You separate individual parameters by an ampersand amp and parameter names from parameter values with an equals sign DB2 for z OS and OS 390 Ready for Java 15 6 1 Modifying the EmployeeList Servlet First we modify the employee list Servlet so that it displays the individual entries not as plain text but as a hyperlink that can take us to the corresponding detail page Modify the printRow method in EmployeeListServlet as shown in Example 15 10 Example 15 10 EmployeeListServlet printRow modified private void printRow PrintWriter out Employee emp out printIn tr printCol out lt a href EmployeeDetailServlet empno emp getEmpNo emp
23. 16 1 Introduction In the previous chapter we used Servlets to access and display employee information Although we reused the code developed in Chapter 11 SQLJ revisited on page 199 what we basically did was access the database using hand written code that is we manually coded the logic for accessing the data processing the query result sets and generating the HTML output This made the code hard to read and therefore hard to maintain The HTML code to be produced is buried inside the Java code and even with a very simple example like we developed the HTML structure is difficult to understand In this chapter we take a different approach We use JavaServer Pages JSPs which take the same approach as Servlets but the other way round so to speak While our Servlets in the previous chapters were essentially Java code interspersed with HTML JSPs are HTML code interspersed with Java code snippets JSPs are not restricted to generating HTML it may just as well be XML or any other content type supported by clients Note In fact JSPs are actually translated into Servlets by the application server behind the scenes One of the exciting features of JSPs is that the JSP language is designed to be extensible using a feature called custom tag libraries To use a custom tag library in your JSP you declare the tag library in a so called page directive which is part of the JSP syntax Once declared you can use the tags provided by t
24. Contents V 9 6 Preparing an application to use static SQL llle 157 9 6 1 Preparing SQLJ programs to use static SQL through WSAD 157 9 6 2 Doing it yourself Manual program preparation for static SQLJ 166 9 6 3 Running your sqlj program locally on a DB2 for z OS system 172 9 6 4 In summary end ett toi OR Ae eii er Yee A ge aoas a 173 Chapter 10 SQLJ tutorial and reference 0 0 0 ccc eee eee 175 10 1 The basic syntax of SQLJ statements lille eese 176 10 1 1 Executable statements llli ens 176 10 1 2 Iterator declarations liliis ee 177 10 2 Host variables and expressions 0 c eee ae 179 10 3 Null values r eeta aa temone eat ee tate behead ened sie oh pe mre 179 10 4 Datatype mapping o ce cson otitp anderai k RR RR IRI rh 180 10 5 Queries iterators and the assignment statement 00000e eae 182 10 5 1 Using positioned iterators llle 182 10 5 2 Using named iterators 0 eee 183 10 5 3 SQLJ iterators versus cursors lille 184 10 5 4 Holdable iterators liliis 186 10 5 5 Positioned UPDATE and DELETE 0 0 0 eee eee eee 186 10 5 6 Calling stored procedureS 1 0 ccc tees 188 10 6 Connection contexts 0 0 00 teens 188 10 6 1 Setting up and using an implicit connection context 2 04 188 10 6 2 Why the connection context is important
25. Resources Specify the virtual host where you want to install the Web modules contained in your application Web modules can Security be installed on the same virtual host or dispersed among several hosts Environment Apply Multiple Mappings System Administration Web Module Virtual Host Troubleshooting SG24 5435 Web default host v Previous Cancel Step 3 Map modules to application servers Step4 Summary Figure 15 17 Install a new application 4 Chapter 15 Using Servlets to access DB2 273 On the next window Figure 15 18 select the application server you want to intall on WebSphere cel 1 cel111 node nd111sc63 server ws111sc63 in our case and check the box next to SG24 6435 and click Next Bak gt O A Qusearch Favorites GmMeda lt 4 D S Si 5 Address E http wtsc63 itso ibm com 9080 admin secure logon do Go Links 2 pm f Roa WebSphere Application Server Administrative Console eg E Version 5 E Home Save Preferences Logout Help User ID Install New Application cel111 Allows installation of Enterprise Applications and Module Servers Applicati Ei Applications Step1 Provide options to perform the installation Enterprise Applications Install New Application Resources gt Step 3 Map modules to application servers Step2 Map virtual hosts for web modules Security Specify the application server where you want to install modules contained
26. page language java contentType text html charset IS0 8859 1 gt lt meta http equiv Content Type content text html charset IS0 8859 1 lt meta name GENERATOR content IBM WebSphere Studio gt lt meta http equiv Content Style Type content text css gt lt link href theme Master css rel stylesheet type text css gt Chapter 16 JavaServer Pages 283 lt title gt EmployeeList jsp lt title gt lt head gt lt body gt lt p gt Place content here lt p gt lt body gt lt html gt We are now ready to develop the JSP in more less the same way as the Employee List Servlet from Chapter 15 Using Servlets to access DB2 on page 249 The fundamental difference is that we can focus on the HTML code that is on the presentation of the data rather than on Java code to generate HTML The full source code of EmployeeList jsp is shown in Example 16 2 Example 16 2 EmployeeList jsp lt DOCTYPE HTML PUBLIC W3C DTD HTML 4 01 Transitional EN gt lt html gt lt head gt lt taglib uri jspsql prefix sql gt 1 lt page language java contentType text html charset IS0 8859 1 gt meta http equiv Content Type content text html charset IS0 8859 1 gt lt meta name GENERATOR content IBM WebSphere Studio gt lt meta http equiv Content Style Type content text css gt lt link href theme Master css rel stylesheet type text css gt lt title gt Emp
27. version 1 0 author public class EmployeePicServlet extends HttpServlet implements Servlet see javax servlet http HttpServlet void javax servlet http HttpServletRequest javax servlet http HttpServletResponse private static Employee Ctx ctx public void doGet HttpServletRequest req HttpServletResponse resp throws ServletException IOException resp setContentType image bmp OutputStream out null InputStream pic null String empno 000130 String empno req getParameter empno try ctx new Employee Ctx Employee emp Employee findByPrimaryKey ctx empno out resp getOutputStream pic emp getPicture ctx copy pic out if pic null getServletContext getRequestDispatcher images noimage gif forward req resp else copy pic out catch Exception e throw new ServletException e finally Clean up if pic null pic close if out null out close private void copy InputStream in OutputStream out throws IOException byte buf new byte 1024 int nread while nread in read buf gt 0 out write buf 0 nread Appendix B Source code of sample programs 309 JavaServer Pages Here we list both JSPs that we developed in this publication EmployeeList JPS 310 Example B 8 shows the source code for the EmployeeList JSP program Example B 8 EmployeeList IDOCType HTML PUBLIC W3C
28. 18 init 17 service 17 servlets 5 10 249 session beans 20 Set Client Information API 226 SET SYSPARM LOAD 80 SET TRANSACTION ISOLATION LEVEL 155 setBatching 195 setBigDecimal 42 setBlob 129 setBytes 128 setClob 129 setDB2ClientAccountingInformation 227 setDB2ClientApplicationInformation 227 setDB2ClientUser 227 setDB2ClientWorkstation 227 setDefaultContext 189 setUccLogWriter 244 setting up a JDBC provider 87 setting up system variables 83 setXxx 106 Index 333 SF99501 28 SF99502 28 SFSB 20 shared file system 113 S108451 28 S108452 28 S108478 28 S108479 28 SLSB 20 SMB Server Message Block 113 SMEUI 82 SMP E 58 Software Development Kit 7 SPUFI 121 130 SQL unqualified 194 SQL Stored Procedure Builder 64 SQL JRT 41 SQL OLB 41 SQLCODE 241 SQLERRM 242 SQLESETI 226 SQLException 24 43 104 195 230 240 242 SQLException getMessage 104 SQLJ 3 36 41 48 54 142 customization script 155 158 db2sqljcustomize 169 db2sqljprint 156 169 executing as static SQL 171 interoperability with JDBC 198 iterators versus cursors 184 online checking 236 preprocessor 151 profile binder 156 profile customizer 154 profile printer 156 program preparation process 151 runtime 154 sqlj translator 167 translator 151 152 SQLJ iterator 153 SQLJ profile binder 72 SQLJ profile customizer 72 SQLJ profile printer 72 SQLJ translator 41 45 72 194 sglj customize xml 161 sqlj project properties 160 sql
29. Chapter 5 Setup 91 92 3 WebSphere Administrative Console icrosoft Internet Explorer File Edit View Favorites Tools Help EJ Back gt O A A seach xjFavortes Meda lt 4 Shy E Version 5 Home Save Preferences Logout Help User ID bart il i i Native Library Path S DB2JCC DRIVER PATH Mib ij An optional path to any native libraries celt11 alls o s Native Servers path entries are ee separated by using the Applications ENTER key and must E Resources not contain path separator characters JDBC Providers such as or Generic JMS Providers Native paths may WebSphere JMS Provider contain variable symbolic names WebSphere MQ JMS Provider which can be Mail Providers substituted using a Resource Environment Providg variable map URL Providers Implementation Classname com bm db2 joc DB2ConnectionPooll i The Java classname of the JDBC driver z A Resource Adapters implementation Security Appl OK Reset Cancel Environment _Apply Ls Cancel System Administration k Troubleshooting Additional Properties Data Sourges Data Source is used by the application to access the data from the database A data 1 source is created under a JDBC provider which provides the specific JDBC driver implementation class Data Sources Version 4 This is the WebSphere 4 x data source that us
30. Console Palette Snippets 5624 6435 Web JavaSource comfibm 2a r Cnmnare With b Figure 15 3 Running a Servlet in the WebSphere test environment The following dialog prompts you for the TCP IP port number the server will listen on We stick with the default port number of 9080 Click Finish WSAD now creates and starts the test environment and deploys our code Be patient this may take a while When the server has started up WSAD opens a new editor window containing an embedded Web browser and requests the URL by which our Servlet can be invoked http localhost 9080 56G24 6435 EmployeeListServlet The WebSphere test environment passes the request to the Servlet which sends HTML text back to the client Note If something goes wrong and you have to modify the Servlet code you can run it anytime again as described above However we noticed that sometimes the embedded browser window does not reload the page even if you press the Refresh button If this happens simply close the browser window and try again You can also use an external browser to test your application and just copy and paste the URL into the browser s address bar The Web browser window now displays the heading line the text between the h1 and lt h1 gt tags We are now ready for the interesting part 15 2 3 Displaying the employee list Now that the skeleton of our Servlet is ready we can go for the interesting part displaying the list of employees on
31. D Default DB2 JDBC Provider COM ibm db2 jdbc DB2ConnectionPoolDataSource Edit gt Remove Data source defined in the JDBC provider selected above Name JNDI Name Type Resource properties defined in the data source selected above Name Value Server Configura Environment Web Data source Ports Variables Trace Security EJB J2C JMS Applications Figure 15 5 Creating a new data source 3 On the Create a JDBC Provider window select User defined as the database type and User defined JDBC provider as JDBC provider type Figure 15 6 on page 259 and click Next DB2 for z OS and OS 390 Ready for Java 4 Create a JDBC Provider x Create a JDBC Provider Select the type of JDBC provider to create Database type a User defined tem Dez Cloudscape I mnformix Sybase sys zi JDBC provider type User defined JDBC Provider Description Custom JDBC2 0 compliant Provider configuration Bact Finish Cancel Figure 15 6 Create a JDBC Provider 4 Specify a name for the JDBC provider for example IBM Type 2 JDBC provider as well as a description Select com ibm db2 jcc DB2ConnectionPoolDataSource as the implementation class name and add the correct jar files to the class path using the Add External Jars button as shown in Figure 15 7 on page 260 and click the Finish button Chapter 15 Using Servlets to access DB2 259 S Create a JDBC P
32. Each of these bean types handles different elements of a business application Session beans control business logic between the client and the application while entity beans represent business objects typically being an in memory representation of a row in a database table Finally message driven beans provide a link between asynchronous messaging and the synchronous session and entity beans Stateless session beans SLSBs Stateless session beans are the most simple implementation of the EJB model These beans are memory based components that do not maintain data between calls If data is to be processed by an SLSB it has to be passed into the bean with each invocation of any of the bean s methods As stateless session beans match the stateless model of HTTP they are popular for handling access from Web applications Because SLSBs do not maintain data between calls they are very generic in nature which means that they can easily be pooled for efficient reuse As the method call is the same to all clients and no data is stored for a call clients can use any bean of the same type of bean that they require from the pool Once a SLSB has finished processing a client s request it can be returned to the pool for reuse Stateful session beans SFSBs The use of SLSBs is somewhat cumbersome for applications that require data to be maintained across invocations SLSBs can be used for such applications by pushing the session data back to th
33. JDBC Providers gt DB2 JCC T2 20S JDBC Provider gt Data Sources gt DB2JCCT2zOSDataSource gt Custom Properties gt databaseName Custom properties that may be required for Resource Providers and Resource Factories For example most database vendors require additional custom properties for data sources that will access the database Li Mail Providers Configuration Resource Environment URL Providers General Properties Resource Adapters p Scope cells cel111 nodes nd111sc63 servers ws111sc63 The scope of the configured Security i esource This value indicates the nfiguration location for the Environment ol onfiguration file i oo System Administration Troubleshooting Required true Name databaseName i Name associated with this property for example PortNumber and ConnectionURL Value pezv i Value associated with this property in this property set Description This String property specifies the location name of the database that i Text to describe any bounds or well should be used when establishing connections using this data source defined values for this property object If location name is not the local site of the DB2 Subsystem See DB2SQLJSSID property in db2sqljjdbc properties file then location X Figure 5 41 Specifying the databaseName When done click Ok and continue
34. Project then select Java in the left pane and Java Project in the right pane Press Next On the next panel enter the project name SG24 6435 You should leave the checkbox Use default project contents checked Press Finish WSAD automatically changes to the Java perspective 2 Create a Java package for our program to live in In the Package Explorer view select the proiect name then select File gt New gt Package or press the New Java package icon Gt on the toolbar Enter the package name com ibm itso s9g246435 jdbc and press Finish WSAD creates subdirectories under your project contents directory to reflect the package name That is we now have a directory called WSDir SG24 6435 com ibm itso sg246435 jdbc Where WSDir is your WSAD workspace directory 3 Now we create the Java source file for our Hello program In the Package Explorer view select the package then select New gt Class The New Class wizard opens see Figure 6 1 In the Name field type Hel 10 check the appropriate Which method stubs would you like to create boxes and press Finish New Java Class Create a new Java class Source Folder 5324 6435 Browse Package com ibm itso sg246435 jdbc Browse Enclosing type Browse Name Hello Modifiers public C defaut C ate protected abstract final r Superclass java lang Object Browse Interfaces Add Remove Which method stubs would you like t
35. ResultSet rs null try Load the JDBC driver Class forName com ibm db2 jcc DB2Driver Connect to the database server conn DriverManager getConnection url user password Prepare the SELECT statement stmt conn prepareStatement SELECT LASTNAME FIRSTNME SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN AND Set parameters for the SELECT statement stmt setBigDecimal 1 new BigDecimal 30000 stmt setBigDecimal 2 new BigDecimal 50000 Execute the query to retrieve a ResultSet rs stmt executeQuery Iterate over the ResultSet while rs next String lastname rs getString 1 LASTNAME DB2 for z OS and OS 390 Ready for Java String firstname rs getString 2 FIRSTNME BigDecimal salary rs getBigDecimal 3 SALARY System out print1n lastname firstname salary catch SQLException e Print exceptions to the console System err printIn e getMessage catch Exception e System err printIn e finally Clean up try if rs null rs close if stmt null stmt close if conn null conn close catch SQLException ignored Hello sqlj Here Example B 2 we provide the listing for the first SQLJ program Hello sqlj Example B 2 Hello sqlj package com ibm itso sg246435 sqlj import java sql import sqlj runtime ref import java math BigDecimal import java sql SQLException
36. amp 0 00 06 8 23 03 12 31 51 AM Z Overview SQL Activity DML Identification LUWs and oth Requester cor Total DML 7 DBRM Package Select EL Suspensions Insert Times Update Class 1 2 3 Delet Suspensions EUS Other Prepare Locking p Describe Locked Resource Describe table RID List E Open Close Fetch eo O 00O OOGOO O O Figure 9 9 Type of DML statements executed by HELLO2 A more drastic way to demonstrate that the application now indeed uses static rather than dynamic SQL is to free the DB2 packages In TSO go to the DB2 Interactive panel and select FREE PACKAGE Option 5 7 Enter the collection ID and the package name HELLO2 then press Enter Figure 9 10 Note Remember the customizer creates four packages one for each isolation level Since the default isolation level after connecting to the database is CURSOR STABILITY CS it is sufficient for the sake of this example to only free the corresponding HELLO2 package that was bound with CS S FREE PACKAGE SSID DB7Y COMMAND gt 1 Free ALL packages gt to free authorized packages or Enter package name s to be freed 2 LOCATION NAME gt Defaults to local 3 COLLECTION ID gt 6246435 Required 4 PACKAGE ID eee gt HELLO2 to free all packages 5 VERSION ID eese gt Blank or version id
37. boolean SEX BigDecimal SALARY public static EmployeeIterator findAll Ctx ctx throws SQLException EmployeeIterator iter sql ctx iter SELECT EMPNO 300 DB2 for z OS and OS 390 Ready for Java FIRSTNME gt MIDINIT LASTNAME HIREDATE CASE SEX WHEN M THEN 1 ELSE 0 END gt SALARY FROM DSN8710 EMP h return iter public static Employee fetch EmployeeIterator iter throws SQLException String empno null String firstName null String middleInitial null String lastName null Date hireDate null boolean male false BigDecimal salary null sql FETCH iter INTO empno firstName middleInitial gt lastName thireDate male gt salary h if iter endFetch return null Employee emp new Employee empno emp firstName firstName emp lastName lastName emp middleInitial middleInitial emp hireDate hireDate emp male male emp salary salary return emp Ww Retrieves the employee s picture return An InputStream to read the picture in GIF format or lt code gt null lt code gt if no picture is available exception SQLException A database error occurred t public InputStream getPicture Ctx ctx throws SQLException Blob picture try sql ctx SELECT BMP_PHOTO INTO picture FROM DSN8710 EMP_PHOTO_RESUME WHERE EMPNO empNo h Appendix B Source code of sample programs 301
38. gt DB2 JccT2 zOS JDBC Provider gt Data Sources New screen Initially all the fields except the Statement Cache Size and the Datasource Helper Classname fields are empty The Statement Cache Size field is defaulted to 10 and the Datasource Helper Classname is defaulted to a class name DB2 for z OS and OS 390 Ready for Java com ibm websphere rsadapter DB2390LocalDataStoreHelper The Datasource Helper Classname field should not be changed Fill in the following fields as indicated below Name DB2JccT2zOSDataSource JNDI Name jdbc empdb Description New JDBC Datasource Container managed persistence check the box on the screen You may also fill in the Container managed Authentication Alias field if you want to specifiy an authentication alias that your installation has set up to contain the user ID and password that you want associated with all connections obtained from the datasource If you do not specify anything in this field and if the resource reference of this datasource gets bound to in an application that is defined with resauth Container all connections obtained from the datasource will be associated with the server s identity The following screen shots Figure 5 37 and Figure 5 38 on page 94 are a partial view of what the new datasource screen looks like when filled out E WebSphere Administrative Console Microsoft Internet Explorer Fie Edit View Favorites Tools Help Back G A GQsearch Favorites media
39. 39 3 4 Accessing data using SQLJ 0 00 41 3 5 Using JDBG or SQEJ ius eel REL er e BRI Gum ree Rech Reed Rote e 42 3 5 4 SQLJis easier to code noiae eee eee 42 3 5 2 SQLJ catches errors SOONEr 1 teen eee 44 955 3 SOLI IS Taster tuos ere or RO Ha tend Grae Siar aE aes a DEN RR DNI NIS 45 3 5 4 SQLJ provides better authorization control 2l lees 46 3 5 5 SQLJ is more predictable and reliable 0 0 00 ccc eee 48 3 5 6 SQLJ allows for better monitoring liliis eere 48 3 57 SOLI Tooling oval ei nag tont eo RED TA ERROR RR tn ele hare tees e 48 3 5 8 Use JDBC for flexible SQL statements liliis cece eee eee 49 3 5 9 SQLJ JDBC interoperability 2lliesseeee IB 49 3 6 SUMMAN sienna tate detested eien A a ee a a Gane Stud 49 Part 2 Prerequisites and setup aaau cece te ehe 51 Chapter 4 Products and levels Now and soon 0 00 cee ee ences 53 4 14 Products and levels ani ci seed ded hehe weed ahah ee Vea wand REESE 54 ANA NOW See guide adit ore Eat rece NIU e DS NOE ep ee eat iin a eee e 54 4 1 2 8001h4 aea Dadi ed aan doa stew xs esum uices wa dte qat y died itae 54 Chapter 5 Setup 3 sia a nates RR ME ate o eg MR Baa e ER ag 57 5 1 DB2 for OS 390 and z OS V7 sss esu eee ewan RE HH REG KR ew Ga RAUS 58 5 1 1 Installing DB2 SQLJ JDBC support a an anaana 58 5 1 2 Installing the Universal Driver on a z OS or OS 390 platform 58 5 1 3 Requi
40. 6 lle d dg avi w x J EmployeelistServlet java X 39 Web Browser amp Palette x 29 x 4 import javax servlet http HttpServletResponse aj palette t hol avaleble for Z is editor gt DefaultEAR amp m Bm BS 5624 6435 import com ibm itso sg246435 sqlj Employee Ela 3G24 6435 Web 3 I web Site Configuration New EY 4j Web Deployment Descripl Go To b a E1299 JavaSource Open eus El com ibm itso sg2464 Open With gt e Jj Erb scuta pone mployeeListServlet extends HttpServlet B J Serv Copy D J EmployeePicServl Paste A B 9 WebContent Delete ax servlet http HttpServletfvoid javax servlet ht 4 Move d doGet HttpServletRequest req HttpServletRespons Project Navigator Gallery Struts Ex Rename ServletException IOException 8 Attributes Import Export riter out resp getWriter intln DOCTYPE HTML PUBLIC W3C DTD HTML 4 0 Add Bookmark intln cHTML intln HEAD intln lt TITLE gt Employee listingc TITLE No attributes are available Rebuild Project Refresh intln lt HEAD gt intln BODY R2 References gt Declarations L v5 0 Test Environment WebSphere v5 0 E aK A dx Run Validation 2 376 CEST 4e68a2fc WebGroup I SRVEO180I SGZa4 3 Debug on Server 2 796 CEST 4e68a2fc WebGroup I SRVED180I SG2 Attributes styles Thumbnails od Profile on Server Servers
41. BigDecimal max new BigDecimal args 4 Ctx ctx null EmployeeIterator iter null try ctx initialize args 0 args 1 args 2 sql ctx iter SELECT LASTNAME FIRSTNME gt SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN min AND max ORDER BY LASTNAME FIRSTNME h String lastname null String firstname null BigDecimal salary null while true sql FETCH iter INTO lastname firstname salary h if iter endFetch break System out println lastname firstname salary catch Throwable e e printStackTrace finally try if iter null iter close if ctx null ctx close catch SQLException ignored 144 DB2 for z OS and OS 390 Ready for Java w 10 11 12 13 Notes on Example 8 1 on page 143 1 Declares a connection context class for the SQL statements in the program 2 Declares a positioned iterator for the SELECT statement at step 9 3 Loads the JDBC driver 4 Creates and initializes the connection context and returns it For information about connection contexts see Connection contexts on page 188 For now think of a connection context as representing a database connection much like a Connection object in JDBC 5 Unlike in the previous chapter we do not hard code the connection information URL user name and password but expect it as command line arguments The lower and upper limits for the salary are pass
42. Example B 5 EmployeeListServlet package com ibm itso sg246435 web import java io IOException import java io PrintWriter import java sql SQLException import javax servlet Servlet import javax servlet ServletException import javax servlet http HttpServlet import javax servlet http HttpServletRequest import javax servlet http HttpServletResponse import com ibm itso sg246435 sqlj Employee Appendix B Source code of sample programs 305 306 version 1 0 author j public class EmployeeListServlet extends HttpServlet implements Servlet see javax servlet http HttpServlet void javax servlet http HttpServletRequest javax servlet http HttpServletResponse public void doGet HttpServletRequest req HttpServletResponse resp j F B F F F FN throws ServletException IOException PrintWriter out resp getWriter out printIn IDOCType HTML PUBLIC W3C DTD HTML 4 01 Transitional EN gt out printIn HTML out printIn HEAD out printIn lt TITLE gt Employee listing lt TITLE gt out printIn lt HEAD gt out printIn lt BODY gt out printIn lt H1 gt Employee listing lt H1 gt try printTable out catch SQLException e throw new ServletException e getMessage e out printIn lt BODY gt out printIn lt HTML gt out close Print the supplied value to the servlet output stream
43. Improved security for DB2 authentication on page 226 kerberosServerPrincipal See Improved security for DB2 authentication on page 226 gssCredential See Improved security for DB2 authentication on page 226 readOnly Creates a read only connection By default this is false This property is flowed at connect time for Type 4 connectivity deferPrepares When enabled server prepare requests are deferred until execute time reducing network traffic currentSchema Sets the CURRENT SCHEMA special register of the DB2 server Currently not supported by DB2 Universal Database for OS 390 and z OS and will result in either an error or an exception when targeting DB2 Universal Database for OS 390 and z OS servers currentSQLID Sets the CURRENT SQLID special register on DB2 Universal Database for OS 390 and z OS Setting this property will fail when targeting DB2 Universal Database on Windows or UNIX based platforms cliSchema Indicates the schema of the DB2 shadow catalog tables or views to search when you issue a database metadata catalog query retrieveMessagesFromServerOnGetMessage Enables this property to direct all calls to the standard JDBC SQLException getMessage method to invoke a server side stored procedure that retrieves the formatted message text for the error the invocation of the stored procedure starts a separate unit of work By default this property is disabled and the full message text is not returned to the c
44. Machines This technology decreases transaction startup time by creating a common system heap of system classes and other sharable classes This heap is created at subsystem startup time and is then shared across all JVMs running in that subsystem Each transaction running in this subsystem has its own JVM which ensures isolation between transactions However when a transaction completes the JVM is not destroyed but waits to be reused by the next transaction By minimizing the startup cost by sharing system resources and reusing JVMs Java technology is reaping the benefits of the mainframe platform You can get specific details about the Persistent Reusable JVM in the publication New IBM Technology featuring Persistent Reusable Machines SC34 6034 which is available at http wcs haw ibm com servers eserver zseries software java pdf jtc0al00 pdf Along with the development of the base JDK has been the continued development and enhancement of drivers for DB2 for z OS and OS 390 which support the JDBC API specification Initially drivers were provided for Version 5 of DB2 which were reliant on the Chapter 1 A brief history of Java 7 8 ODEC specification a database connectivity API that has been developed for the Windows platform but also implemented in DB2 for OS 390 Since then the move has been to remove the need for ODBC and provide drivers that have native support for DB2 culminating in a Java only driver with DRDA calls int
45. Salary emp getSalary catch Exception e throw new ServletException e Go back to the employee list and click the link for employee 000130 or any other employee for whom a picture has been supplied The final version of the detail page should now look similar to Figure 15 12 on page 269 DB2 for z OS and OS 390 Ready for Java J Emplo las Web J Emplo J Emplo m X webs Jj Emplo http iflocalhost 8080 5624 6435 EmployeeDetailServlet empno 000 Co gt e E Serial 000130 Name QUINTANA DOLORES Hired 1971 07 28 Salary 23800 00 Figure 15 12 Output from EmployeeDetailServlet final version 15 7 Deploying the application to WebSphere for z OS Until now we used the WebSphere test environment of WSAD to develop and test our Servlets Now it is time to deploy the application to a real Web application server 15 7 1 Customizing the Web application to run as static SQL However first of all we want to make sure that we are really running this application using static SQL to get the best performance In WSAD you simply have to generate and run your SQLJ Ant script again as described in Preparing SQLJ programs to use static SQL through WSAD on page 157 Now we are ready to deploy the application on WAS for z OS 15 7 2 Creating a WAR file To deploy an application to WebSphere or another Web application server you first package the application
46. This is a good time to rename the configuration to say Hello SQLJ Now switch to the Arguments tab In the Program arguments text box type the five arguments our program expects separated by blanks see Figure 8 4 4 Launch Configurations EI Create manage and run launch configurations Launch Configurations Name Hello SQLJ FJ Java Application 7J Hello 3J Hello SQL Main Arguments i IRE TL Classpath E Source E Common 4 Java Bean Ju JUnit Program arguments Run time Workbench 4 Server jdbc db2 wtsc63 itso ibm com 33756 DB7 bartr1 bingo 30000 50000 E WebSphere v4 Applicatic Rf WebSphere v5 Applicatic YM arguments Working directory Local director IV Use default working directory Delete Apply Revert aee Figure 8 4 Launch Configurations dialog Also make sure that the CLASSPATH is set up correctly including the db2jcc jar and db2jcc license cisuz jar files Click Run to save the changes to launch the configuration and to run the program again Now the output should look similar to Figure 8 5 on page 147 DB2 for z OS and OS 390 Ready for Java e Console terminated C Program FilesNIBMY WebSphere Studio eclipse jre bin javaw exe 11 11 02 2 59 P gx GEYER JOHN 40175 00 HEMMINGER DIAN 46500 00 KWAN SALLY 38250 00 LUCCHESI VINCENZO 46500 00 PULASKI EVA 36170 00 STERN IRVING 32250 00 THOMPSON MICHAEL 41250 00 4
47. UPDATE DSN8710 EMP SET BONUS 500 00 WHERE WORKDEPT E21 E In some situations however you may want to change columns as you iterate through the result set of a query For example in an interactive application the manager may decide what bonus to give each employee while browsing through the list of all employees in his department You can update or delete the row on which an iterator is currently positioned by executing the positioned UPDATE or DELETE statements respectively sql ctx UPDATE DSN8710 EMP SET BONUS bonus WHERE CURRENT OF iter h sql ctx DELETE FROM DSN8710 EMP WHERE CURRENT OF iter js However you have to declare the corresponding iterator as updateable You do this by adding the clause implements sqlj runtime ForUpdate after the iterator class name again refer to Modifying iterator behavior using the implements and with clauses on page 178 for a discussion of the implements clause sql iterator MyIter implements sqlj runtime ForUpdate columnList DB2 for z OS and OS 390 Ready for Java By default all columns in the result table will then be updateable through this iterator even columns that are not in the SELECT list Optionally you can supply a list of columns you want to update using the with clause sql iterator MyIter implements sqlj runtime ForUpdate with updateColumns COL1 COL2 columnList If you know beforehand which columns you are going to update yo
48. a minimum level of support for the components that are required to achieve this We cover the J2EE technology in more detail in Chapter 2 An introduction to Java 2 Enterprise Edition on page 9 1 2 Java and the z OS and OS 390 platforms 6 One of the questions asked when porting the original Java reference implementation to OS 390 was whether a technology targeted at the consumer device and browser market would be viable on a mainframe server The answer was yes with some reasonable modifications and additions DB2 for z OS and OS 390 Ready for Java Java was first made available with a port of the AIX amp Java Development Kit 1 0 2 directly to OS 390 Unix System Services in 1996 This was followed by the first fully supported release of JDK 1 1 1 for OS 390 in 1997 Some of the initial issues to be handled were purely platform related These included gt Java technology is purely Unicode based whereas the mainframe uses EBCDIC gt Floating point data types that are used in Java were not implemented on OS 390 Java mandates the IEEE floating point standard while the S 390 9 hardware implemented its own proprietary format gt Security in Java was based on layers of defence while mainframe security is principal based access control The solution to these issues were Manage the differences between the mainframe architecture and the Java specifications so while Java uses Unicode internally it was only converte
49. actually uses static SQL Example 9 8 Executing the sqlj program using static SQL C Test SQLJ SG24 6435 gt java com ibm itso sg246435 sq1j Hello jdbc db2 wtsc63 itso ibm com 33756 DB7Y bart bingo 30000 50000 GEYER JOHN 40175 00 HEMMINGER DIAN 46500 00 KWAN SALLY 38250 00 LUCCHESI VINCENZO 46500 00 PULASKI EVA 36170 00 STIEN IRVING 32250 00 THOMPSON MICHAEL 41250 00 9 6 3 Running your sqlj program locally on a DB2 for z OS system 172 To demonstrate the magic of this even better we now demonstrate that you can just take the work that you did on your workstation either manually or using WSAD ship it to the mainframe and run your Java program there just like that FTP the class files and the ser file to z OS You can use the WSAD export facility or if your program is just in your workstation file system use plain FTP to send the following files in binary to your z OS HFS file system Hello EmployeeIterator class Hello class Hello SJProfileKeys class Hello SJProfileO ser Note that we do not ship the source programs as there is no need to retranslate or recompile them on the z OS system Run the program just like that on z OS Provided your system is set up correctly correct CLASSPATH and such you can now just run the program from USS on z OS like this java com ibm itso sg246435 sq1j Hello jdbc db2 wtsc63 itso ibm com 33756 DB7Y bart bingo 30000 50000 Note that we use the exact same c
50. automatically generates the code to release statements However you still have to close Iterators yourself 13 2 JDBC recommendation JDBC applications should call prepared statement setters that match the column type at the server This helps especially in cases where deferred prepare is on and the driver has to guess the data type to send to the server If the driver guesses incorrectly an error may be returned from the server In some cases the driver will attempt a retry of the statement using a different data type This results in two network flows and defeats the advantage of having deferred prepare enabled in the driver 13 3 SQLJ performance considerations The following sections discuss performance topics that are specific to SQLJ applications namely Use of matching data types Positioned iterators vs named iterators Importance of online checking Importance of explain table Rebinding your applications vvvvy 13 3 1 Use matching data types Use the recommended mappings of DB2 to Java data types For example while it is perfectly legal to FETCH a TIMESTAMP column into a String variable you definitely should not do so Chapter 13 Performance topics 235 For one thing using a String is less efficient since the SQLJ runtime has to format the TIMESTAMP column into String format Additionally using a java sql Timestamp variable allows you to control the formatting of the timestamp yourself Also as explained in Alwa
51. db2 jcc sqlj Begin Bind ibm db2 jcc sq j Loading profile com ibm itso sg246435 sq1j Hello SJProfil e e ibm db2 jcc sq j Binding package HELLO1 at isolation level UR ibm db2 jcc sqlj Binding package HELLO2 at isolation level CS ibm db2 jcc sqlj Binding package HELLO3 at isolation level RS ibm db2 jcc sq j Binding package HELLO4 at isolation level RR ibm db2 jcc sq j Bind complete for com ibm itso sg246435 sq1j Hello SJProfi p ppp prm r3 oO e Let us now look at some of the parms that we specified on the db2sqljcustomize command gt url jdbc db2 wtsc63 itso ibm com 33756 DB7Y is the URL of the database we want to customize against Note that we use the same notation as we use inside our program gt user bart is the user ID that is to be used to connect to the database gt password blngo is the password that is to be used to connect to the database gt rootpkgname HELLO is the start of the name that the static DB2 package will have when we bind it against the database gt collection 6G246435 is the name ofthe DB2 collection where that package will be bound into gt com ibm itso sg246435 sqlj Hello SJProfileO ser is the name of the serialized profile that we want to customize Now that we have done a successful customization and bind invoking the binder is done by default let us now look at the serialized profile again Remember that customization not o
52. embedded in a lt code gt td lt code gt element param out The servlet output stream param value The value to be printed private void printCol PrintWriter out String value Ww LIE ML E F FN out printIn lt td gt out printIn value out printIn lt td gt Print employee data to the servlet output stream in an HTML table row param out The servlet output stream param emp The employee to be displayed private void printRow PrintWriter out Employee emp out printIn lt tr gt printCol out lt a href EmployeeDetailServlet empno emp getEmpNo gt emp getEmpNo lt a gt printCol out emp getEmpNo DB2 for z OS and OS 390 Ready for Java printCol out emp getLastName printCol out emp getFirstName emp getMiddleInitial out printIn lt tr gt private static Employee Ctx ctx Prints the table of employee data param out The servlet output stream exception SQLException A database error occurred F E private void printTable PrintWriter out throws SQLException out printIn lt table gt ctx new Employee Ctx Employee EmployeeIterator iter Employee findAl1 ctx try Employee emp while emp Employee fetch iter null printRow out emp out printIn table finally iter close EmployeeDetailServlet Example B 6 shows the EmployeeDetailS
53. jcc sqlj Loading profile com ibm itso sg246435 sqlj Hello SJProfileO java ibm db2 jcc sqlj Binding package HELLO1 at isolation level UR java ibm db2 jcc sqlj The DDM parameter value is not supported DDM parameter code point having unsupported value 0x2112 java ibm db2 jcc sqlj Bind process has failed java BUILD FAILED file C wsad51 workspace SG24 6435 SQLJAntScripts sqlj customize xml 15 Java returned 1 Total time 5 seconds This error occurs when binding against a DB2 V6 or V7 subsystem The DRDA level that they support requires the collection ID to be in upper case This is no longer necessary when binding against a V8 system A future version most likely FixPak 5 of the JCC driver will produce an enhanced error message that makes it easier to identify the cause of the problem Chapter 9 The SQLJ program preparation process 159 Click OK You can select the properties that we just specified by expanding the newly created SQLJAntScripts directory and double clicking the sqlj project properties file It should look somewhat like Figure 9 4 Java sqlj project properties IBM WebSphere Studio Application Developer File Edit Source Refactor Navigate Search Project Run Profile Window Help FB amp g lts x d a e se Ia I e ej IE Package Explorer x wAs 5 0 Test Server J Hello sqlj E sali project properties X B Outline x 39 x 4 Sat Aug 23 00 06 23 PD
54. under an application server on one machine and use the Type 2 driver to talk to DB2 Connect Server which passes on your database requests to DB2 for z OS and OS 390 on another machine The database communication protocol that is used for this is DRDA Note that from the DB2 for z OS and OS 390 point of view these are remote connections coming into DDF The next burning question you may have is why not use a Type 4 driver in all cases and forget about the Type 2 driver in the scenarios where the Java application is running on another platform In both cases DRDA involved so performance is likely to be equivalent The answer is that the Type 4 driver does not support all the functions that the Type 2 driver does The most important one being that currently two phase commit is not supported by the Type 4 driver However this is likely to change very soon 3 2 5 DB2 for z OS and OS 390 as a DRDA Application Requester People that are into very high availability running their WAS systems on zSeries hardware in a parallel sysplex often have the same security guidelines to follow as mentioned before No Web server and database server on the same machine In this case you can have a WAS running your Java applications that talks to a Type 2 JDBC driver that talks through RRS to a DB2 system DB2A that is local to the machine running the WAS This local DB2 then routes all requests to a remote database server DB2B using DRDA This configuratio
55. 177 10 2 Assigning the result set of a query to aniterator 00 0 eee eee eee 182 10 3 Fetching result set rows into host variables llle 183 10 4 Wrong usage of endFetch Do not try this athome 8 183 10 5 Using a named iterator 0 0 0 0 0 ce eae 184 10 6 Cursor declaration includes SELECT statement 0 0000 e eae 184 10 7 Iterator declaration does not include SELECT statement 185 10 8 Positioned UPDATE 0 0 a a a E rrr 187 10 9 Using different connection contexts l l eee 190 10 10 Output from Example 10 9 on page 190 0 02 eee 193 10 11 Synchronizing access to the execution context 0 000 eee eee 196 Copyright IBM Corp 2003 All rights reserved XV xvi 11 1 Employee sqlj skeleton 0 00 cee mh 201 11 2 Employee sqlj with attributes and accessors 000 cece eee eee 202 11 3 Employee constructor 0 2 0 0 00 tees 203 11 4 The Employee insert method 00 eee 204 11 5 The EmployeeTest class 205 11 6 Sample properties file for the EmployeeTest program 00 00000 206 11 7 Create printable representation llle ee 207 11 8 Employee findByPrimaryKey first version llle 208 11 9 Employee findByPrimaryKey second version 20000e eee eens 209 11 10 Modified EmployeeTest main slsleeeeeeeee ee 210 11 11 Employee delete meth
56. 228 DB2 for z OS and OS 390 Ready for Java The driver collects the following timing information as depicted in Figure 12 3 gt Server time the time spent in DB2 itself Network I O time the time used to flow the DRDA protocol stream across the network gt Core driver time the time spent in the driver this includes network I O time and server time gt Application time the time between the start and stop calls v Java Application Universal DB2 Server Driver monitor start prepareStatement executeUpdate executeUpdate monitor stop App Driver I O Server time time time time Figure 12 3 Times collected by DB2 Universal Driver for SQLJ and JDBC monitoring The use of the API is demonstrated in Example 12 4 Example 12 4 Using the DB2 Universal Driver for SQLJ and JDBC monitoring API import com ibm db2 jcc DB2Connection import com ibm db2 jcc DB2SystemMonitor DB2SystemMonitor monitor conn getDB2SystemMonitor monitor enable true monitor start DB2SystemMonitor RESET TIMES SQL statements monitor stop ServerTimeMicros is red line in figure above System out println Server time monitor getServerTimeMicros NetworkIOTimeMicros is orange line in figure above System out println Network I O time monitor getNetworkIOTimeMicros CoreDriverTimeMicros is green line in figure above System out println Co
57. 260 DB2 for z OS and OS 390 Ready for Java 4 Modify Data Source Eg Create a Data Source Select the type of data source to create gl Name Data source for servlet JNDI name jdbc empdb Description DB2 Universal Driver Datasource For testing servlets Category Statement cache size Data source helper class name Connection timeout Maximum connections Minimum connections Reap time Unused timeout Aged timeout Purge policy EntirePool z Component managed authentication alias Container managed authentication alias IV Use this data source in container managed persistence CMP Required field lt Back Next gt Cancel Figure 15 8 Create a data source 8 Now we have to configure a few properties specific to DB2 data sources The following dialog displays a list of configurable properties which you can set in the Value field below the list Select the properties databaseName Figure 15 9 on page 262 serverName and portNumber in turn and enter the corresponding values namely your DB2 subsystem name your z OS system s IP host name or IP number and the DB2 port number Click Finish Chapter 15 Using Servlets to access DB2 261 262 create a Data Source x Create Resource Properties Create the resource properties For this data source Jj Resource Properties databaseName driverType serverName This is a required prop
58. 4 is underway DB2 for OS 390 and z OS V7 at RSU0306 Service for the following APARs necessary to use the DB2 Universal Driver for SQLJ and JDBC was applied PQ62695 PQ72453 For details see Required service on page 58 z OS 1 4 at RSU0306 Java SDK 1 3 1 WebSphere Application Server V5 0 0 for z OS Build level W501000 9 15 2002 WebSphere Studio Application Developer V5 1 WSAD on Windows Not yet generally available when we wrote this publication We downloaded it from an IBM internal site at this level 5 1 0 Based on 20030720_ 2137 Build With the initial SQLJ tooling support added in WSAD V5 1 it is no longer necessary to leave WSAD to run the SQLJ translator or to perform SQLJ profile customization in the DB2 Command Window for the BMP beans session beans and Servlets Java Runtime Environment JRE 1 3 supplied with DB2 UDB V8 and WSAD V5 1 Java 2 Runtime Environment Standard Edition build 1 3 1 Classic VM build 1 3 1 J2RE 1 3 1 IBM OS 390 Persistent Reusable VM build cm131s 20030913 JIT enabled jitc Soon gt DB2 for OS 390 and z OS V7 Universal Driver Type 4 planned for first quarter of 2004 Universal Driver Type 2 planned for first quarter of 2004 Note The DB2 Universal Driver for SQLJ and JDBC will be made available for DB2 for z OS and OS 390 Version 7 through the PTF for APAR PQ80841 This APAR was still open at the time of writing of this publicatio
59. 6 4 Modifying the runtime classpath in the Launch Configurations dialog 3 Press the Add External JARs button In the dialog that opens navigate to the Java subdirectory of your DB2 installation directory by default this will be C Program Files IBM SQLLIB java Select the Jar file containing the DB2 JCC driver db2jcc jar and the Jar file named db2jcc license cisuz jar hold down the Ctrl key to select multiple files Press Open The Jar files have now been added to the runtime classpath Note The db2jcc license cisuz jar file contains a license code that is necessary in order to use the DB2 Universal Driver for SQLJ and JDBC with DB2 for OS 390 and z OS databases Click Run to save the modifications to the launch configuration and re launch the program Chapter 6 Getting started with JDBC 111 If everything went smoothly the output should look similar to Figure 6 5 Congratulations We successfully created and ran our first program to access DB2 for z OS from the workstation Console terminated C Program Files IBM WebSphere Studioteclipseljrelbinljavaw exe 11 11 02 2 59 P p 3x GEYER JOHN 40175 00 HEMMINGER DIAN 46500 00 KWAN SALLY 38250 00 LUCCHESI VINCENZO 46500 00 PULASKI EVA 36170 00 STERN IRVING 32250 00 THOMPSON MICHAEL 41250 00 id Tasks Console Figure 6 5 Output from running the Hello program Tip To show any view in full screen mode double click
60. Chapter 2 An introduction to Java 2 Enterprise Edition 15 2 4 4 Stand alone Java applications By far the simplest form of a Java program is the stand alone Java application These are complete programs that manage all the functionality within one unit of code although they can make use of external classes They also tend to be console applications which means that they receive input and send output in text format from the command line In z OS terms this means that the program is executed directly in the Unix System Services environment As this type of program is similar to other mainframe batch programs a stand alone Java application can be run in batch on a z OS system using the BPXBATCH utility see Running the Hello application from MVS batch on page 147 for an example A stand alone Java program application has a main method which is the entry point for the program It can accept command line arguments that are placed in the String parameter args Once the application has been coded it is compiled into byte code by running the javac command The output of this process is a class file The program is executed by running the java command 2 4 2 Java applets These are Java programs that can be included in an HTML page using the applet tag They are downloaded to the client machine by the browser that is displaying the host HTML page and executed under the browser s JVM Applets are normally small programs that extend t
61. DB2 Universal Driver for SQLJ and JDBC Because our WebSpere runs on z OS we want to use a JDBC Type 2 driver to connect to the DB2 system DB7Y that runs on the same z OS image To set up a data source in WAS we use the WAS for z OS V5 Administrative Console This browser appliation replaces the SMEUI that was used in previous versions of WAS on z OS DB2 for z OS and OS 390 Ready for Java 5 6 1 Log onto the WAS Administrative Console The WAS admin console uses a Web interface Open a browser session and type in the URL to connect to the admin console application In our case the URL is http wtsc63 itso ibm com 9080 admin A screen similar to Figure 5 23 should appear E IBM WebSphere Application Server Administrative Login Microsoft Internet Explorer File Edit View Favorites Tools Help Bak gt Q A Qusearh Favorites media 4 F 5 ml s Address amp http wtsc63 itso ibm com 9080 admin Edit with Microsoft Word Links Search the Web with Google amp IBM BluePages amp IBM Standard Software Installer ustomize Lini BM Internal Help amp Free Hotmail WebSphere Application Server Administrative Console Version 5 User ID bart The User ID does not require a password and does not need to be a User ID of a user in the local user registry It is only used to track user specific changes to configuration data Security is NOT enabled Internet Figure 5 23 WAS Administrativ
62. DB2 for z OS and OS 390 Ready for Java Note During our testing we found that WebSphere Studio would regularly mess up the server configuration definition or would not pick up changes made to the configuration This was without any doubt due to the fact that we were working with an early copy of the product and those problems are not very likely to occur in your case However when they do it is best start from scratch that is delete the complete server Websphere V5 0 Test Environment stop start WSAD and define the server as well as the data source from scratch Now we are ready to run the Servlet once more Republish the server configuration and restart the server If the browser window showing the error message is still open you can simply click its Refresh button otherwise select the Servlet and click Run on Server Now the SQLJ runtime will create connection context from the data source registered under jdbc empdb and the Servlet runs successfully The output in the browser window should look similar to Figure 15 11 J EmployeeListServlet java E EE X gi WebSphere v4 0 Test Environment http localhost 5G24 6435 EmployeeListServlet io gt 92833 B Employee listing 000010 HAAS CHRISTINE I 000020 THOMPSON MICHAEL L 000030 KWAN SALLY A 000050 GEYER JOHN B 000060 STERN IRVING F 000070 PULASKI EVAD 000090 HENDERSON EILEEN W 000100 SPENSER THEODORE Q 000110 LITCCHEST VINCENZO G B Done Figu
63. DSN8710 EMP Tasks DB Output 1 item selected Figure 11 83 Sample contents with Dent inserted 11 2 6 Implementing the findByPrimaryKey method 208 Now that we are able to create a new employee we need a method to find an existing employee given his or her employee number Since we do not have an object to work with after all we want to find one in the first place findByPrimaryKey cannot be an instance method it has to be a class method in Java terms it must be declared static We implement this method using the SELECT INTO SQL statement You use SELECT INTO whenever you expect exactly one result row as the result from a query singleton select When DB2 detects that no row or more than one row satisfies the WHERE condition an SQLException will be raised The latter case cannot occur in our example since EMPNO being the primary key column does not allow duplicate values We have a little problem with the SEX column since we decided to map it to a boolean variable Therefore we have to convert the string value in the column to a boolean In Example 11 8 below we do this by SELECTing the value into an intermediate local String variable then inspecting that variable s contents and setting the attribute s value accordingly Example 11 8 Employee findByPrimaryKey first version public static Employee findByPrimaryKey Ctx ctx String empNo throws SQLException Employee emp new Employe
64. DTD HTML 4 01 Transitional EN out printIn HTML out printIn HEAD out printIn lt TITLE gt Employee listing lt TITLE gt out printIn HEAD out printIn BODY out printIn lt H1 gt Employee listing lt H1 gt out printIn BODY out printIn HTML out close 15 2 2 Testing the Servlet 252 We are now ready to test the Servlet skeleton Fortunately we do not have to install and configure a full blown Web application server in order to run the Servlet WSAD comes with a component called the WebSphere test environment which is basically a stripped down version of WebSphere Application Server that has been integrated into the WSAD environment WSAD takes you through all the steps to create a WebSphere test environment In the J2EE Navigator view right click the Servlet class and select Run on Server Figure 15 3 on page 253 Since no test environment has been created yet WSAD will prompt you to create one On the following dialog select the Create new server radio button if not selected by default and select WebSphere version 5 0 gt Test Environment then click Next DB2 for z OS and OS 390 Ready for Java qp web EmployeeListServlet java IBM WebSphere Studio Application Developer f ji m ni xj File Edit Source Refactor Navigate Search Project Run Window Help Be 5 je a8 B gs ds amp lm 9 023 2
65. INSERT INTO DSN8710 EMP PHOTO RESUME EMPNO BMP PHOTO VALUES empNo lobStream h finally picture close Notes on Example 11 23 on page 217 1 Since we cannot know the picture s size other than by reading the input stream untill the end it must be passed in as a parameter by the caller 2 The preferred host variable type for BLOBs is BinaryStream The SQLJ runtime wants to know the BLOB s size beforehand so we have to pass it in with the constructor for BinaryStream Alternatively we could have read the entire picture into a byte array ourselves and pass the byte array as host variable As a convenience we implement another createPicture method Example 11 24 which takes a file name argument then determines the length of the file and calls the original createPicture method We add that one to the Employee class as well Example 11 24 Overloaded createPicture method Creates the employee s picture param pictureFilename Name of a file containing the picture in BMP format exception SQLException A database error occurred exception IOException An error occurred reading the file FF F OR HF HF public void createPicture Ctx ctx String pictureFilename throws SQLException IOException File pictureFile new File pictureFilename createPicture ctx new FileInputStream pictureFile int pictureFile length For testing purposes we added the createPic
66. IP Telnet Telnet provides a virtual terminal facility that allows users of one computer to act as if they were using a terminal connected to another computer The Telnet client program communicates with the Telnet daemon on the target system to provide the connection and session 324 DB2 for z OS and OS 390 Ready for Java Temporary table A table created by the SQL CREATE GLOBAL TEMPORARY TABLE statement that is used to hold temporary data Contrast with result table Thin client Thin client usually refers to a system that runs on a resource constrained machine or that runs a small operating system Thin clients don t require local system administration and they execute Java applications delivered over the network Third tier The third tier or back end is the hardware and software that provides database and transactional services These back end services are accessed through connectors between the middle tier Web server and the third tier server Though this conceptual model depicts the second and third tier as two separate machines the NCF model supports a logical three tier implementation in which the software on the middle and third tier are on the same box Thread A separate flow of control within a program Timestamp A seven part value that consists of a date and time expressed in years months days hours minutes seconds and microseconds Trace A DB2 facility that provides the ability to monitor and collect DB2 moni
67. JDBC drivers available for DB2 on the z OS and OS 390 platform and some great new ones just around the corner each having different characteristics and levels of cross platform compatibility Table 3 1 gives an overview Table 3 1 JDBC drivers and driver types for use with DB2 z OS and OS 390 T uu en db2sgljclasses COM ibm db208s390 sqlj jdbc DB2SQLJ i Can only be used on z OS or zip Driver OS 390 platform Contains all classes necessary for preparing and running JDBC and SQLJ programs 250 0 az 2 26 DB2 for z OS and OS 390 Ready for Java Jar file s Driver name Usage notes ovi db2sqljruntime COM ibm db20s390 sglj jdbc DB2SQLJ zip Driver Can only be used on z OS or OS 390 platform Contains only execution classes for JDBC and SQLJ programs Cannot be used to execute the SQLJ preprocessor translator N o db2j2classes zip COM ibm db20s390 sqlj jdbc DB2SQLJ Driver db2java zip COM ibm db2 jdbc app DB2Driver db2java zip COM ibm db2 jdbc net DB2Driver db2jcc jar com ibm db2 jcc DB2Driver Can only be used on z OS or OS 390 platform JDBC extension file jdbc2 O stdext jar does not have to be installed as JDBC 2 0 extensions are included in the class file Once JCC becomes available on z OS This is the old DB2 for LUW Type 2 driver also known as the app driver This driver is only mentioned here as it can be used with DB2 Connect running on the client machine to access
68. Java compiler will complain about the use of a possibly unassigned variable although we do know that they have been assigned if the FETCH statement was successful Tries to fetch the current row into the host variables declared in note 1 that as explained above we do not need to specify a connection context in fact the SQLJ translator issues a warning if you do 3 Return nul if no more rows were found 4 Otherwise create and populate an Employee object and return it Now it is time to test our findA11 method First we code a testFindAl1 method in our testEmployee program Figure 11 18 Example 11 18 testFindAll method public static void testFindAll throws SQLException EmployeeIterator iter null try iter Employee findAl ctx for 33 Chapter 11 SQLJ revisited 213 Employee emp Employee fetch iter if emp null break System out print n emp finally if iter null iter close Do not forget to add an import statement for the Employeelterator in the beginning of your EmployeeTest java program like import com ibm itso sg246435 sqlj Employee EmployeeIterator Then we invoke it from within the main program Figure 11 19 Example 11 19 Invoking the testFindAll method public static void main String args try initialize testFindATI catch Exception e 11 2 10 Working with LOB data The getPicture and setPicture methods 214 To
69. LASTNAME gt HIREDATE CASE SEX WHEN M THEN 1 ELSE O END gt SALARY INTO emp firstName emp middleInitial emp lastName emp hireDate emp male emp salary FROM DSN8710 EMP WHERE EMPNO empNo h Chapter 11 SQLJ revisited 209 return emp To test the findByPrimaryKey method we insert a call to the Employee findByPrimaryKey method and we comment out the call to testInsert for the time being otherwise we would not be able to run the test program due to the unique key constraint violation we saw in Creating a test driver on page 204 Example 11 10 shows the updated main method Example 11 10 Modified EmployeeTest main public static void main String args try initialize testInsert Employee emp Employee findByPrimaryKey ctx 000042 System out printIn Successfully retrieved emp catch SQLException e System err println e SQLCODE e getErrorCode catch Exception e System err printIn e Before running the test program again do not forget to re translate Employee sqlj The test program should print Successfully retrieved Dent Arthur Before we can un comment the call to test Insert again we have to write a method to delete employee records which we do in the following section 11 2 7 Implementing the delete method 210 The delete method deletes an Employee by running an SQL DELETE statement against the d
70. Pages 281 4 New JSP File x New JSP File AS Specify a name and location For the new JSP file Folder I5624 6435 Web Web Content Browse File Name EmployeeList Markup Language xe Options Create as JSP Fragment Use XML Style Synta Model None Generate a new blank JSP page IV Configure advanced options lt Back Next gt Cancel Figure 16 2 New JSP File wizard Enter EmployeeList in the File Name text entry field We choose XHTML as the markup language HTML or Compact HTML would have been possible too Make sure that Configure advanced options is checked and click Next In the next dialog window of the wizard we tell WSAD that we want to include a custom tag library in the JSP Click the Add button which will open the dialog in Figure 16 3 on page 283 282 DB2 for z OS and OS 390 Ready for Java 4 Tag Library x Select a Tag Library Select one or more entries by checking them from the list below le Tag Libraries Import 2ispsql Web Content WEB INF lib jspsal jar Show recommended URI choices one choice for each resource Show all valid URI choices Prefix sql Description Tag library for database actions Source WEB XML Custom Tags Description batch Group the enclosed tags in a batch column Specifies a column value dataSourceSpec Defines a connection using a DataSource deleteRow Specifies a row to be de
71. SQLJ is a language extension When an SQLJ program has been coded it is first run through the SQLJ translator fittingly called sqlj This takes the SQLJ statements and replaces them with valid Java methods The SQL statements are placed in a file name ClassName SJProfileO ser while the Java code is placed in a standard Java file called ClassName java The program file can now be compiled and run Chapter 3 Accessing DB2 from Java 41 the translator compiles the program automatically unless you use a command line switch to suppress it However besides the mandatory translation step you can and should execute an optional customization step Not all DBMS systems supporting SQLJ also support customization but DB2 does During customization against a DB2 system the SQL statements in the ser files are bound into packages and they will execute as true static SQL statements at runtime In case the DBMS does not support customization or customization has not been done the translated SQLJ statements are executed through JDBC as dynamic SQL Important The real strengths of SQLJ come only into play through the customization process Do not expect better performance of SQLJ programs over equivalent JDBC programs if no customization has taken place In fact uncustomized SQLJ will rather run slightly slower because of some additional overhead in the SQLJ runtime For a more extensive discussion of the methods for SQLJ program preparation and why
72. Security grant codeBase file connectorComponent D Environment B grant codeBase file vebComponent X Bl System Administration Bl Troubleshooting grant codeBase file ejbComponent X Application Name SG24 B5435 static war Reload Interval Directory to Install Application Pre compile JSP No Application Name G24 6435 static_war Previous Finish Cancel Figure 15 19 Install a new application 6 Now it is time to save our changes You do so by clicking Save to Master Configuration as shown in Figure 15 20 WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help EJ Back Qsearch Favorites Cmeda C4 B 5 Si S Address ja http wtsc63 itso ibm com 9080 admin secure logon do Go x Links WebSphere Application Server Administrative Console Version 5 Home Save Preferences Logout Help User ID the configuration until the process is complete Fe cel111 Check the SystemOut log on the Deployment Manager or Server where the application is deployed for specific information Servers about the EJB Deploy process as it occurs E Applications ADMASDABE Installation of SG24 8435 static_war started Enterprise Applications Install New Application ADMASO05 Application SG24 6435 static_war configured in WebSphere repository Resources Security ADMASDDTI Appli
73. Summary of changes This section describes the technical changes made in this edition of the book and in previous editions This edition may also include minor corrections and editorial changes that are not identified Summary of Changes for SG24 6435 00 for DB2 for z OS and OS 390 Ready for Java as created or updated on December 22 2004 December 2004 This revision reflects the addition deletion or modification of new and changed information described below New information gt Information about the no charge feature IBM z OS Application Connectivity to DB2 for OS 390 and z OS was added See 3 2 6 IBM z OS Application Connectivity to DB2 for z OS and OS 390 on page 34 Changed information Added a shaded box on page 28 to clarify that the Universal Driver code that ships with DB2 for Linux Unix and Windows should not be used on the z OS platform You should use the Universal Driver that ships with DB2 for z OS V7 or V8 on the z OS platform This is true for both type 2 and type 4 connectivity You can of course use the Universal Driver that comes with DB2 for Linux Unix and Windows fo access data that resides in DB2 for z OS V6 V7 or V8 system either directly via type 4 connectivity or via DB2 connect Copyright IBM Corp 2003 All rights reserved XXV xxvi DB2 for z OS and OS 390 Ready for Java Part 1 Introduction In this part we give a brief overview of the Java history the different components
74. V7 system DB7Y Database vendor type Select DB2 Universal Database for OS 390 V7 from the drop down list JDBC driver We choose Other DB2 390 Driver It un greys the driver class and location fields DB2 for z OS and OS 390 Ready for Java gt For JDBC driver class we coded the name com ibm db2 jcc DB2Driver as the name for the DB2 UDB V8 Universal Driver Type 4 gt We use Browse to find the class location It is in the directory where we installed DB2 UDB V8 1 we used C SQLLIB Java but the default is C Program Files IBM SQLLIB Java Locate the correct Universal Driver db2jcc jar in the Java subdirectory Figure 5 16 as well as the appropriate licence jar file db2jcc license cisuz jar because we are going to a DB2 for z OS and OS 390 system The license jar is provided with the DB2 Connect product so in order to be able to use the Type 4 driver you must have a DB2 Connect license Important As of DB2 UDB V8 1 2 the DB2 Universal Driver for SQLJ and JDBC requires a license jar file It has to be in the CLASSPATH along with the db2jcc jar file Here are the required license JAR files gt db2jcc license c jar Permits JDBC connectivity to DB2J that is Cloudscape servers only Cloudscape is bundled with WAS along with this license gt db2jcc license cu jar Permits JDBC SQLJ connectivity to all DB2 LUW Linux Unix Windows servers and Cloudscape This is the standard license provided with UDB on U
75. WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Ea esk gt A Bsearch Favorites media 4 E4 5 ig Address amp http vwtsc63 itso ibm com 9080 admin secure logon do h Go Links search the Web with Google amp IBM BluePages 4 IBM Standard Software Installer Customize Links rem Internal Help Free Hotmail H4 Version 5 User ID bart cel111 Servers Applications E Resources JDBC Providers Generic JMS Providers WebSphere JMS Provider WebSphere MQ JMS Provider Mail Providers Resource Environment Providd URL Providers Resource Adapters Security E Environment Update Web Server Plugin Virtual Hosts Manage WebSphere Variable3 Shared Libraries Naming System Administration Troubleshooting EE WebSphere Application Server Administrative Console B Ej Bo DE e Home Save Preferences Logout Help JDBC Providers gt DB2 JCC T2 20S JDBC Provider gt Data Sources gt DB2JCCT2z0SDataSource gt Custom Properties Custom properties that may be required for Resource Providers and Resource Factories For example most database vendors require additional custom properties for data sources that will access the database Li Filter Total 7 Preferences New Delete Hame Value v Description Required r currentSQLID This value willbe used fal
76. WebSphere Developer InfoCenter Domain Figure 15 22 Administrative Console Main menu 276 DB2 for z OS and OS 390 Ready for Java Starting an application To activate an application we select the Enterprise Applications option on the left pane This takes us to the Enterprise Applications window Figure 15 23 You can see that our newly installed application is stopped indicated by the red X To start the application select the application by checking the box next to SG24 6435 static war and clicking the Start button E WebSphere Administrative Console Microsoft Internet Explorer DE x File Edit View Favorites Tools Help Ea Back gt amp 2 A Qsearch Favorites media G 5 c4 s Address http Jwtsce3 itso ibm com 9080 admin secure logon do Go Links 25 i 60x a WebSphere Application Server Administrative Console ae rs 3 Version 5 i _ a4 Home Save Preferences Logout Help BE User ID Enterprise Applications cel111 A list of installed applications A single application can be deployed onto multiple servers Li Servers E Applications Enterprise Applications Total 5 Install New Application Filter Resources Preferences security Stop Install Uninstall Update Export Export DDL Environment x A System Administration Hame Y Status v Troubleshooting SG24 6435 Web war gt SG24 6435 Web wa
77. as an error In Java throwing an exception means passing that object to an interested party a signal indicates what kind of condition has taken place Catching an exception means receiving the sent object Handling this exception usually means taking care of the problem after receiving the object although it might mean doing nothing which would be bad programming practice Extends A subclass or interface extends a class or interface if it add fields or methods or overrides its methods See also derived type External function A function for which the body is written in a programming language that takes scalar argument values and produces a scalar result for each invocation Contrast with sourced function and built in function Extranet In some cases intranets have connections to other independent intranets An example would be one company connecting its intranet to the intranet of one of its suppliers Such a connection of intranets is called an extranet Depending on the implementation they may or may not be fully or partially visible to the outside Field A data object in a class for example a variable Glossary 319 File Transfer Protocol FTP In the Internet suite of protocols an application layer protocol that uses TCP and Telnet services to transfer bulk data files between machines or hosts First tier The client the hardware and software with which the end user interacts Foreign key A key that is specified in t
78. at runtime because the package that is needed to execute the SQL does not exist The DBRM on the other hand could be deleted after the bind step although this is usually not done in practice 9 2 Overview of the SQLJ program preparation process The SQLJ program preparation process is similar to the program preparation process for embedded static SQL programs in other languages like COBOL see Program preparation in other languages on page 150 The process is illustrated in Figure 9 2 on page 152 where again the straight lines indicate compile time and the dashed lines indicate runtime The following steps are performed 1 The SQLJ translator also called SQLJ preprocessor translates the original SQLJ source file into a Java file SQL statements in the source file are replaced by calls into the SQLJ runtime library The translator also produces one or more serialized profiles ser files To be precise it creates a separate serialized profile for each context class used in the program see Using more than one context class on page 194 for details The Java compiler translates the preprocessed source file into class files At this point the program can be run successfully but it will use dynamic SQL at runtime Optionally the serialized profiles can and should be customized by the profile customizer The profile customizer updates the ser files and by default also creates a package in the DB2 database cat
79. being returned from an SQL statement the program jumps to the standard error routine to report the error and exits the program Java has no GOTO statement It removes the need for this statement with tight mechanisms for error trapping and handling using exceptions and the try catch finally construct This provides a sophisticated error trapping and handling processing from within the programming language itself The general syntax of the try catch finally construct looks like this Connection conn null try Class forName conn DriverManager getConnection url username password Call a method which may throw an exception catch ClassNotFoundException e catch SQLException e finally 2 3 6 Java Virtual Machine 14 Java has a philosophy of compile once run anywhere As the language had the need to be able to run on all types of processors and operating systems a major requirement is that code that is written and compiled on one platform can be moved to and executed on any other machine To make this happen Java has the concept of first compiling the code in into bytecode that is portable and then running that bytecode on a platform specific Java Virtual Machine JVM as depicted in Figure 2 2 on page 15 Although this was not the first time a software microprocessor was thought of to allow portability the use of one with major language initiatives made the concept more accessible and acceptable Th
80. catch SQLException sqlException if sqlException getSQLState equals 02000 row not found return null else throw sqlException return picture getBinaryStream Creates the employee s picture param picture An InputStream supplying the picture in BMP format On return from this method the stream is closed exception SQLException A database error occurred exception IOException An error occurred reading the input stream F F OR OR OR HF HF X public void createPicture Ctx ctx InputStream picture int length throws SQLException IOException try BinaryStream lobStream new BinaryStream picture length sql ctx INSERT INTO DSN8710 EMP PHOTO RESUME EMPNO BMP PHOTO VALUES empNo lobStream h catch SQLException e System err println e SQLCODE e getErrorCode catch Exception e System err printIn e finally picture close Creates the employee s picture Gparam pictureFilename Name of a file containing the picture in BMP format exception SQLException A database error occurred exception IOException An error occurred reading the file F F HF HF FH public void createPicture Ctx ctx String pictureFilename throws SQLException IOException 302 DB2 for z OS and OS 390 Ready for Java File pictureFile new File pictureFilename createPicture ctx new FileInputStream pictureFile int p
81. components The EJB server itself manages the services required by the bean such as security transaction control and object pooling This allows developers to concentrate on business logic rather than on the technical intricacies of database access authentication and authorization and so on EJBs are discussed in more detail in Enterprise Java Beans on page 19 Servlets and JavaServer Pages Servlets are a form of Java programs that plug into Web server environments to dynamically generate Web content This model defines how Java Servlets are written and how they are to be managed on the server J2EE extends the Servlet model through the introduction of JavaServer Pages which make the creation of Java Servlets easier by giving the code an HTML tag language look and feel A more complete explanation of Servlets can be found in Java Servlets on page 17 while JavaServer Pages are discussed in JavaServer Pages on page 18 Hands on examples on Chapter 2 An introduction to Java 2 Enterprise Edition 11 how to implement and deploy Servlets and JavaServer Pages are presented in Chapter 15 Using Servlets to access DB2 on page 249 and Chapter 16 JavaServer Pages on page 279 respectively Other J2EE technologies J2EE provides other enterprise technologies that are not directly related to the development of DB2 Java applications These are gt Remote Method Invocation RMI is a standard for distributed access to Java objec
82. countries SET SET Secure Electronic Transaction and the SET Logo are trademarks owned by SET Secure Electronic Transaction LLC Other company product and service names may be trademarks or service marks of others XX DB2 for z OS and OS 390 Ready for Java Preface Read the following statements The earth is flat The earth is the center of the universe Men shall never fly Java will never work properly on the mainframe vvvy All four statements had a lot of advocates for a long time but all of them turned out to be wrong Completely wrong In this IBM Redbook we show how Java and DB2 for z OS and OS 390 can work together and form a strong combination that can run your mission critical enterprise applications This publication focusses on the new IBM Universal Driver for SQLJ and JDBC IBM s new JDBC driver implementation supporting both Type 2 and Type 4 driver connectivity to the members of the DB2 family including DB2 for z OS and DB2 for Linux Unix and Windows This publication provides guidance on the different ways to set up your environment to hook a Java program up to a DB2 for z OS subsystem through JDBC or SQLJ using the Type 2 driver and the Type 4 driver We provide an SQLJ tutorial and demonstrate how to develop and deploy SQLJ programs using the new SQLJ support functions that became available with WebSphere Studio Application Developer We demonstrate the use of Java and DB2 using n
83. creates the following files in the com ibm itso sg246435 sglj subdirectory Hello class Hello SJProfileKeys class Hello EmployeelIterator class Note that the Java compiler also generates a class file for the iterator that we declared in the program Hello Employeelterator class and a class file for a utility class that is used internally by the SQLJ runtime Hello SJProfileKeys class If we were to execute the program at this point it would run using dynamic SQL As our goal is to run with static SQL we are not going to However in a development environment this is very useful to do validation of your SQL statements to make sure they return the correct data DB2 for z OS and OS 390 Ready for Java Customization db2sqljcustomize This is the magic step that makes an sqlj program run statically against a DB2 system We use the db2sqljcustomize utility for this purpose The command is shown in Example 9 6 Example 9 6 Customizing a serialized profile C TestSQLJ SG24 6435 gt db2sqljcustomize url jdbc db2 wtsc63 itso ibm com 33756 DB7Y user bart password blngo rootpkgname HELLO collection SG246435 com ibm itso sg246435 sq1j Hello SJProfileO ser ibm db2 jcc sq13 ibm db2 jcc sq1j Begin Customization ibm db2 jcc sq j Loading profile com ibm itso sg246435 sq1j Hello SJProfil a D marr e ibm db2 jcc sqlj Customization complete for profile com ibm itso sg246435 s q1j Hello SJProfile0 ser ibm
84. db7y DSNTJSPP SPB _ DB7YREXX For Rexx SPs DSNTPSMP DSNTBIND NK DB7YUTIL DB7Y stored proc Utility 2 WLMENV DB7Y all non spcial SPs WLMENV1 DB7Y stored proc environment ek ee hee e eee ee he ee e ee he ee eee ke Bottom of data ek ee hee e eee ee e ee e e e eee e e e af Figure 5 8 WLM Select an application environment Chapter 5 Setup 63 Since copying is the sincerest form of productivity choose key 2 beside an existing DB2 WLM Environment and press Enter We see Figure 5 4 Application Environment Notes Options Help Copy an Application Environment Command gt Application Environment Requi red Description DB7Y all non spcial SPs Subsystem Type DB2 Procedure Name DB7YWLM Start Parameters DB2SSN DB7Y NUMTCB 8 APPLENV WLMENV Limit on starting server address spaces for a subsystem instance 1 1 No limit 2 Single address space per system 3 Single address space per sysplex J Figure 5 4 WLM Copy an application environment WLMENV uses the default environment we chose on the DB2 Installation Panels at DB2 installation time This application environment name is used for all stored procedures that ship with DB2 which have no specific requirements That is there is no requirement to single thread them TCB 1 as is required for DSNUTILS the utility stored procedure and DSNTPSMP the SQL Stored Procedure Builder We could have used WMLENV for our stored proced
85. disposition of Service Definition Description The service definition has been changed but not saved Select one of following opt Select one of the following options or PF12 to go back to the WLM Administrative Application 2 1 Save definition to data set and continue 2 Install definition on WLM couple data set and continue 3 Discard changes and continue 10 Scheduling Environments Figure 5 7 WLM Install the WLM definition Specify option 2 and press Enter again This takes you back to the WLM primary menu From the Utilities drop down menu we select option 3 to activate the policy as shown in Figure 5 8 File Utilities Notes Options Help Funct 3 1 Install definition Appl LEVELO13 Comma 2 Extract definition 3 Activate service policy Defin 4 Allocate couple data set 5 Allocate couple data set using CDS values Defin 6 Validate definition Descr Select one of the following options 1 Policies 2 Workloads 3 Resource Groups 4 Service Classes 5 Classification Groups 6 Classification Rules 7 Report Classes 8 Service Coefficients Options 9 Application Environments 10 Scheduling Environments Figure 5 8 WLM utilities Choose to activate the policy Figure 5 9 on page 67 lists the services policies currently installed in the WLM Couple Data Sets 66 DB2 for z OS and OS 390 Ready for Java File Utilities Notes Op
86. each statement However we do not recommend using the default context except maybe in stand alone programs that do not share a JVM with other applications The default context is a static variable of class sqlj runtime ref DefaultContext the setDefaultContext method is used to modify it Now suppose your application is running in a multithreaded environment such as WebSphere Application server There are two obvious problems with using the default context gt First you potentially share a single database connection with other threads which can create a throughput bottleneck Second there is no guarantee that between the time your application sets the default context and then executes using an implicit context another thread has not changed the value for the default context After all the default context is a global variable you are sharing with all threads running in the same JVM Strictly speaking it is not necessary to declare a separate context class using the sql context clause You can use the DefaultContext class supplied with the SQLJ runtime library as long as you do not call setDefaultContext and use an implicit context For maximum portability however the SQLJ standard recommends that you always declare and use explicit context since the specification of the default context is implementation defined There are situations however where it is mandatory to declare your own context classes This is explaine
87. environment is set up correctly more specifically the WLM application environments The following screens are shown to illustrate what is necessary to set up a Workload Manager Application Environment In most installations this work is normally performed for you by the z OS systems staff as access to the WLM policy and its ISPF panels is usually restricted The example below uses an existing policy in which at least the default DB2 WLM Environment has been already created We modify the existing policy by copying a valid environment to a new one in this case WLM100 changing the values as desired saving it installing the policy and activating the policy Chapter 5 Setup 61 Note Occasionally you may need to change the WLM environment used by some of the stored procedures following installation of some maintenance You can do this via the ALTER PROCEDURE SQL statement through SPUFI or via the DB2 Control Center if that is installed From the ISPF panel we enter WL to bring up the WLM panels yours is likely different Press Enter on the WLM startup screen and the panel shown in Figure 5 1 appears d File Help Command gt Choose Service Definition Select one of the following options 2 1 Read saved definition 2 Extract definition from WLM couple data set 3 Create new definition ENTER to continue A Figure 5 1 WLM Choose a service definition Use option 2 and pre
88. file can be found in the C WINNT system32 drivers etc directory db2 update dbm cfg using SVCENAME lt TCP IP service name gt You must do a db2stop and db2start for this setting to take effect Note also that the port number used for applets and SQLJ programs needs to be the same as the TCP IP SVCENAME number used in the database manager configuration file As we are focussing on accessing DB2 for z OS and OS 390 data this does not really apply to our samples However in many installations people develop on a workstation and test against local DB2 data on that workstation In that case the listener port has to be set up properly in order for the Universal Driver to work 5 5 WSAD setup As mentioned before we use WebSphere Studio Application Developer WSAD for most of our samples in this publication Since WSAD V5 1 was not yet available at the time we wrote this publication we downloaded a pre release version of WSAD V5 1 from an IBM internal site Since your version will likely use a CD your installation process will be slightly different Click the Setup exe in the folder into which you want the unzipped WSAD files We only need to install WebSphere Studio Application Developer not Rational Clear Case or the IBM Agent Controller In the Custom Setup section we did not modify any of the options For the version that we use we install only the Required Features which include the Integrated Development Environment IDE
89. for Java 15 Using Servlets to access DB2 In this chapter we demonstrate how to access DB2 from a Web server environment Since we do not want to reinvent the wheel we use the code developed in Chapter 11 SQLJ revisited on page 199 using it from the Servlets that generate the dynamic HTML content The simple Web application we develop in this chapter displays a list of employees Each employee serial number is a hyperlink that takes the user to a detail page displaying the employee s personal data and picture To develop and test the code we use the WebSphere test environment of WSAD Copyright IBM Corp 2003 All rights reserved 249 15 1 Creating the project 250 In WSAD a Web application must live in a Web project To create the new project select File gt New gt Project Select Web then Dynamic Web Project and click Next In the Project name field type SG24 6435 Web Make sure that the Configure advanced options box is selected and click Next On the next panel enter 5624 6435 in the Context root field overriding the default value that is the project name Make sure that J2EE level 1 3 is selected then press Finish db New Web Project x J2EE Settings Page Set the Enterprise Application project settings context root and J2EE level am EAR project DefaultEAR si New Context root 5624 6435 J2EE level 1 3 hA J2EE Level 1 3 includes a Servlet Specification level of 2 3 and a ISP
90. found return null else throw sqlException return picture getBinaryStream Again we test the method by creating and calling a test method in our EmployeeTest class The test method shown in Example 11 22 expects an employee number and a directory where to save the picture file Example 11 22 EmployeeTest testGetPicture import java io File import java io InputStream import java io OutputStream import java io FileOutputStream public static void testGetPicture String empno File dir throws SQLException IOException Employee emp Employee findByPrimaryKey ctx empno InputStream in null OutputStream out null try in emp getPicture ctx if in null System out printIn No picture available for employee empno DB2 for z OS and OS 390 Ready for Java else out new FileOutputStream new File dir Emp empno bmp int nread byte buf new byte 1024 while nread in read buf gt 0 out write buf 0 nread System out println Picture retrieved for employee empno in dir with name Emp empno bmp finally if in null in close if out null out close The DB2 sample database contains several employees for whom a picture is available including employee 000130 We modify the main method of EmployeeTest by adding testGetPicture 000130 new File C temp After running EmployeeTest once more a file named Emp0
91. getEmpNo a printCol out emp getLastName printCol out emp getFirstName emp getMiddleInitial out printIn tr Now run the Servlet again The employee number will now appear as a hyperlink When hovering over the hyperlink you will see in your browser s status bar that the link target is of the form http localhost 9080 SG24 6435 Emp1oyeeDetai 1Servlet empno xxxxxx Where xxxxxx is the employee number If you click the hyperlink the EmployeeDetailServlet is invoked and passed an employee number Of course since the EmployeeDetailServlet does not use the parameter right now following the link always takes you to the hardcoded employee number 000130 15 6 2 Modifying the EmployeeDetail and EmployeePic Servlets With the modification in the previous section we are now able to pass in the employee number to the EmployeeDetail Servlet as a parameter To retrieve the parameter value a Servlet calls the getParameter method of its request object the first parameter to the doGet method The argument to getParameter is a string the parameter name Thus the required modification to both EmployeeDetailServlet and EmployeePicServlet is very simple Just replace in their respective doGet methods the lines String empno 000130 With String empno req getParameter empno Note In a real application you probably want to perform some error checking to ensure that the parameter is not
92. implements ODBC function calls and interacts with a data Source 322 DB2 for z OS and OS 390 Ready for Java Open Database Connectivity ODBC A Microsoft database application programming interface API for C that allows access to database management systems by using callable SQL ODBC does not require the use of an SQL preprocessor In addition ODBC provides an architecture that lets users add modules called database drivers that link the application to their choice of database management systems at run time This means that applications no longer need to be directly linked to the modules of all the database management systems that are supported Package 1 In Java a program element that contains classes and interfaces 2 In DB2 a control structure produced during program preparation that is used to execute SQL statements Parameter marker A question mark that appears in a statement string of a dynamic SQL statement The question mark can appear where a host variable might appear if the statement string was a static SQL statement Persistence In object models a condition that allows instances of classes to be stored externally for example in a relational database Plan See application plan Plan name The name of an application plan Precompilation A processing of application programs containing SQL statements that takes place before compilation SQL statements are replaced with statements that are recognized by the ho
93. job after customizing it that lives in the hig SDSNSAMP library This APAR introduces 13 stored procedures Twelve of these procedures provide the ability to generate a result set that corresponds to the schema metadata APIs An additional procedure introduced by this APAR formats SQLCODE message text given input fields from a DB2 generated SQLCA SYSIBM SQLCOLPRIVILEGES SYSIBM SQLCOLUMNS DB2 for z OS and OS 390 Ready for Java SYSIBM SQLFOREIGNKEYS SYSIBM SQLPRIMARYKEYS SYSIBM SQLPROCEDURECOLS SYSIBM SQLPROCEDURES SYSIBM SQLSPECIALCOLUMNS SYSIBM SQLSTATISTICS SYSIBM SQLTABLEPRIVILEGES SYSIBM SQLTABLES SYSIBM SQLGETTYPEINFO SYSIBM SQLUDTS SYSIBM SQLCAMESSAGE gt PQ72453 This APAR resolves some errors in the initial implementation of the metadata catalog queries delivered through APAR PQ62695 Note You also need to have this support when using the Universal Driver running on a Windows or AIX platform and you are connecting to the mainframe DSNTIJUZ The DB2 UDB for OS 390 and z OS Installation Guide Version 7 GC26 9936 clearly states that you must rebind affected packages after the value has been set to YES Important Make sure the DSNZPARM parameter DESCSTAT is set to YES Installation Panel DSNTIPF Describe for Static before you run the next job Otherwise you have to rerun them in order to rebind the affected packages after you make this change Do
94. levels and their corresponding DB2 isolation levels 176 10 2 Best mappings of Java to DB2 data types 20 2 eee ee 181 11 1 Employee sample table 00 0 eae 200 12 1 Property keys for the DB2 Universal Driver for SQLJ and JDBC 223 14 1 Trace level options for the DB2 Universal Driver for SQLJ and JDBC 244 161 SQLSTATE categories meee e aara eren 291 Copyright IBM Corp 2003 All rights reserved xiii xiv DB2 for z OS and OS 390 Ready for Java Examples 3 1 Loading the DB2 JDBC driver llle 37 3 2 Connecting using the Type 4 driver llli 39 3 3 JDBC vs SQLJ Multi row query isses mee 43 3 4 JDBC vs SQLJ Single row query lisse res 43 3 5 JDBC vs SQLJ INSERT statement 0 0 0 00 cee 44 5 1 Invoking the DB2Binder utility lll 60 5 2 db2jcct2 properties file 2l 61 pS Bind plazo y eso 8 ae ee a Sats oy RR AEG Pee batter 61 5 4 Started task JCL for DB7YWLM in SYS1 PROCLIB 2 0005 67 5 5 Setting the CLASSPATH from the command line in Unix System Services 70 5 6 USS s User profile ee pee heh pr eres um emis ade Lv to mM P d dina 71 5 7 DSNL004I DB2 390 DDF information used for Control Center and WSAD 76 6 1 Source code for Hello application 000 cece eee 103 7 1 Printing database metadata tee 124 7 2 Find tables in a schema querying the DB2 catalog
95. lille 126 7 3 Find tables in a schema using DatabaseMetaData lisse 126 7 4 Using updatable cursors llliseeeee nen 128 7 5 Spufi class declaration and constructors 0000 cece eee eee eee 130 7 6 Spufi addStream addFile and addSql 0 0c cee eee 132 7 7 Spufi execute rada a eaaa a na a e a a aa Ea aa ia A 132 7 8 Spufi execute 0 0 tee eee 133 7 9 Spufi executeUpdate 0 0 134 7 10 Spufi executeQuery 0 0 eee 134 7 11 Spufi getColumnWidths 0 0 00 0 eee 135 7 12 Spufi printRow oseaan aaaea 136 7 18 Spufi getColumnValue 0000s 137 7 14 Spufi main oo yesh Bebe ee exe Rice eat ee Rand eo 138 8 1 Source code for the Hello application liliis 143 8 2 Sample JCL to run a Java application in MVS batch liliis 147 9 1 Qutput of db2sgljptint maia aee eem qe ee EE REDE 156 9 2 Customization error using lowercase characterS 00000 cee eee 159 9 83 Output from running the customization script 0 000 cee eee 162 9 4 Excerpt from SYSIBM SYSPACKAGE catalog table after profile customization 162 9 5 Uncustomized serialized profile llle 167 9 6 Customizing a serialized profile llle 169 9 7 Customized serialized profile llle eee 169 9 8 Executing the sqlj program using static SQL 1 2 2 20 0 0 eee 172 10 1 Iterator declaration as an innerclass llli ee
96. local machine or a remote server E BartSG24 6435 static war Browse z ee ues Preparing for the application installation Specify the EARAVARMAR module to upload and install i Choose the local path if the ear resides on the same machine as the browser Choose the server path if the ear resides on any of the nodes in your cell context pami o oo Context Root Used only for standalone Yeb modules war itso i You must specify a context root if the module being installed is a WAR module lt Previous Next gt October 29 2003 4 13 29 AM GMT C o Total Configuration Problems 0 Preferences Figure 15 14 Install a new application ag internet On the next window Figure 15 15 on page 272 we do not need to change anything just click Next to continue Chapter 15 Using Servlets to access DB2 271 E WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Bak gt Q A Gsearch GaFavortes meda 4 D 3 SI a Address E http wtsc63 itso ibm com 9080 admin secure logon do gt Go Links au 7 r A AE WebSphere Application Server Administrative Console LN Version 5 i Home Save Preferences Logout Help User ID z 1 Preparing for the application installation cel111 Servers You can choose to generate default bindings and mappings i E Applicati
97. name that adheres to these conventions The collection flag also causes the named collection to be registered in the serialized profile When using the T4 driver this information is passed to the server and is used to search for the correct package at the server DB2 for z OS and OS 390 Ready for Java Properties for SG24 6435 iol x Info SQLJ Customization Script BeanlInfo Path External Tools Builders URL jdbc db2 wtsc63 itso ibm com 33756 DB7Y Java Build Path Java Compiler User Javadoc Location Java JAR Dependencies Password Java Task Tags Project References Options COLLECTION 56246435 Server Preference SQLJ Customization Script SOL Profile Package Root Name Validation Figure 9 3 SQLJ customization script properties Attention Make sure to specify the collection name in uppercase Otherwise you get the error message shown in Example 9 2 when executing the Ant script Example 9 2 Customization error using lowercase characters Buildfile C wsad51 workspace SG24 6435 SQLJAntScripts sqlj customize xml com ibm itso sg246435 sqlj Hello java ibm db2 jcc sqlj java ibm db2 jcc sqlj Begin Customization java ibm db2 jcc sqlj Loading profile com ibm itso sg246435 sqlj Hello SJProfileO java ibm db2 jcc sqlj Customization complete for profile com ibm itso sg246435 sqlj Hello_SJProfile0d ser java ibm db2 jcc sqlj Begin Bind java ibm db2
98. not use implicit contexts by omitting the ctx clause However there are two situations where it is mandatory to declare your own context classes gt Your application connects to different database servers Recall the role of the profile customizer discussed in The DB2 profile customizer on page 154 What the customizer and binder do is to inspect each SQL statement in the ser file check it for correct syntax and authorization and create a package in the database for the ser file Obviously we cannot have a single ser file in a program that connects to different servers Rather we need several of them each one containing only the SQL statements meant for one particular database server and we need to run the customizer multiple times once for each ser file gt You use unqualified SQL and use different schemas This situation is similar to the one above You must be able to distinguish between the different schema names since the object names on the statements are unqualified Without declaring separate context classes the profile customizer cannot know against which schema to check your statements Furthermore the default schema name that applies when using unqualified SQL is recorded in the database package Obviously when using unqualified SQLJ we must have several packages one for each schema used by the program This is where the context class declaration comes into play The SQLJ translator will create a separ
99. of CGI is that it is hard to link between processes once they are started making it difficult to handle requests such as session authorization and logging The Java Servlet was introduced in 1997 to provide a Java based solution for server side processing A feature of the Servlet is that it provides answers to the execution issues of using CGI Servlets are executed in a shared Java Virtual Machine on the server which is started once and can then be reused for new requests On the initial execution of a Servlet the server initializes the Servlet once and then pools it in memory making it readily available for new incoming requests This cuts the cost of starting and stopping a Servlet Additionally Java Servlets are written in the Java language and are able to use all the features of that language such as platform independent coding in contrast to CGI At the same time the development of standard APIs for database access was going on This culminated in the Java Database Connectivity JDBC API specification and acceptance of the Enterprise JavaBean component architecture which encapsulates business functionality into a easily configured and deployed components An issue also arose with Java Servlets It had nothing to do with their performance this time The difficulty arose from the fact that Web page design was rightfully moving from software developer s hands into the realm of graphic designers The Servlet programming model does not su
100. of this process to the client in the form of dynamic Web pages by opening a PrintWriter and printing out HTML tags and data Chapter 2 An introduction to Java 2 Enterprise Edition 17 coc NNI RR destroy Called by the Servlet container to indicate that the Servlet instance is to be terminated It will only be called when all threads using the Servlet have terminated Its purpose is to allow resources held by the Servlet to be freed Although Servlets provide efficient performance with a simple architecture there can be problems due the fact they are Java programs that implement dynamic Web pages using hardcoded HTML This leads to three issues gt As the HTML is hardcoded it can be inconvenient to implement changes to the output Web pages as the program needs to be recompiled every time a page is changed gt Second it is difficult to support different languages in the Web output by determining the user s language and location and then displaying them in the dynamic page A way around this is to build Servlets for specific languages and regions but this leads to replication of the HTML and further complicates the change process gt The third issue is caused by the issue of responsibilities The rise of the Web has given rise to the Web designer normally a person with a graphics and user interface design background and usually not grounded in the development of Java programs By mixing straight Java code and HTML t
101. provide an alternative to the dynamic JDBC specification SQLJ is not part of J2EE but is part of the SQL 1999 ISO ANSI standard SQLJ consists of two parts gt ISO IEC 9075 Part 10 Object Language Bindings SQL OLB This is the specification for embedded SQL in Java and is what we discuss in the later chapters of this publication gt ISO IEC 9075 Part 13 Routines and Types Using the Java Programming Language SQL JRT This part is the specification for SQL routines using Java In this publication we focus on the support of embedded SQL in Java applications and Servlets There are three different forms of the SQLJ statement gt Executable statements gt Iterator declarations gt Connection declarations Refer to Chapter 10 SQLJ tutorial and reference on page 175 which discusses the different types of SQLJ statements and provides code examples For programmers in other mainframe languages such as COBOL and PL I embedded static SQL is almost the default way of coding DB2 database access For programmers on other platforms it is far more likely that they will be coding using dynamic statements utilizing ODBC JDBC methods The major difference between dynamic JDBC Java programs and static SQLJ programs from a development point of view is that SQLJ programs have to be preprocessed prior to execution This is due the way SQL statements are coded in SQLJ programs Unlike JDBC which is a call level interface an API
102. public MyCtx String url String user String password boolean autoCommit throws java sql SQLException v public MyCtx String url java util Properties info boolean autoCommit throws java sql SQLException v public MyCtx String url boolean autoCommit throws java sql SQLException The first of those constructors accepts a JDBC Connection object the second uses an existing connection context The others look very similar to the methods of the java sql DriverManager class and in fact create a Connection object using these parameters except that additionally you specify whether or not you want automatic commit enabled for this context If you specify the with dataSource jndiName clause a no argument constructor will also be generated which creates the connection via a DataSource object obtained by the given JNDI name 10 6 5 Specifying which connection instance to use 190 To specify which connection instance to use for an SQLJ statement you specify the name of the variable holding the reference to that instance enclosed in square brackets after the sq1 token For example sql context MyCtx MyCtx ctx ctx new MyCtx url username password false false means no auto commit sql ctx COMMIT Note The only exception is the FETCH INTO statement which does not need an explicit connection clause in fact the translator issues a warning if you code it This is demonstrated in Example 10 9 We cre
103. record A natural way of presenting the list is a tabular format in HTML what you do is create a table An HTML table in its simplest form looks like Example 15 2 on page 254 Chapter 15 Using Servlets to access DB2 253 254 Example 15 2 HTML table table tr lt td gt Cell data for first in row lt td gt lt td gt Cell data for second in row lt td gt more cell data lt tr gt lt tr gt Second row lt tr gt more rows lt table gt To display the employee list we show the data for each employee in a row with the individual attributes being the columns As mentioned before we make use of the Employee class developed in Chapter 11 SQLJ revisited on page 199 To be able to do this we have to tell WSAD that code in the SG24 6435 project should be available to the SG24 6435 Web project that is to include SG24 6435 in the Java build path Also we have to make sure that the binary code sitting in SG24 6435 is packaged with SG24 6435 Web when deploying the application To set the Java build path and to include the binary code perform the following steps 1 Select SG24 6435 Web in the Navigator view and select Properties from the context menu 2 Select Java Build Path and switch to the Projects pane Check SG24 6435 3 Then select the Libraries tab click Add External JARs and add the sqlj zip file that lives in SQLLIB Java Otherwise you may get messages indicating that Th
104. source values from the server while the last one is the JDBC class for data source object support The code for a connection using data sources for retrieving the values stored in the server uses what is called a context This returns the actual values of the data source So the first thing to do is to define the context you are going to use Context ctx Then declare the connection itself Connection conn Now you must open the data source object and return the values that are needed to make the connection to the database DB2 for z OS and OS 390 Ready for Java ctx new InitialContext DataSource ds DataSource ctx lookup jdbc DB7Y Now the values relating to the data source have been acquired by the program and the connection can be made To do this you code the following statement conn ds getConnection Or conn ds getConnection userid password And the connection is made The first version of the getConnection method uses the default user ID and password that is stored in the server On the Windows version of WAS these values are stored in the data source object itself With WAS for z OS and OS 390 no option is provided to store these values in the data source so the default user ID and password are stored in the server itself 3 4 Accessing data using SQLJ SQLJ is a series of specifications for ways to use the Java programming language with SQL It was developed by IBM Oracle and Tandem to
105. specify command line parameters 8 2 1 Creating the launch configuration To create a launch configuration select the Hello java file which was generated by the SQLJ translator in the previous step and not the Hello sqlj file then press the drop down button beside the Running Man icon From the drop down menu select Run As gt Java Application WSAD will create a launch configuration called Hello 1 and launch it Since the program expects command line arguments but we did not provide any it just prints an error message and exits see Figure 8 3 on page 146 Chapter 8 Getting started with SQLJ 145 Console terminate Sphere Studioteclipseljrelbinljavaw exe 11 11 02 4 36 PM Program Files IBM sage java com ibm itso sg246435 sq41j Hello lt url gt user lt password gt lt min gt lt max gt 1 Figure 8 8 Error message from running the Hello program 8 2 2 Specifying command line parameters 146 To specify the command line parameters we have to modify the run configuration we created in the previous step Open the Launch Configurations dialog by selecting Run gt Run or select the drop down beside the Running Man icon and select Run from there The Launch Configurations dialog opens This dialog lists all available launch configurations see also Setting up the classpath on page 111 Make sure that the launch configuration for the SQLJ program called Hello 1 is selected
106. started life as a problem to be solved The developers of Java at Sun Microsystems saw that the next big thing in the evolution of computing was the rise of consumer electronics and the increased computing power within these devices They saw a move away from the desktop computer with its proprietary operating system to a multitude of consumer computers running video home recorders sound systems and even toasters The people at Sun Microsystems then looked at what was then one of the dominant programming languages C and found it was not up to the task With consumer devices reliability not speed was seen as the key attribute C with its heritage from C was a mixture of object oriented design with the procedural elements of C Although C was a very powerful language which could compile to very fast executable code with pointers and such it was also error prone The computer users of the day were quite used to rebooting their desktop machines when fixing programming problems This was not a situation that should occur on a remote control unit or a toaster Along with the issue of reliability C tended to be platform specific Developers had to be aware of what machine and what operating system they were coding for If a program had to run on another platform the program had to be recoded to run elsewhere With the plethora of new devices you could not have this luxury Develop once run many was the mantra of this era So the new
107. that the insert method works 204 DB2 for z OS and OS 390 Ready for Java Rather than implementing the test code in the Employee class itself we create a small test class which creates a new Employee object and calls insert to create the record in the database We call the class EmployeeTest and put it in the same Java package Since the program will not contain SQL statements itself we can create a regular Java class as opposed to an SQLJ source file The easiest way to create the skeleton source code for the class is to use the New Class wizard see Creating the project on page 102 Example 11 5 shows the source code of the EmployeeTest class Example 11 5 The EmployeeTest class package com ibm itso sg246435 sq1j import java io FileInputStream import java io FileNotFoundException import java io IOException import java math BigDecimal import java sql Connection import java sql DriverManager import java sql SQLException import java util Properties public class EmployeeTest private static Employee Ctx ctx Load JDBC driver and initialize SQLJ connection context Driver name URL and driver specific properties are read from a configuration file private static void initialize throws ClassNotFoundException SQLException FileNotFoundException IOException Read the properties file 2 FileInputStream propsFile new FileInputStream EmployeeTest p
108. that this technique only works for String valued properties Get the driver name from the properties if not present use the JCC driver as a default Load the JDBC driver Get the URL property and open the connection Create an SQLJ connection context using that connection First we create and initialize an Employee instance o N DODO OQ Then we call the Employee insert method passing it the connection context As explained above this program does not use hardcoded values for the JDBC driver name URL username and password rather it expects them in a properties file Create the properties file directly in the SG24 6435 folder right click the folder name and select New File Enter EmployeeTest properties for the file name this file name is hardcoded in the program Type your connection properties Example 11 6 lines beginning with a sign are comment lines Example 11 6 Sample properties file for the EmployeeTest program Class name of the JDBC driver driver com ibm db2 jcc DB2Driver URL username and password url jdbc db2 wtsc63 itso ibm com 33756 DB7Y retrieveMessagesFromServerOnGetMessage TRUE user bartr3 password secret Now try and run the test program You should normally receive the following message com ibm db2 jcc c SQLException AN UPDATE INSERT OR SET VALUE IS NULL BUT THE OBJECT COLUMN CANNOT CONTAIN NULL VALUES SQLCODE 407 This is because we did not supply a
109. the customization step is so important please refer to Chapter 9 The SQLJ program preparation process on page 149 3 5 Using JDBC or SQLJ Following are some good reasons to consider using SQLJ as well as some reasons why JDBC may be more appropriate 3 5 1 SQLJ is easier to code 42 The first advantage of SQLJ over JDBC is that SQLJ is easier to code to read and to maintain This is an effect of SQLJ being not an API but a language extension providing for better integration of the SQL code with the Java code The developer can concentrate on the logic of individual SQL statements without having to worry about wrapping them in API calls This simplicity is helped by the ease by which host variables are defined maintained and accessed within an SQLJ program As SQL is coded in purely SQL syntax without the need to wrap them in a Java method the programs themselves are easier to read making them easier to maintain Also since some of the boilerplate code that has to be coded explicitly in JDBC is generated automatically in SQLJ programs written in SQLJ tend to be shorter than equivalent JDBC programs We illustrate the easier SQLJ syntax in the following examples showing a multi row query a single row query and an INSERT statement Example 3 3 on page 43 shows a multi row query The amount of coding is similar with JDBC and SQLJ Note however that the binding between statement and host variables in SQLJ is much tighter
110. the SELECT statement that is you have to declare one cursor per SELECT statement An iterator declaration in SQLJ is different it only specifies the column types and names in the case of named iterators Therefore you can assign the result of any SELECT statement to an iterator instance as long as the columns in the SELECT list match the iterator We demonstrate this in Examples 10 6 and 10 7 Example 10 6 Cursor declaration includes SELECT statement COBOL people this is only pseudocode Java people this is the only COBOL sample in this book so keep reading EXEC SQL DB2 for z OS and OS 390 Ready for Java DECLARE EMP MIN MAX CURSOR FOR SELECT LASTNAME HIREDATE FROM DSN8710 EMP WHERE SALARY BETWEEN MIN AND MAX EXEC SQL DECLARE EMP ALL CURSOR FOR SELECT LASTNAME HIREDAE FROM DSN8710 EMP EXEC SQL OPEN EMP MIN MAX PERFORM WITH CHECK AFTER UNTIL SQLCODE IS EQUAL TO 100 EXEC SQL FETCH EMP MIN MAX INTO LASTNAME gt HIREDATE END PERFORM EXEC SQL CLOSE EMP MIN MAX EXEC SQL OPEN EMP ALL PERFORM WITH CHECK AFTER UNTIL SQLCODE IS EQUAL TO 100 EXEC SQL FETCH EMP ALL INTO LASTNAME HIREDATE END PERFORM EXEC SQL CLOSE EMP ALL Let us now look at a similar example using SQLJ Example 10 7 Iterator declaration does not include SELECT statement fsql iterator EmployeeIterator String Date EmployeeIterator iter sql ctx iter SELECT LASTNAME HIREDATE FROM DSN8710 EMP WHERE SA
111. the result set type is the second parameter the result set concurrency is the third parameter Since both are of type int the Java compiler will not complain if you got it the wrong way round Restriction The combination of TYPE SCROLL INSENSITIVE with CONCUR UPDATABLE is not supported with the DB2 Universal Client 7 8 A complete example Poor man s SPUFI 130 In this section we demonstrate some techniques presented in the previous sections with a complete example program a Java version of the well known SPUFI application SQL Processing Using File Input The SPUFI tool that comes with DB2 for z OS and OS 390 reads SQL statements from a sequential file processes those statements and displays the results in an ISPF browse session Our Java version does a similar job except that it can process multiple files or in general Java input streams which may or may not be files on disk and can print the results to an arbitrary output stream by default System out The class declaration and constructors We start by showing the class declaration and constructors Example 7 5 Example 7 5 Spufi class declaration and constructors package com ibm itso sg246435 jdbc import gnu getopt Getopt import gnu getopt Getopt LongOpt import java io import java sql import java util ArrayList import java util Iterator import java util List A poor man s SPUFI SQL Processing Using File Input Processes SQL
112. this publication To avoid having to retype all the code or even copy paste it we also provide the sample Source code as additional material that you can download from the Web See Appendix C Additional material on page 313 for details Copyright IBM Corp 2003 All rights reserved 293 Hello java 294 Here Example B 1 we provide the listing of the Hello program written with JDBC Example B 1 Hello java Created on Oct 25 2003 To change the template for this generated file go to Window amp gt Preferences amp gt Java amp gt Code Generation amp gt Code and Comments xf package com ibm itso sg246435 jdbc import java math BigDecimal import java sql Connection import java sql DriverManager import java sql PreparedStatement import java sql ResultSet import java sql SQLException Sample test program to retrieve all employees in the EMP sample tables whose salary is in a given range author Ulrich Gehlert public class Hello JDBC URL to the database private static final String url jdbc db2 wtsc63 itso ibm com 33756 DB7Y sretrieveMessagesFromServerOnGetMessage TRUE User name to connect to the database private static final String user bart Password for the database connection private static final String password secret public static void main String args Connection conn null PreparedStatement stmt null
113. time Casting Explicitly converting an object s or primitive s data type Catalog In DB2 a collection of tables that contains descriptions of objects such as tables views and indexes Catalog table Any table in the DB2 catalog CGI The Common Gateway Interface CGI is a means of allowing a Web server to execute a program that you provide rather than to retrieve a file A number of popular Web servers support the CGI For some applications for example displaying information from a database you must do more than simply retrieve an HTML document from a disk and send it to the Web browser For such applications the Web server has to call a program to generate the HTML to be displayed The CGI is not the only such interface however Character large object CLOB A sequence of characters single byte multibyte or both up to 2 gigabytes A CLOB can be used to store large text objects Also called character large object string Compare to Binary large object BLOB Class An encapsulated collection of data and methods to operate on the data A class may be instantiated to produce an object that is an instance of the class Class hierarchy The relationships between classes that share a single inheritance All Java classes inherit from the Object class Class method Methods that apply to the class as a whole rather than its instances also called a static method Class path When running a Java a list of directories and JA
114. to translate your PC s ASCII codes into host EBCDIC codes so that they can talk each other This protocol converter also serves to translate the 3270 command sequences into VT terminal command sequences zj OK Cancel Help Figure 5 10 PCOM Telnet screen after choosing Communication gt Configure gt Attachment Change the Attachment to VT over Telnet Then click the Link Parameters button to produce the screen shown in Figure 5 11 on page 69 DB2 for z OS and OS 390 Ready for Java TelnetASCII LX Host Definition Advanced Security Setup Host Name or LU or Port IP Address Pool Name Number Primary wtsc630e itso ibm com 23 Backup 1 23 Backup 2 23 Auto reconnect Enable Security Terminal ID v Default Cancel Apply Help Figure 5 11 Link Parameters changed Adding the host DNS port Specify the host name or IP address Our system had been configured to use native USS using the host name WTSC630E It is also configured to use port number 23 Find out what your values are Then click OK to return to the Customize Communication screen Click the Session Parameters button and go to Figure 5 12 Session Parameters ASCII Host xi Session Parameters Online Local C Online Local Operating Mode Echo C Char Machine Mode VTANSI ha Screen Size 24x80 m Type of Host Code Page National MultiNational C PC Host Gode Paue Optional
115. token NOT FOUND IN PLAN plan name REASON reason DB2 for z OS and OS 390 Ready for Java So when you receive a 805 SQLCODE DB2Sq1ca getSqlErrmcTokens will return a six element array containing the location name collection ID DBRM name consistency token plan name and reason code in that order The meaning of the reason code is also documented in the message description Example 14 4 demonstrates how to retrieve DB2 specific error information Example 14 4 Retrieving DB2 specific extended error information try SQL calls catch SQLException e if e instanceof DB2Diagnosable DB2Sqlca sqlca DB2Diagnosable e getSqlca System err println SQLERRM sqlca getMessage System err println SQLCODE sqlca getSqlCode System err println SQLSTATE sqlca getSqlState String tokens sqlca getSqlErrmcTokens if tokens null for int i 0 i tokens length i System err println ERRMC i tokens i System err println SQLWARN new String sqlca getSqlWarn else System err printIn e In addition the DB2 JCC driver provides the utility class com ibm db2 jcc DB2ExceptionFormatter which prints formatted error information to a stream similar to what we did explicitly in 14 4 You may find this utility class useful for logging purposes Summary of DB2Sqica methods Below is a summary of DB2Sqica methods gt getSqlErrmcTokens public abst
116. try Chapter 15 Using Servlets to access DB2 265 ctx new Employee Ctx Employee emp Employee findByPrimaryKey ctx empno out resp getOutputStream 3 pic emp getPicture ctx copy pic out catch Exception e throw new ServletException e finally Clean up 4 if pic null pic close if out null out close private void copy InputStream in OutputStream out throws IOException 5 byte buf new byte 1024 int nread while nread in read buf gt 0 out write buf 0 nread Notes on Example 15 9 on page 265 1 This tells the client usually a Web browser the content type of the response The default for HttpServlets is a content type of text html indicating that the response is an HTML document Since we return a different content type a GIF image we have to override that default 2 Again for the time being the employee number is hardcoded 3 While the previous examples used the getWriter method to obtain the Servlet output stream this Servlet uses getOutputStream The getWriter method is used when your content type is text such as HTML whereas getOutputStream must be used for binary content types 4 To make sure that resources are freed even in the case of an error the cleanup code is in a finally block 5 This is a utility method that reads the input stream in chunks and copies the data to the output stream until no more data is available
117. value for a NOT NULL column in the table see Table 11 1 on page 200 the middle initial column must be specified 206 DB2 for z OS and OS 390 Ready for Java Of course it would be easy to change the test program by adding emp setMiddleInitial But a better approach is to modify the Employee class and ensure that the middle initial is always initialized to an appropriate default value a string containing just one blank Find the declaration of the middle initial field in Employee sqlj and modify it to initialize the field with a default value private String middleInitial While we are at it we implement a toString method to return a printable representation of an Employee object Example 11 7 Example 11 7 Create printable representation Return a printable representation public String toString return getLastName getFirstName getMiddleInitial Retranslate Employee sqlj then run EmployeeTest again This time the program should print Employee Dent Arthur created successfully Without the toString method the program would have printed something like Employee com ibm itso sg246435 sqlj Employee 420f0a00 created successfully Run EmployeeTest one more time This time it should print com ibm db2 jcc c SqlException AN INSERTED OR UPDATED VALUE IS INVALID BECAUSE INDEX IN INDEX SPACE XEMP1 CONSTRAINS COLUMNS OF THE TABLE SO NO TWO ROWS CAN CONTAIN DUPLICATE VALUES IN T
118. when applications at both ends of the invocation are written in Java Included in JDK 1 1 Java Runtime Environment JRE A subset of the Java Development Kit for end users and developers who want to redistribute the JRE The JRE consists of the Java Virtual Machine the Java Core Classes and supporting files Definition copyright 1996 1999 Sun Microsystems Inc All Rights Reserved Used by permission Java Servlet Servlets are similar to CGI programs except that they are written in Java and run in a Java Virtual Machine managed by the Web server Servlets are an effective substitute for CGI scripts because they provide an easier and faster way to generate dynamic documents They also address the problem of doing server side programming with platform specific APIs because they are developed with the Java Servlet API a standard Java extension Servlets are modules that run inside Java enabled Web servers and extend them in some manner For example a Servlet might be responsible for validating the data in an HTML order entry form Java Virtual Machine JVM A software implementation of a central processing unit CPU that runs compiled Java code applets and applications JavaDoc Sun s tool for generating HTML documentation on classes by extracting comments from the Java source code files JavaScript A scripting language used within an HTML page Superficially similar to Java but JavaScript scripts appear as text within the HTML
119. with little regard to the database you are actually using as all the platform dependant code is stored in the JDBC drivers Having said that JDBC also has to be flexible in what functionality it does and does not provide solely based on the fact that different database systems have different levels of functionality For example JDBC provides a method called getArray which supports a column type of ARRAY This column type is not supported by DB2 and therefore such a function is meaningless When a DBMS does not support such functions the JDBC standard says that if the method is executed in a program the driver has to raise an SQLException So although JDBC allows for greater portability it is important to understand what functionality is available in the database system that you are using The other side of the coin is that vendors that provide JDBC drivers can add functionality htat is database system specific These methods are known as extensions Although they may provide benefits they should also be used carefully as they limit portability 3 1 1 JDBC driver types As mentioned before the JDBC drivers provide the physical code that implements the objects methods and data types defined in the JDBC specification The JDBC standard defines four different types of drivers The distinction between them is based on how the 24 DB2 for z OS and OS 390 Ready for Java driver is physically implemented and how it communicates with th
120. with the other variables When you are done you can Save the new datasource definition In addition to the standard DB2 JDBC datasource properties that are initially defined for the datasource you can also add other properties to reflect new properties that are now supported by the new DB2 JCC datasource implementation To do so using the administrative console go back to the JDBC provider datasource you just created and click Custom Properties The Custom Properties screen is displayed again For each new property you wish to add click New Once you click New a screen is displayed where you can enter the new Property Name Property Description Property Value and Property Type After entering all the property information click Apply Repeat this process for each property you wish to add After applying the last property you wish to add click Save to save all the new datasource custom properties We did not add any additional properties for our simple application To make sure the datasource definition gets activated we bounced the WAS Server Chapter 5 Setup 97 98 DB2 for z OS and OS 390 Ready for Java Part 3 Putting it all together In the previous parts of this publication we described the concepts and facilities for accessing DB2 from Java and how to set up your environment In the following chapters we take you through a step by step description much like a cookbook of how to actually use all the different parts and
121. wrote this redbook liliis xxii Become a published author 0 0 0 rn xxiv Comments welcome 0 000 ccc ct ete tees xxiv Summary of changes osis ayee madea ean p a a a E A N A A ARE XXV December 2004 ziii an bred a hte ee hd met Sd doe A uua a hee ak ad XXV Part 1 Introduction o cerae PEE Lan Gc dee xp A ane aaa ee ile Gwe Ss 1 Chapter 1 A brief history of Java 0 0 00 eee 3 1 1 From toaster to the enterprise llle 4 1 1 1 Java and the consumer electronics revolution llle 4 1 1 2 Accessing tne data iss padi ee aE Pies aie eG ER PERLE E Gade 5 1 1 3 The rise of the Internet server 5 1 1 4 The Internet and the enterprise llli 6 1 2 Java and the z OS and OS 390 platforms lesen 6 Chapter 2 An introduction to Java 2 Enterprise Edition 9 2 1 The three technology editions for the Java 2 platform 00 eee 10 2 2 Java 2 Platform Enterprise Edition llle 10 2 9 Java 2 features i dca eae vRIA V IDEM AREE Rome eR Band dhe las Die 12 2 3 1 Object oriented programming lisse see 12 2 3 2 Primitive data typeS seccions ceiien i iaa ne CEN n 13 2 3 8 Garbage collection sisin E eee 13 2 3 4 Removal of pointers asuaaa anaana rnana tte 13 2 3 5 No More GOTOS c utu Ea a ia E ae e E exe ERR e e A eee e Cen 14 2 3 6 Java Virtual Machine 0 en 14 2 4 Java application environments 0 0 0 eee eee 15
122. 00130 bmp is created in the C temp directory which you can view with a Web browser Storing employee photos in the database Now that we are able to retrieve employee photos we also want to store them in the database Example 11 23 shows the createPicture method which takes an InputStream argument from which it reads the picture in BMP format and a length argument that gives the size of the bitmap in bytes Also note that we called the method createPicture rather than setPicture since the method only allows creating a picture not replacing an existing one If we want that functionality we first would have to figure out if a picture already exists then perform either an INSERT or an UPDATE statement Implementing this is left as an exercise Example 11 23 Employee createPicture import sqlj runtime BinaryStream import java io IOException import java io File import java io FileInputStream Creates the employee s picture param picture An InputStream supplying the picture in BMP format On return from this method the stream is closed exception SQLException A database error occurred exception IOException An error occurred reading the input stream public void createPicture Ctx ctx InputStream picture int length 1 throws SQLException IOException Chapter 11 SQLJ revisited 217 try BinaryStream lobStream new BinaryStream picture length 2 sql ctx
123. 1 Basic error handling 240 As with all error handling in Java error handling for JDBC and SQLJ uses the try catch construct Whenever an error is encountered in a JDBC or SQLJ program the JDBC driver throws an SQLException JDBC provides getter methods to produce the SQLCODE and the related message These methods are getErrorCode to return the SQLCODE and getMessage to produce the full text error message related to the error generated Both are methods of the SQLException class Tip Although the objects and methods described here are JDBC related they also apply to SQLJ programs In fact error handling for JDBC and SQLJ programs is identical Warning messages are handled differently and do not throw an exception so if desired they must be handled specifically using the SQLWarning class As multiple warnings can be generated from a single SQL statement JDBC creates an object for each warning received and chains them together If a program is to output the warnings it receives the SQLWarning objects have to be retrieved using the method getWarnings of the Connection object You then step through the warnings using the getNextWarning of the SQLWarning object Like the SQLException class the SQLWarning class allows you to retrieve the SQLSTATE and SQLCODE using the methods getSQLState and getErrorCode respectively Example 14 1 gives examples of error and warning handling in JDBC Example 14 1 Error handling in JDBC
124. 113 and Exporting via FTP on page 115 respectively Tip For more information about DFS SMB and Samba refer to S 390 File and Print Serving SG24 5330 6 3 1 Exporting to a shared file system To export your code to a USS directory you first have to map that directory as a drive on your PC From Windows Explorer select Tools Map Network Drive Select a drive letter and enter the network path for the shared directory In our case the network path is wtsc63 rc63 which maps to the u rc63 directory on HFS see Example 6 6 on page 114 Chapter 6 Getting started with JDBC 113 114 Map Network Drive Eg Windows can help you connect to a shared network folder and assign a drive letter to the connection so that you can access the Folder using My Computer Specify the drive letter for the connection and the Folder that you want to connect to Drive lu Folder wtscestrcea Browse Example server share IV Reconnect at logon Connect using a different user name Create a shortcut to a Web folder or FTP site lt Bad Cancel Figure 6 6 Accessing the HFS file system as a shared network drive After mapping the network drive you should be able to browse it in Windows Explorer just like a regular local drive To export the code to the mainframe follow these steps 1 Select the project folder SG24 6435 then select File Export 2 Select File System from the list of export destina
125. 127 130 connection context 145 176 188 default 176 189 implicit 188 connection properties 246 connection properties in the URL 222 ConnectionContext close 194 getConnection 194 getExecutionContext 195 isClosed 195 consistency token 150 154 console 110 Container Managed Persistence 21 Container managed persistence 20 context 40 context class 189 using more than one 194 context root 250 270 ContextClassName 189 conversion 115 converting 329 JDBC result set into SQLJ iterator 198 SQLJ iterator into JDBC result set 198 CORBA 12 CS 176 CTM 19 CUE 32 cursor stability 176 D data perspective 74 DataSource 36 39 105 173 datasource definition 91 setup 82 Datasource Helper Classname 92 DB2 authentication 226 DB2 built in functions 234 DB2 Connect 29 Enterprise Edition 32 Unlimited Edition 32 DB2 Connect Server 32 DB2 for iSeries 28 DB2 package 154 DB2 specific error handling 242 DB2 Universal Driver for Java Common Connectivity 26 27 DB2 Universal Driver for SQLJ and JDBC 222 DB2390LocalDataStoreHelper 93 DB2BaseDataSource 244 DB2Binder 59 DB2Connection setJccLogWriter 244 DB2ConnectionPoolDataSource 259 DB2Diagnosable 242 DB2ExceptionFormatter 243 db2j2classes zip 27 db2java zip 27 db2jcc jar 27 111 146 162 DB2JCC_DRIVER_PATH 84 db2jcc_license_c jar 77 db2jcc_license_cisuz jar 77 111 146 162 db2jcc_license_cu jar 77 DB2JCCPROPERTIES 87 db2jcct2 properties 61 DB2Sqlca 242 243 getMessage 244 getSqi
126. 197 getQueryTimeout 197 getWarnings 197 methods 196 setBatching 197 setBatchLimit 197 setMaxFieldSize 197 setMaxRows 198 setQueryTimeout 198 F FixPak 72 159 FixPak2 28 Fixpak 3 28 FTP 113 115 172 G garbage collection 13 108 GenericServlet 17 getApplicationTimeMillis 229 getArray 24 getBytes 128 getColumnDisplaySize 135 getColumnLabel 135 getColumnWidths 135 getConnection 38 41 105 getCoreDriverTimeMicros 229 getDriverMajorVersion 124 getDriverMinorVersion 124 getDriverName 124 getDriverVersion 124 getErrorCode 240 242 getExecutionContext 195 getMessage 240 getNetworklOTimeMicros 229 getObject 123 getServerTimeMicros 229 getShort 123 getSqlca 242 getSglErrmcTokens 243 getSQLState 240 241 getWarnings 240 getXxx 43 107 GOTO 14 H heap based 13 helper method 138 HFS 58 history 3 holdability 179 host expression 179 host variable 179 host variables and expressions 179 HTML 5 16 HTTP 17 l IBM DB2 Universal Driver for SQLJ and JDBC 26 27 IBM Personal Communications 68 IBM WebSphere MQ 12 IDENTITY_VAL_LOCAL 231 IEEE floating point 7 IIOP 12 inheritance 12 init 16 17 InitialContext 41 instance swapping 19 instance variable 12 interface 24 interface_list 178 Internet Inter Orb Protocol 12 interoperability 49 isolation levels 155 setting 176 iterator class 177 declaration 177 holdable 186 implements clause 178 named 178 positioned 1
127. 2 10 Working with LOB data The getPicture and setPicture methods 214 Chapter 12 The DB2 Universal Driver 0 0000 cece eee 221 12 1 What the DB2 Universal Driver for SQLJ and JDBC is llsllslsuss 222 12 2 Setting connection properties inthe URL 0 00000 cee eee 222 12 3 Functionality enhancements 000 ae 225 12 3 1 Scrollable cursor support 2 0 0 eee 225 12 3 2 Batch Updatese c 2 c0 Be kk hate dete ee ngu eee dd Bae eae 225 12 3 3 Improved security for DB2 authentication llle 226 vi DB2 for z OS and OS 390 Ready for Java 12 3 4 Improved Java SQL error information llli else 226 12 3 5 Java API for Set Client Information SQLESETI 00005 226 12 3 6 Java API for application monitoring llle 228 12 3 7 Native DB2 server SQL error messageS 0000 e eee eee 230 12 3 8 Multiple open cursors slssellseee Rr 230 12 3 9 SAVEPOINT support aasan saaana ne 231 12 3 10 Auto generated keys ec eee 231 Chapter 13 Performance topics 0 00 cece eee cee 233 13 1 General performance recommendations 00 cece eee 234 13 1 1 Use static SQL wherever possible 0 0 00 cece eee 234 13 1 2 Turn auto commit off 0 0 0 0 000 ee 234 13 1 3 Only retrieve update columns as needed 000 eens 234 13 1 4 Store numbers as numeric data types 00 00 e eee eee
128. 2 4 1 Stand alone Java applications 0 0 0000 16 2 42 Javaapplets seb pue ae ee ee epe EMEN ee eR 16 2 4 8 Java Servlets ananunua anea 17 2 4 4 JavaServer Pages rsen 18 2 4 5 JavaScripts co lll ELA a a es E LCS 19 2 4 0 Java Beans aa iste Sen ae oka eR E RG D RU ENDE eS 19 2 4 7 Enterprise Java Beans lisse nen 19 Chapter 3 Accessing DB2 from Java isilsils leise 23 3 31 JDBC DaSICS 2 3 oh Soest Mao AE E dose E as datas A T 24 3 1 1 JDBC driver types 0 0 rne 24 3 1 2 The IBM DB2 Universal Driver for SQLJ and JDBC 0000005 27 Copyright IBM Corp 2003 All rights reserved iii 3 2 Different ways to connect to a DB2 for z OS and OS 390 uisus 30 3 2 1 Direct T2 connection to a local DB2 subsystem 000000 30 3 2 2 Using the Type 4 driver to talk to a local DB2 for z OS and OS 390 30 3 2 3 Type 4 connectivity from a non z OS platform llli elles 31 3 2 4 Type 2 connectivity from a non z OS platform llli 32 3 2 5 DB2 for z OS and OS 390 as a DRDA Application Requester 33 3 2 6 IBM z OS Application Connectivity to DB2 for z OS and OS 390 34 3 8 Developing a Java application using JDBC 0 cee 36 3 8 1 Connecting to a database 1 teens 36 3 3 2 Using the DriverManager interface liliis 37 3 3 3 Connecting using the DataSource API 0 1 2 0 0 0 0 sn
129. 2 for Linux Unix and Windows Version 8 became generally available At that time only the Type 4 JDBC driver was available DB2 UDB for LUW FixPak 2 added the Type 2 driver to the Universal Driver family Most of our samples throughout this publication use WebSphere Studio running on a Windows environment but you can also run Java programs natively from a command prompt The Universal Driver for SQLJ and JDBC is installed as part of the normal DB2 UDB for LUW V8 installation process The default location of the driver is c Program Files IBM SQLLIB java However since most people tend to install DB2 in c SQLLIB we have tried to consistently use that directory throughout this publication Updating the environment variables The DB2 installation process normally takes care of updating the CLASSPATH environment variable but it does not hurt to check again to make sure In addition when you want to run Java applications from the command line Start gt Programs gt DB2 gt Command Line Tools gt Command Window you have to make sure the PATH environment variable is set up correctly Also when running the Ant scripts in WSAD to do SQLJ customization see Preparing SQLJ programs to use static SQL through WSAD on page 157 they use the Windows classpath settings To check the environment variables on a Windows system you can select Start gt Control Panel gt System gt Advanced gt Environment Variables In the System Vari
130. 234 13 1 5 Use DB2 built in functions 2 0 0 0 ae 234 13 1 6 Release resources 1 1 tees 235 13 2 JDBC recommendation 00 000 ce teens 235 13 3 SQLJ performance considerations 0 0 0 eens 235 13 3 1 Use matching datatypes eose rsin eee 235 13 3 2 Use positioned iterators not named iterators 222200 00 236 13 3 3 Always customize with online checking enabled 000 236 13 3 4 Check explain tables 1 0 0 00 c ess 237 13 3 5 Rebind packages regularly llis 237 13 4 System level performance tuning 000 cee eee 237 13 4 1 Tune the JVM heap Size 1 ee 237 13 4 2 Get the latest code and maintenance 0 00 c eee eee 237 13 4 3 Turn on DB2 dynamic statement caching llli sees 238 Chapter 14 Error handling and diagnosis 000 0c eens 239 14 1 Basic error handling 523a Bele ete kx m Rdg Y Rb ee ae ee 240 14 2 SQLCODE and SQLSTATE ssssssssssses rn 241 14 3 Cleaning up resources sssssssssseee rh 241 14 4 DB2 specific error handling llli 242 14 5 Tracinig sou tiere ERE RR ERU Mele ET RR UC UR RR Regan 244 14 5 1 Turning on tracing in the program 00 eee 244 14 5 2 Turning on tracing using connection properties 0 0c eee 246 Part 4 Accessing DB2 from Web applications 0 0 ee ee 247 Chapter 15 Using Servlets to access DB2 0 000 ccc ee 24
131. 25 326 DB2 for z OS and OS 390 Ready for Java Related publications The publications listed in this section are considered particularly suitable for a more detailed discussion of the topics covered in this redbook IBM Redbooks For information on ordering these publications see How to get IBM Redbooks on page 328 Squeezing the Most Out of Dynamic SQL with DB2 for z OS and OS 390 SG24 6418 DB2 for OS 390 and z OS Powering the World s e business Solutions SG24 6257 e business Cookbook for z OS Volume III Java Development SG24 5980 Distributed Functions of DB2 for z OS and OS 390 SG24 6952 WebSphere Studio Application Developer Version 5 Programming Guide SG24 6957 S 390 File and Print Serving SG24 5330 DB2 for z OS and WebSphere The Perfect Couple SG24 6319 Other resources These publications are also relevant as further information sources Application Programming Guide and Reference for Java SC26 9932 Application Programming Guide and Reference FOR JAVA Version 8 SC18 7414 01 DB2 UDB for OS 390 and z OS Installation Guide Version 7 GC26 9936 DB2 UDB for OS 390 and z OS Application Programming and SQL Guide SC26 9933 New IBM Technology featuring Persistent Reusable Machines SC34 6034 DB2 Universal Database for OS 390 and Z OS Messages and Codes GC26 9940 DB2 Universal Database for OS 390 and Z OS SQL Reference SC26 9944 CICS TS for z OS V2 2 Java Applications in CICS SC
132. 2710 However we used pp db2javadriver as our home directory Example 5 5 Setting the CLASSPATH from the command line in Unix System Services java cp DB2HOME classes db2jcc jar DB2HOME classes db2jcc license cisuz jar com ibm itso testJDBC TestJDBC class DB2 for z OS and OS 390 Ready for Java Setting environment variables These variables can be set by hand for each session but it is more common to include the settings in the profile file in one s home directory Under Unix System Services the home directory is set up in the RACF profile for the user A USS profile is analogous to a TSO logon clist It includes the libraries and other settings that the USS user needs to perform his activities The export command is used to set up the environment variable For example to put the file db2jcc jar in your CLASSPATH you can specify the following command either on the command line or by placing it in your profile export CLASSPATH pp db2javadriver classes db2jcc jar CLASSPATH The CLASSPATH entry represents the current value in CLASSPATH so this command adds the jar file to the beginning of this environment variable Files that are added to these variables must be fully qualified so the example assumes that the required file is in the usr Ipp db2 db2 db2710 classes directory The default installation directory is usr Ipp db2 db2710 Because we use a test version of the new JCC driver on the same system that has the old JD
133. 34 6000 New IBM Technology featuring Persistent Reusable Machines SC34 6034 Unix System Services Command Reference SA22 7802 WebSphere Application Server V4 0 1 for Z OS and OS 390 Installation and Customization GA22 7834 Assembling Java 2 Platform Enterprise Edition J2EE Applications SA22 7836 WebSphere Application Server V4 0 1 for Z OS and OS 390 Systems Management User Interface SA22 7838 John Ellis et al JDBC 3 0 Specification Sun Microsystems October 2001 Hatcher et al Java Development With Ant Manning Publications Company 2002 ISBN 1930110588 Copyright IBM Corp 2003 All rights reserved 327 Referenced Web sites These Web sites are also relevant as further information sources gt Download site for DB2 Visual Explain http www ibm com software data db2 0s390 db2ve JDBC 3 0 specification http java sun com products jdbc download html gt The Samba homepage http www samba org How to get IBM Redbooks You can order hardcopy Redbooks as well as view download or search for Redbooks at the following Web site ibm com redbooks You can also download additional materials code samples or diskette CD ROM images from that site IBM Redbooks collections Redbooks are also available on CD ROMs Click the CD ROMs button on the Redbooks Web site for information about all the CD ROMs offered as well as updates and formats 328 DB2 for z OS and OS 390 Ready for Java Index
134. 4 F gt 5 E WebSphere Application Server Administrative Console Version 5 eo Home Save Preferences Logout Help User ID bart JDBC Providers gt DB2 JCC T2 20S JDBC Provider gt Data Sources gt New cel111 Servers Data Source is used by the application to access the data from the database amp data source is created under a JDBC provider which provides the specific JDBC driver implementation class Li Applications E Resources JDBC Providers Configuration Generic JMS Providers i General Properties Scope cells cel111 nodes nd111sc63 servers ws111sc63 i The scope ofthe Mail Providers configured resource This value indicates Resource Environment URL Providers the Resource Adapters configuration Security location for the configuration Environment file system Admnisirelion Name pB2ucCT2z0SDataSource i The Troubleshooting required display name for the resource JNDI Name iciempb i The JNDI name for the resource Container managed persistence IV Use this Data Source in container managed persistence CMP Cees if 4 Figure 5 37 Setting up a datasource definition Chapter 5 Setup 93 E WebSphere Administrative Console Microsoft Internet Explorer C Lx
135. 4 2 Bad style Iterator not closed in case of exception 2 0c ee eee 241 14 8 try finally construct to ensure proper closing of iterator 242 14 4 Retrieving DB2 specific extended error information 2 000 eae 243 14 5 Turning on tracing from your program 2 1 eee 245 14 6 Trace output from running the Hello application llle 245 15 1 The doGet method skeleton 000 c cc teens 252 15 24 ATM table vows t epa Ee este E Siete e ees o e den e RG 254 15 8 The EmployeeListServlet printCol utility method llis 254 15 4 The EmployeeListServlet printRow method sslessesleseessss 255 15 5 The EmployeeListServlet printTable method llle 255 15 6 Adapted doGet method 0 0c res 256 15 7 Sample error message from console lille 257 15 8 EmployeeDetailServlet doGet first version 0 0 0 0 000 cee eee 263 15 9 EmployeePicServlet doGet first version llli eee 265 15 10 EmployeeListServlet printRow modified isses 267 15 11 EmployeeDetailServlet displaying a picture llli 268 16 1 Skeleton JSP file generated by the New JSP Page wizard 283 16 2 Employeelistjsp niae kaiaa a E e 284 16 3 EmployeeDetailjsp 0 0 ee rh 285 BEL Hello ja Vd eer bete td ed ER ERU a tes 294 B 2 Hello sgl 5 2 ug el ee ee a SG REM VAR e ebd 295 DB2 for z OS
136. 5 1 1 the use of a iterator class in a method return will no longer be flagged as an error Finally we need a method to fetch the current row and construct an Employee object Example 11 17 on page 213 Again this method is declared static since we do not have an object to work with 212 DB2 for z OS and OS 390 Ready for Java Unlike the other methods the fetch method need not be passed a connection context This is analogous to the situation in JDBC where you do not need a Connection object in order to work with a ResultSet Instead we pass an iterator instance that had been returned by a previous call to findA11 Example 11 17 Employee fetch method public static Employee fetch EmployeeIterator iter throws SQLException String empno null 1 String firstName null String middleInitial null String lastName null Date hireDate null boolean male false BigDecimal salary null sql FETCH iter 2 INTO empno firstName middleInitial lastName thireDate male gt salary h if iter endFetch 3 return null Employee emp new Employee empno 4 emp firstName firstName emp lastName lastName emp middleInitial middleInitial emp hireDate hireDate emp male male emp salary salary return emp Notes on Example 11 17 1 Declare host variables into which the current row will be fetched For technical reasons we have to initialize all host variables otherwise the
137. 6 ADDITIONAL PACKAGES gt NO Yes to include more packages PRESS ENTER to process END to save and exit HELP for more information af Figure 9 10 Freeing the package Chapter 9 The SQLJ program preparation process 165 Then run EmployeeTest again Not surprisingly we receive the message com ibm db2 jcc c SQLException DBRM OR PACKAGE NAME DB7Y S6G246435 HELLO2 000000F29E5625A7 NOT FOUND IN PLAN DISTSERV REASON 04 This indicaties that the package referred to by the SQLJ profile could not be found and thus verifies that the SQLJ runtime tried to run the program using static SQL To recreate the package simply run the Ant buildfile again Note As shown in this example once the profile has been customized for DB2 the package has to be present for the application to run SQLJ only reverts to JDBC at runtime is no customization for that particular database that you are running against exists in the serialized profile 9 6 2 Doing it yourself Manual program preparation for static SQLJ If you do not have a nice tool like WSAD that does all or most of the program preparation work for you you can still do it by executing a set of commands as we show hereafter In order to not have to retype our Hello sqlj program we export it into the file system from WSAD Right click the project and select Export File System to get to the dialog shown in Figure 9 11 Select only the Hello sqlj file and make sure the
138. 77 with clause 178 iterator declaration 177 iterator variable 145 J J2EE 3 6 9 10 Connector Architecture 12 J2EE 1 2 19 J2EE 1 3 19 J2EE 1 4 19 navigator view 250 J2EECA 12 J2ME 9 10 J2SE 9 10 JAAS 226 JAF 12 Java 19 history 3 java compile command 117 Java applications applets 16 stand alone 16 Java Authorization and Authentication Service 12 226 Java Beans 19 Java Cryptography Extension 226 Java Database Connectivity 5 11 24 Java Debug Wire Protocol 119 Java Development Kit 7 Java Generic Security Service 226 Java Messaging Services 12 Index 331 Java Naming and Directory Interface 11 Java package 102 Java Platform Debugger Architecture 119 Java Remote Method Protocol 12 Java Runtime Environment 54 Java Server Pages 18 Java servlets 17 Java Transaction API 12 Java Type 180 Java Virtual Machine 14 java lang ClassNotFoundException 110 java sql package 110 java sql DriverManager 190 java util Properties 39 Java2 5 12 Java2 Enterprise Edition 9 Java2 Platform 9 Enterprise Edition 9 10 Micro Edition 9 Standard Edition 9 Java2 Runtime Environment Standard Edition 54 Java2 Technology Enterprise Edition 6 JavaBeans Activation Framework 12 javac 16 117 168 JavalDL 12 JavaMail 12 JavaScript 19 JavaServer Pages 6 10 11 JavaServer pages 279 javax servlet http HttpServlet 251 JCC 26 27 JCC driver 278 licensing 77 JCE 226 JDB7712 FMID 58 JDBC 3 5 10 11 24 101 JDBC 2 0 39 225 JDBC 2 0 compliant 39 J
139. 9 15 1 Creating the project i i ves cete ed IR oet x e Ep Re RR RM AL 250 15 2 Creating the EmployeeList Servlet liliis leise 251 15 2 1 Implementing the doGet method lsllsilsllsislesessn 251 15 22 Testing the Servlet i2 2o edu redet PicRE Gud eb inr ce Ross 252 15 2 3 Displaying the employee list llle 253 15 3 Running the completed EmployeeList Servlet 00 0c eee eee 256 15 4 Creating the EmployeeDetail Servlet lllliliis liess 263 15 5 Creating the EmployeePic Servlet 0 0000 265 15 6 Putting ittogether 20 eae a e eee 266 15 6 1 Modifying the EmployeeList Servlet 00 000 c eee eee 267 15 6 2 Modifying the EmployeeDetail and EmployeePic Serviets 267 15 6 3 Using EmployeePicServlet from EmployeeDetailServlet 268 15 7 Deploying the application to WebSphere for z OS 0 00 cee eee 269 15 7 1 Customizing the Web application to run as static SQL lius 269 Contents vii 15 7 2 Creating a WAR fil amp i3 22 8 erena etann a PP uL LEUEStCFPYULdu 269 15 7 3 Installing a new application on WAS for Z OS 0 00000 cee eee 270 15 7 4 Test the application llli 277 Chapter 16 JavaServer Pages iiiiuiisllsl lees 279 16 1 Introd ctionr zi ee tooniexiquRebesteuurepenereridseei ene rte eade vide aed 280 16 2 Creating the EmployeeList USP 0 0 00 280 16 3 Creating
140. 90 Thread 1 ctx1 SELECT 1 Thread 1 ctx1 Salary 23800 00 2 Thread 1 ctx1 Updating row 3 Thread 1 ctx1 Sleeping for 10 seconds 4 Thread 2 ctx1 SELECT 5 Thread 3 ctx2 SELECT 6 Thread 2 ctx1 Salary 24100 00 7 Thread 1 ctx1 Awake 8 Thread 1 ctx1 Rollback 9 Thread 3 ctx2 Salary 23800 00 10 What happens is the following NO oO ov The updating thread first issues a SELECT and retrieves the old value 23800 00 Next it updates the row obtaining an exclusive lock on the row or page It now sleeps for 10 seconds giving the other threads a chance to query the database The second and third thread each issue their SELECT statements Thread 2 which uses the same context instance as Thread 1 immediately gets its result seeing the updated but uncommitted value Thread 3 on the other hand has to wait for the exclusive lock on the updated row to be released After 10 seconds Thread 1 wakes up Chapter 10 SQLJ tutorial and reference 193 9 and rolls back the change also releasing the lock on the modified row 10 Now that Thread 1 released the lock Thread 3 is unblocked and sees the old value of the column 10 6 6 Using more than one context class As explained before it is not strictly necessary but recommended to declare your own context class you could create and use an instance of class sqlj runtime ref DefaultContext as long as you do
141. ACE_STATEMENT_CALLS 0x0002 TRACE_RESULT_SET_CALLS 0x0004 TRACE_DRIVER_CONFIGURATION 0x0010 244 DB2 for z OS and OS 390 Ready for Java To turn tracing off call setJccLogWriter with a nul argument Example 14 5 shows how to turn a trace on and off Example 14 5 Turning on tracing from your program DB2Connection conn DB2Connection DefaultContext getDefaultContext getConnection PrintWriter jccLogWriter new PrintWriter new FileWriter C temp jcctrace log int tracelevel DB2BaseDataSource TRACE CONNECTION CALLS DB2BaseDataSource TRACE DRIVER CONFIGURATION DB2BaseDataSource TRACE RESULT SET CALLS DB2BaseDataSource TRACE STATEMENT CALLS gt conn setJccLogWriter jccLogWriter tracelevel Turns tracing on conn set ccLogWriter null Turns tracing off The trace produced by running the Hello sample from Getting started with SQLJ is listed in Example 14 6 Example 14 6 Trace output from running the Hello application Thread main Connection 48f70faa prepareStatement SELECT LASTNAME FIRSTNME SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN AND ORDER BY 1 2 1003 1007 called Thread main Connection 48f70faa prepareStatement returned PreparedStatement 22df8fa9 Thread main PreparedStatement 22df8fa9 setBigDecimal 1 30000 called Thread main PreparedStatement 22df8fa9 setBigDecimal 2 50000 called Thread main PreparedStatement 22df8fa9 executeQuery called Thread main
142. AD comes with built in Ant integration so starting Ant from within WSAD is as easy as pushing a button This eliminates the necessity to leave WSAD go to the DB2 command window run the customizer go back to WSAD and refresh the workspace Customization script generation Let us now try to get the Hello sqlj program to run using static SQL Right click the project then select Properties Select the SQLJ Customization Script panel and fill out the URL field Figure 9 3 on page 159 Important Filling out the URL field is important When you leave it blank customization is likely to fail The JCC driver by default tries to bind the packages against the database during customization automaticbind YES is the default In addition the driver will do online checking onlinecheck YES is the default Online checking is very important See Why online checking is good for you on page 155 for more details The dialog also allows you to fill out the User and Password fields However as this is a possible security exposure we suggest that you leave them blank We also recommend and assume in the following sections that you enter the following in the Options field collection G246435 This causes the packages to be created in a collection named SG246435 if you do not specify this option they are created in the NULLID collection Your shop probably has conventions for collection names so you may want to replace SG246435 with another
143. ANTs EXECUTE privilege to PAYROLL SQLJ application UPDATE table UPDATE V empiD LASTNAME SALARY 000130 QUINTANA 23800 00 UPDATE 000140 000150 NICHOLLS 28420 00 25280 00 ADAMSON SELECT y PAYROLL has SELECT privilege Figure 3 13 Static SQL no UPDATE but EXECUTE privilege Note When using the Type 2 driver life is slightly more complicated In that case Diane needs EXECUTE authority on the plan to execute it that includes the package in its PKLIST and not EXECUTE authority on the package But as before the trusted entity is no longer a user but an application 3 5 5 SQLJ is more predictable and reliable The key here is that you have static SQL statements that are bound into packages The access paths for the statements are locked in during package bind They will not change on your production system unless you recompile the program or rebind This means you can use normal change control procedures to manage the Java programs and can reasonably expect that the programs will run and perform the exact same way until the next time you make a concious change rebind or recompile 3 5 6 SQLJ allows for better monitoring With SQLJ you get much better system monitoring and performance reporting Static SQL packages give you the names of the programs that are running at any given point in time This is extremely useful for studying C
144. Application Server DB2 UDB PSSS r GE SH Tier 0 Tier Tier 2 Tier 3 Client Side Server Side Server Side Server Side Presentation Presentation Business Logic Data Logic Browser Web Container Pure HTML JSP Java Applet Servlet r Desktop J2EE Server Core J2EE Server Core Java Application JAF JAF Java Mail Java Mail JDBC JDBC Other device JNDI JNDI JTA JTA J2EE Client RMIIOP RMI IIOP Figure 2 1 J2EE application model Java Database Connectivity JDBC JDBC defines the standard Application Programming Interface API for accessing relational database systems such as DB2 from Java This API is the fundamental building block for writing DB2 Java applications and will be discussed extensively throughout this publication Java Naming and Directory Interface JNDI JNDI provides standardized Java based naming and directory services interfaces for accessing various types of naming providers such as Lightweight Directory Access Protocol LDAP Microsoft Active Directory or Domain Name Service DNS JNDI is significant to JDBC users as it is used to look up DataSource objects that can be used to connect to a database See Connecting using the DataSource API on page 39 for more details on the use of DataSource objects Enterprise JavaBeans EJBs The EJB model provides a standard for the development deployment and execution of server based Java business
145. Ar 5 B WebSphere Application Server Administrative Console H Version 5 3 j BS E 2 Home Save Preferences Logout Help User ID bart perform E specific cel111 database functions Servers n Component managed Authentication Alias B i References Applications authentication E Resources data for component JDBC Providers managed Generic JMS Providers signon to the WebSphere JMS Provi resource WebSphere MQ JMS Pi Container managed Authentication Alias i References 3 i authentication Mail Providers data for Resource Environment container URL Providers managed signon to the Resource Adapters resource Security Apply OK Reset Cancel Environment Apply exi pee pe System Administration Troubleshooting Additional Properties Connection Pool An optional set of connection pool settings Custom nem S Properties that may be required for Resource Providers and Resource Factories For example most database vendors require additional custom properties for data sources that will access the database Related Items J2C Authentication Data Entries Specifies a list of userid and password for use by Java 2 Connector security Figure 5 39 Select custom properties When you do this the Custom Properties window is displayed Figure 5 40 on page 96 You need to provide some values for some of the variables Chapter 5 Setup 95 96 E
146. B2 for z OS and OS 390 Ready for Java We also declare a connection context class an instance of which will be passed to all methods that perform database operations see Connection contexts on page 188 for a detailed explanation of connection contexts Example 11 1 Employee sqlj skeleton package com ibm itso sg246435 sq1j import java math BigDecimal import java sql Date import java sql SQLException public class Employee fsql public static context Ctx with dataSource jdbc empdb 1 private final String empNo private String firstName private String middleInitial private String lastName private Date hireDate private Short educationLevel private boolean male private BigDecimal salary Note on Example 11 1 1 This declares an SQLJ connection context The with dataSource declaration causes the SQLJ runtime to create a no argument constructor which tries to set up a connection using a JNDI lookup as explained in Creating an instance of the context class on page 190 We use this mechanism in Part 4 Accessing DB2 from Web applications on page 247 Observe that we choose the attribute corresponding to the SEX column in the table to be a boolean variable rather than a string variable When inserting data into the table we will have to make sure that the boolean value true is mapped do the string constant M and the boolean value false is mapped to F Vice versa when reading data from the table w
147. BC SQLJ for z OSDB2 driver we decided to install the JCC driver in a different directory namely pp db2javadriver Example 5 6 shows our profile With these settings we managed to run the sample Java applications in this publication Example 5 6 USS User profile This line exports the variable settings so that they are known to the system export PATH EDITOR PS1 DB2HOME pp db2javadri ver CLASSPATH DB2HOME classes sqlj zip CLASSPATH CLASSPATH DB2HOME cTlasses db2jcc jar CLASSPATH CLASSPATH DB2HOME classes db2jcc license cisuz jar CLASSPATH CLASSPATH DB2HOME classes db2jcc javax jar CLASSPATH export CLASSPATH export JAVA HOME usr 1pp java IBM J1 3 export LIBPATH DB2HOME 1ib LIBPATH LD LIBRARY PATH DB2HOME lib LD LIBRARY PATH export LD LIBRARY PATH PATH DB2HOME bin PATH export PATH JAVA HOME bin PATH export STEPLIB DB7Y7 SDSNEXIT DB7Y7 SDSNLOAD DB7Y7 SDSNLOD2 export DB2JCCPROPERTIES u bart db2jcct2 properties When you run the samples that use the DB2 Universal Driver and still have the legacy drivers JDBC SQLJ driver for z OS in your CLASSPATH you must make sure that the db2jcc jar file is in the CLASSPATH ahead of the legacy driver files because some classes have the same name in the legacy and the DB2 Universal Driver for SQLJ and JDBC However to avoid confusion it is probably best to only have one set of drivers old or new in the CLASSPATH at any one time 5 4 DB2 Universal Driver Setup
148. C translation it always transfers files in binary This is OK for the class files but not if you want to ship the source code to the mainframe 6 3 3 Running the program 116 Now that the program has been exported to the HFS file system we are ready to run it from the Unix System Services command shell 1 2 3 F Log into USS via Telnet see Unix System Services on page 68 Change to the HFS directory to which you exported the code Make sure that your CLASSPATH environment variable contains the current directory and the Jar file containing the DB2 JCC driver To verify this type echo CLASSPATH If not follow the instructions in Setting up the JDBC SQLJ environment variables on page 70 Now run the program by typing the command to run the Java virtual machine java followed by the main class com ibm itso sg246435 jdbc Hello igure 6 9 on page 117 shows a sample session DB2 for z OS and OS 390 Ready for Java EZYTE27I login bartrl EZYTE28I bartrl Password Licensed Material Property of IBM 5694 A01 C Copyright IBM Corp 1993 2001 more legal yada yada BARTR1 u bartrl cd u rc63 SG24 6435 BARTR1 u rc63 SG24 6435 gt echo CLASSPATH usr 1pp db2 db2710 classes db2jcc jar BARTR1 u rc63 SG24 6435 gt java com ibm itso sg246435 jdbc Hello THOMPSON MICHAEL 41250 00 KWAN SALLY 38250 00 GEYER JOHN 40175 00 Figure 6 9 Running the program in a USS teln
149. CT list must match the number of columns in the iterator declaration 3 5 3 SQLJ is faster In most circumstances an SQLJ application runs faster than its JDBC equivalent provided it has been customized against the database to use static SQL To execute an SQL statement the following steps have to be performed gt The SQL statement is parsed into an internal form gt DB2 checks that the user ID has sufficient authority to execute the statement gt DB2 calculates an access path With dynamic SQL these steps are performed at runtime and they have to be executed each time the program runs provided you are not using the dynamic statement cache With customized SQLJ on the other hand all three steps are done at development time DB2 only has to check whether the user running the program is authorized to execute the package that had been created for the program This is illustrated in Figure 3 10 Dynamic SQL Static SQL Check plan package Check plan package authorization authorization Parse SQL statement Check table view authorization Calculate access path Y Execute statement Execute statement Figure 3 10 Execution of dynamic vs static SQL statements Chapter 3 Accessing DB2 from Java 45 On the other hand as already pointed out the customize and bind steps are optional When you run uncustomized the SQLJ runtime
150. Code 244 getSqlErrd 243 getSqlErrmc 243 getSqlErrmcTokens 243 getSqlErrp 243 getSglState 244 getSqlWarn 244 DB2Sglca getMessage 104 db2sqljbind 156 db2sqljcustomize 55 154 169 db2sqljprint 153 156 169 db2sqljruntime zip 27 DB2SystemMonitor 230 DBRM 150 330 DB2 for z OS and OS 390 Ready for Java DDF 30 debugging an application 118 defining the datasource 91 DESCRIBE SQLDA 59 DESCSTAT 59 80 destroy 17 18 DFS SMB 113 DISPLAY DDF 76 DISTSERV plan 60 163 DNS 11 doGet 251 263 265 Domain Name Service 11 DRDA 8 26 29 30 DRDA Application Requester 33 DRDA level 159 driver properties 60 driver types 26 DriverManager 36 37 105 DriverManager getConnection 105 DSNJDBC 61 DSNLOO4I 76 DSNTIJMS 59 DSNTJJCL 61 DSNTPSMP 64 DSNUTILS 64 DSNZPARM 80 changing 59 DESCSTAT 59 80 dynamic 179 dynamic SQL 45 dynamic statement cache 45 238 dynamic Web content 17 E EAR project 250 EBCDIC 7 115 EJB 10 11 19 EJB2 0 19 EJB2 1 19 ejbCreate 21 ejbLoad 21 ejoRemove 21 ejbStore 21 embedded SQL 150 endFetch 182 Enterprise Edition 10 Enterprise Java Beans 6 10 11 19 Entity beans 20 Environment variables CLASSPATH 70 PATH 70 setting 71 STEPLIB 70 updating 72 exceptions 14 executable statements 176 EXECUTE privilege 47 execute 132 executeQuery 104 106 executeUpdate 104 106 122 ExecutionContext 195 cancel 196 executeBatch 197 getMaxFieldSize 196 getMaxRows 197 getNextResultSet
151. Create directory structure for files option is checked We export to a directory called TestSQLJ Export x File system Export resources to the local file system P x H Ow Copy of 5624 6435 O F Hello Employeelterator class te 5624 6435 O M Hello class OS sQuantscripts O M Hello java iz E ia 3 Hello sqli E LI Ej Hello s3Profile ser A Bis ouis O F Hello S3ProfileKeys class DOG idbc E H O servers Select Types Select All Deselect All To directory crest SQLJ Browse r Options Overwrite existing files without warning Create directory structure For files Create only selected directories L lt Back Next gt Fns Cancel Figure 9 11 Export to the file system 166 DB2 for z OS and OS 390 Ready for Java The export creates the following directory structure Figure 9 12 in sync with our Java package naming in the program File Edit View Favorites Tools Help EI lt a Back b al Search Folders C Ui uL X t EE Address C Test SQLI SG24 6435 com ibmiitso sq246435 sqlj Folders x Ca notessql al Ga Program Files C PsFONTS psm regclean sdwork C soue swd C Teme Test SQLJ Gi 5624 6435 E com B ibm itso FI sg246435 am windows Update Setup Files WINNT wsad51 7 wuremp hd 3KB SQLJFile H A 3 F E
152. Creating the launch configuration llle 109 6 2 2 Setting up the classpath 2 0 0 0 tee 111 6 2 3 Troubleshooting s sorana oei nri k A eee 112 6 3 Running the Hello application from Unix System Services 2 5 113 6 3 1 Exporting to a shared file system 0 0 0 cece tee 113 6 3 2 Exporting Via FTP sich ee et Rem hom Sd dor EH ed pe ee 115 6 3 3 Running the program 00 eet eee 116 6 4 Running a Java program from a Windows command prompt 117 6 4 1 Compile the Java program javac 0 ccc eee 117 6 4 2 Run the Java program java 0 eee 117 6 5 Debugging the application on the workstation 2 0000 c eee eee 118 6 6 Remote debugging cee tenets 119 Chapter 7 JDBC revisited 0 00 c cect ee 121 7 1 INSERT UPDATE and DELETE statements 0 00 00 eee 122 TA INSERTS aie treo aided ax Sten ea PE BEGIN Dm AERAR ie Seles GP dass 122 74 2 UPDATE i tacks d terrere eve Beate edis Mature ease aia 122 TALS DELETE hia se vinta EE SE HERE ER a Gs fee eae aang E usta i aae 123 2 INULL Dangling seieren iee ESSI ORDRES e ES eb de perd Ris 123 7 9 Examining result sete sede p Rer x RR RE Xo X AENEA EE AIE 123 7 4 Database metadata 0 0 hen 124 7 4 1 Information about the JDBC driver 0 0000 eee 124 7 4 2 Information about the database server 00 lel leere 124 7 4 3 Information about database objects
153. D Information Management Software DB2 for z OS and 08 390 Ready for Java Setting up your Java DB2 environment Easy to use examples including using SQLJ with WSAD V5 1 Java DB2 usage hints and tips Bart Steegmans Ulrich Gehlert Judy Ruby Brown Paul Tainsh dbooks ibm com redbooks International Technical Support Organization DB2 for z OS and OS 390 Ready for Java December 2003 SG24 6435 00 Note Before using this information and the product it supports read the information in Notices on page xix First Edition December 2003 This edition applies to Version 7 of IBM DATABASE 2 Universal Database Server for z OS and OS 390 DB2 for z OS and OS 390 Version 7 Program Number 5675 DB2 and UDB for Linux Unix and Windows V8 FixPak 2 and WebSphere Studio Application Developer WSAD V5 1 pre GA Build Copyright International Business Machines Corporation 2003 All rights reserved Note to U S Government Users Restricted Rights Use duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp Contents FIQUIOS nsn td doe p rds Ms E RR ed Reh ae AAT ix Tables acute tide Soa I RW oe teen LO one ue teu ea xiii Examples 5i bite deat Ue due ee ros uu nth es Bate dubai eu T XV NOUUCOS ote Re eie e eee heed wie based a e a ode wot xix Trademarks oct dete eC t E C ene SO eee Rp PUER me XX Preface oon E RLE rete tite aia ele pedem abere enda E TA xxi The team that
154. DB2 for LUW systems The Universal Driver DB2 for LUW V8 Fixpak 3 at the time of writing of this publication also supports gt Type 4 connectivity to DB2 for iSeries V5 Release 1 PTF SF99501 SI08452 gt Type 4 connectivity to DB2 for iSeries V5 Release 2 PTF SF99502 SI08451 S108479 8108478 gt Type 4 connectivity to DB2 for zOS V6 PTF UQ72081 UQ72082 gt Type 4 connectivity to DB2 for zZOS V7 PTF UQ72083 gt Type 4 connectivity to Cloudscape Network Server 5 1 FixPak 1 and later Ultimately the Universal Driver will also be released with Type 2 and Type 4 connectivity for DB2 for z OS and OS 390 Although not officially released for DB2 for z OS and OS 390 at the time of writing of this publication we used the Type 4 driver for many of the examples in this publication for example when running a Java program in Unix System Services connecting through the Type 4 driver to DB2 for z OS and OS 390 via DDF As a Type 4 driver is a pure Java driver it is sufficient to FTP the required jar files from the Windows platform where the driver is available with DB2 for Linux Unix and Windows Version 8 to the OS 390 platform You only need to adjust the CLASSPATH and you are up and running We had no problems whatsoever with the functionality of the drivers on both platforms a nice illustration of its portability and that of the programs using it Note however that you must have the proper licensing agreements in place befo
155. DB2 for z OS and OS 390 Ready for Java To clean up the resources used by a ResultSet PreparedStatement or Connection you call its respective close method In Example 6 1 on page 103 we call the close methods in the finally part ofa try catch finally block The Java language specification guarantees that a finally block will always be executed no matter if the code in the try block executed successfully or failed threw an exception Annoyingly the catch methods may themselves throw an exception which is why we enclosed them in a try catch block of their own ignoring any exceptions that might have been thrown finally Clean up try if rs null rs close if stmt null stmt close if conn null conn close catch SQLException ignored Note that a ResultSet is automatically closed when the PreparedStatement that returned it is closed and the PreparedStatement in turn is automatically closed when the Connection that created it is closed Still it is good practice to close everything explicitly 6 2 Running the Hello application from WSAD Now we are ready to run our sample application from within WSAD To run a program in WSAD you create a launch configuration A launch configuration contains all the information needed to invoke and run the program such as gt The main class name in our case com ibm itso sg246435 jdbc Hello gt The classpath to use gt Command line argume
156. DBC 3 0 24 226 231 JDBC applet server 26 JDBC driver 26 145 JDBC driver types 24 Type 1 25 Type 3 26 Type 4 26 JDBC or SQLJ 42 JDBC provider 87 258 jdbc defaultDataSource 188 JDBC ODBC bridge 25 JDK 7 JDK 1 1 1 7 JDWP 119 JGSS 226 JIT 15 JMS 12 JMS messages 21 JNDI 11 40 260 defaultDataSource 188 JNDI name 260 285 JNI based connectivity 28 332 DB2 for z OS and OS 390 Ready for Java JPDA 119 JRE 54 JRMP Java Remote Method Protocol 12 JSP 6 18 279 compiler 285 container 18 core language 280 page wizard 283 jspsql tag library 283 JTA 12 Just in Time compiler 15 JVM 7 14 JVM heap size 237 K Kerberos 226 L language extension 44 launch configuration 109 146 LD LIBRARY PATH 70 LDAP 11 LIBPATH 70 licensing the JCC driver 77 Lightweight Directory Access Protocol 11 LOB 58 214 location name 76 logon clist 71 LPAR 30 M main 118 matching data types 235 memory allocation 4 heap based 13 stack based 13 memory leak 13 memory models 13 Message Driven Beans 21 Message driven beans 20 message oriented middleware 5 Micro Edition 10 middleware 5 Model2 19 design 18 modifier 177 monitoring 48 multiple open cursors 230 MVS batch 147 N named iterator 177 183 declaration 178 usage 183 native SQL error messages 230 net driver 27 next 104 106 NULL handling 123 null values 179 NULLID 61 NULLID collection 158 numeric data type 234 O Oak 4 Object Language Bindings 41 obje
157. DLEVEL HIREDATE FROM DSN8710 EMP WHERE h while iter next System out printIn iter lastName t iter edLevel t iter hireDate Although the named iterator variant may look more familiar especially to seasoned JDBC programmers we suggest that you use positioned iterators instead since they are slightly more efficient Named iterators actually use positioned iterators under the cover with an additional hash table to map column names to column positions 10 5 3 SQLJ iterators versus cursors 184 If you are familiar with embedded SQL in other languages such as COBOL or C you may have noted that SQLJ iterators differ in one important respect from SQL cursors If you are not and you have no idea what we are talking about feel free to skip this section An SQLJ iterator is a regular Java object that can be passed for example to other methods even in different classes In other words the SELECT statement populating the iterator and the FETCH statement to retrieve the result rows may well be in different source files An SQL cursor on the other hand can only be used in the source file where it was declared This is illustrated by the fact that in a FETCH statement you refer to a Java iterator by a host variable reference as indicated by a colon whereas in other languages you refer to cursors by their declared names Another significant difference is that the declaration of an SQL cursor includes
158. DTD HTML 4 01 Transitional EN lt html gt lt head gt lt taglib uri jspsql prefix sql gt lt page language java contentType text html charset I1S0 8859 1 gt lt meta http equiv Content Type content text html charset 1S0 8859 1 meta name GENERATOR content IBM WebSphere Studio gt meta http equiv Content Style Type content text css link href theme Master css rel stylesheet type text css gt lt title gt Employee list lt title gt lt head gt lt body gt lt hl gt Employee listing as of lt java text DateFormat getDateInstance format new java util Date gt lt h1 gt lt sql dataSourceSpec id MyConnection dataSource jdbc empdb gt lt sql select id emplist connectionSpec MyConnection gt lt sql sql gt SELECT EMPNO LASTNAME FIRSTNME MIDINIT FROM DSN8710 EMP ORDER BY EMPNO sgl sql sql select table col width 30 col width 70 lt thead gt lt tr align left gt lt th gt Serial lt th gt lt th gt Name lt th gt lt tr gt lt thead gt lt tbody gt lt sql repeat name emplist over rows gt lt tr gt lt td gt lt a href EmployeeDetail jsp empno sql getColumn index 1 gt gt lt sql getColumn DB2 for z OS and OS 390 Ready for Java index 1 lt a gt lt td gt td sql getColumn index 2 gt lt td gt lt tr gt sql repeat tbody tab
159. E EMPNO empno h message Salary salary catch Exception e message e toString DB2 for z OS and OS 390 Ready for Java 10 11 12 13 14 Notes on Example 10 9 on page 190 oO 0 00 ROoON Declare a connection context class and two instances of that class Initialize the context instances Each instance represents a separate database connection Startthe thread which updates a database row Start the thread that selects on the same context instance as the updating thread Start the thread that selects on a different context instance First the updating thread retrieves and prints the old value Now it updates one row in the database We want the two other threads to not start querying the database before the update has been performed To ensure this we use a boolean flag and a monitor object This is a Java technique that has nothing to do with the database 10 Next the updating thread sleeps for 10 seconds giving the two other threads the chance to query the database before rolling back 11 Roll back the change 12 This loop waits for Thread 1 to signal that the row has been updated see 9 13 Single row SELECT on ctx1 which is the same context on which the update has been performed 14 Single row SELECT on a different context instance The output from running this example is shown in Example 10 10 Example 10 10 Output from Example 10 9 on page 1
160. E privilege 2 5 47 Dynamic SQL with SELECT and UPDATE privilege sss 47 Static SQL no UPDATE but EXECUTE privilege llle 48 WLM Choose a service definition liliis 62 WLM Choose options from the extracted policy 00000 eee eee 63 WLM Select an application environment llli eese 63 WLM Copy an application environment 0000 0c eee eee 64 WLM Create application environment WLM100 0000 eee eae 64 DB7YJCC environment now created nasasa saunaa tees 65 WLM Install the WLM definition llle eee 66 WLM utilities Choose to activate the policy llle 66 Activate the modified WLM policy lile BIB 67 PCOM Telnet screen after choosing Communication gt Configure gt Attachment 68 Link Parameters changed Adding the host DNS port 2 0005 69 Session parameters changed 0 0c eee e eee 69 Tell WSAD where to put its workspace 0 000 cee eee 74 WSAD The data perspective nee 75 Creating a new connection using the DB2 UDB V8 Type 4 Universal Driver 76 Select jat files eee era det sk PRUSVERT Ru ROSE LEER De ee als 77 Confirm Fiter Sa s RR eem E e RE ber Ec br Ran e BET MET eae ats 78 Database connection error lille 78 The completed connection DB7Y lsseeeeeeee ee 79 The databases from DB7Y 0 cee nn 80 The tables in DSN8710 database
161. ELDU LL BIER SN E A LLL LLL LI f fe poo eee esa ae TEECEELEEELCE THEME EJUS n eee 3 e aim e E a oc 5 E Lir cio xc ls getBinaryStream getBlob EXT ae Eu ETHICS Ea getCiob JE eE ner de e n pss d ELT EES CERES CC CCC See also Appendix A of Application Programming Guide and Reference for Java SC26 9932 for additional information about the recommended mappings of Java data types to JDBC and SQL data types 6 1 7 Cleaning up resources The last step in a well behaved application is to clean up any resources the application may have allocated In this sample program we could have done without proper cleanup code since the resources will automatically be freed by the JVM and or the operating system upon termination However things are very different if you are running in a long living environment such as a Web application server You may argue that the Java garbage collection mechanism will eventually clean up any unused objects that your program has created However you cannot tell when the garbage collector will eventually spring into action and your program may well run out of resources before that point JDBC being an API to interface with an external system the database has to use and maintain external resources that are potentially limited For example the number of open PreparedStatements or ResultSets may be in fact probably will be limited by the JDBC driver implementation 108
162. HOSE COLUMNS RID OF EXISTING ROW IS X 0000001221 SQLCODE 803 Since we violated a unique constraint We tried to insert another record with the same primary key value 11 2 5 Verifying that the program worked After running the test driver a new row should have been added to the DSN8710 EMP table If you are familiar with QMF or SPUFI you could of course now go ahead and use one of these to verify it We suggest however that you use the WSAD Data Perspective if only to get a feel of it Using the data perspective on page 74 explains how to set up a database connection for the data perspective and how to display the sample contents of a DB2 table Select the DSN8710 EMP table then select Sample Contents from the context menu The result is shown in Figure 11 3 on page 208 Chapter 11 SQLJ revisited 207 Data EmployeeTest java IBM WebSphere Studio Application Developer File Edit Source Refactor Navigate Search Project Run Window Help L Jo x Jj E ES loc we jg jajsesjslgl x ee o lee Data Definition vy x PWASS 0 Test Server J Hello java l 4 Employee sqlj EmployeeTest java X tee 5624 6435 4 9 Servers amp oe ze ER public class EmployeeTest public static void testInsert throws SQLException Employee emp new Employee 000042 H emp setFirstName Arthur emp setLastName Dent emp setMale true
163. LARY BETWEEN min AND max h while true sql FETCH iter INTO lastname hiredate if iter endFetch break d iter close sql ctx iter SELECT LASTNAME HIREDATE FROM DSN8710 EMP h while true sql FETCH iter INTO lastname hiredate if iter endFetch break Chapter 10 SQLJ tutorial and reference 185 iter close As you can see we use the same iterator for both SQL statements 10 5 4 Holdable iterators Normally an iterator is automatically closed during COMMIT operation If you want your iterator to remain open after a COMMIT you must declare the corresponding iterator class as holdable using the with clause discussed in Modifying iterator behavior using the implements and with clauses on page 178 fsql iterator MyIterator with holdability true String int Timestamp Be aware however that holdable iterators can impact performance and concurrency since resources and locks held by such iterators will not be released until you close them During a ROLLBACK operation however DB2 closes all iterators including those that were declared holdable 10 5 5 Positioned UPDATE and DELETE 186 Normally you update or delete rows by doing what is called a searched UPDATE or DELETE That is you specify which rows to be updated or deleted through a WHERE clause For example to give a bonus of 500 00 to all employees who work in department E21 you code sql ctx
164. Limit and executeBatch methods you can execute several statements in a batching fashion which may result in better performance especially in a client server environment Execution contexts are especially important in multithreaded applications sharing the same connection context If a thread attempts to execute an SQL statement using an execution context that currently is in use by another thread a RuntimeException will be thrown Therefore execution contexts may only be shared between threads if their usage is properly synchronized This includes calling getter methods on the execution context They must be protected by the same synchronized block as the operation since otherwise there would be a potential race condition between threads meaning that the information could have been overwritten by another thread in the meantime Example 10 11 on page 196 Chapter 10 SQLJ tutorial and reference 195 196 Example 10 11 Synchronizing access to the execution context WRONG warnings may have been overwritten by another thread synchronized execCtx sql connCtx execCtx UPDATE SQLWarning warning execCtx getWarnings CORRECT operation and access to execution context is guarded by same block SQLWarning warning null synchronized execCtx sql connCtx execCtx UPDATE warning execCtx getWarnings Tip To avoid these problems altogether we recommend that each thread should use
165. MERIC name getCol3 marker index 1 EntryInfo custom data omitted DB2 ProfileData custom data 2 Package Name HELLO Package Version null Package Collection SG246435 Is Default Collection false Package Consistency Token hex format 4341616a52434770 Package Consistency Token character format CAajRCGp Notes on Example 9 1 on page 156 1 This profile has been customized 2 DB2 specific customization information such as the collection package name and consistency token 9 6 Preparing an application to use static SQL Until now we ran our Hello application with uncustomized serialized profiles meaning that the DB2 calls were executed as dynamic SQL As explained in More about profiles on page 153 this is very convenient during program development since you do not have to care about binding your program against the database In this section we customize the serialized profile generated for the program and verify that the program now uses static SQL to access the database We first show how to do this using WSAD but also how to do it the hard way using manual commands 9 6 1 Preparing SQLJ programs to use static SQL through WSAD Unlike preprocessing SQLJ translation which happens immediately whenever you save a changed SQLJ source file in the WSAD editor the customization process is triggered from an Ant script which you have to invoke explicitly The g
166. OM DSN8710 EMP job job trim DB2 for z OS and OS 390 Ready for Java However it is more efficient and easier to use the DB2 TRIM function since no intermediate String object has to be created sql SELECT TRIM JOB INTO job FROM DSN8710 EMP 13 1 6 Release resources Another thing that developers sometimes forget to do is to close and release resources when they are no longer being used The JDBC driver maintains its own links to resources and the resources are only released when the resources are closed or when the connection is closed For this reason gt Close ResultSets when the application is done with them If you do not do this JVM garbage collection cannot reclaim the objects and eventually the application may run out of JDBC resources or even worse run out of memory gt Close PreparedStatements that have ResultSets as soon as they are done being used Closing the ResultSet is not enough to release the underlying cursor resource If you do not close the PreparedStatement the cursor resource is tied up for the life of the PreparedStatement gt Close CallableStatements when you are done with them too or else the application may run out of call sections gt Be sure to release resources even in the case of failure The Java try finally construct is well suited to achieve this see Example 14 3 on page 242 SQLJ makes things a bit easier for developers than JDBC because the SQLJ translator
167. PU consumption by the various applications locking issues such as deadlock or timeout etc 3 5 7 SQLJ Tooling 48 WebSphere Studio Application Developer WSAD V5 1 has full support for SQLJ including native editor support debugger support integrated EJB CMP support and integrated SQLJ translation WSAD also supports profile customization and binding to generate static SQL for maximum performance The usage of the SQLJ editor is shown in Chapter 8 Getting started DB2 for z OS and OS 390 Ready for Java with SQLJ on page 141 We also demonstrat the use of the new SQLJ customization support in WSAD in Preparing SQLJ programs to use static SQL through WSAD on page 157 3 5 8 Use JDBC for flexible SQL statements If there is a requirement for ad hoc SQL statements then JDBC is usually a better if not the only option For example if your program is in response to a Web page that allows a user the option of selecting many different search parameters the resulting SQL will have to be versatile enough to handle any of these combinations In this situation it is better to build the SQL query at runtime and run it using JDBC The SQLJ alternative would be to either build an SQL statement for each possible combination or write a query that has slots for all possible fields and then use 1 ike match string values and number ranges for numeric fields Both these solutions are rather dire and should be avoided A better programmatic sol
168. Parameters Reverse Screen Image User Feature Lock IV Auto Wrap Auto Answer Back Message User Defined Key Lock Transparent Mode Answer Back Message Conceal VIT ID VT340 ID X Advanced v History Logging Size of History Log 64 KB cancel He Figure 5 12 Session parameters changed We chose to use VTANSI for machine mode and checked Autowrap Chapter 5 Setup 69 Click OK exit the panels and then save the configuration using the File toolbar and select Save As Give the session a name You are also prompted whether or not to create an icon on the desktop so you can easily log onto USS 5 3 2 Setting up the JDBC SQLJ environment variables 70 In order for your Java environment to work properly under USS you need to make sure that you have set up your environment variables correctly The STEPLIB environment variable on Unix System Services The data sets SDSNEXIT SDSNLOAD and SDSNLOD2 must be included in the STEPLIB environment variable The form of this environment variable is different than the norm in that it names these data sets using the MVS file system naming convention For example the name used could be DB7Y7 SDSNLOAD where DB7Y7 is the high level qualifier for the DB2 libraries for example DB7Y7 SDSNEXIT DB7Y7 SDSNLOAD DB7Y7 SDSNLOD2 This environment variable does not need to be set if these data sets are present in the linklist The PATH environment variable This dir
169. PreparedStatement 22df8fa9 executeQuery returned ResultSet 4b7d0fa9 Thread main ResultSet 4b7d0fa9 getMetaData called Thread main ResultSet 4b7d0fa9 getMetaData returned com ibm db2 jcc c j 4elecfa9 Thread main PreparedStatement 22df8fa9 getWarnings returned null Thread main ResultSet 4b7d0fa9 next called Thread main ResultSet 4b7d0fa9 next returned true Thread main ResultSet 4b7d0fa9 getString 1 called Thread main ResultSet 4b7d0fa9 getString returned GEYER Thread main ResultSet 4b7d0fa9 getString 2 called Thread main ResultSet 4b7d0fa9 getString returned JOHN Thread main ResultSet 4b7d0fa9 getBigDecimal 3 called Thread main ResultSet 4b7d0fa9 getBigDecimal returned 40175 00 Thread main ResultSet 4b7d0fa9 next called Thread main ResultSet 4b7d0fa9 next returned true rest of result set omitted Thread main ResultSet 4b7d0fa9 next called Thread main ResultSet 4b7d0fa9 next returned false Chapter 14 Error handling and diagnosis 245 Thread main Connection 48f70faa commit called Thread main Connection 48f70faa close called Usually TRACE DRDA FLOWS should be turned off since it generates a lot of output For example to enable all trace levels except TRACE DRDA FLOWS you could simply code TRACE DRDA FLOWS is the bitwise NOT operator in Java 14 5 2 Turning on tracing using connection properties As we said before t
170. QL operations using this context may take to complete 10 8 Interoperability between JDBC and SQLJ JDBC and SQLJ are by no means mutually exclusive In fact SQLJ is built on top of JDBC For example when you use uncustomized serialized profiles the SQLJ runtime uses JDBC functionality to prepare and execute the SQL statements in the profile dynamically see More about profiles on page 153 Not only does the SQLJ implementation use JDBC under the covers it is also perfectly possible to mix SQLJ and JDBC in the same program In fact you may have to because SQLJ has no syntax for embedded dynamic SQL so if your SQLJ program needs to execute dynamic SQL you must code those as JDBC calls SQLJ provides JDBC interoperability by providing constructs to convert a JDBC result set into an SQLJ iterator and vice versa This makes it possible for example to construct and execute a query dynamically using string operations and JDBC calls and evaluate the query s result set using SQLJ syntax 10 8 1 Converting a JDBC result set into an SQLJ iterator To convert a JDBC result set into an SQLJ iterator you use the CAST construct also known as the iterator conversion statement sql iter CAST resultset Where iter is an instance of a public SQLJ iterator class and resultset is a JDBC ResultSet object Obviously the number columns in the result set must match the number of columns in the iterator declaration and the columns must ha
171. QLJ runtime has to read the entire LOB in one big chunk Obviously this can cause problems when working with very large LOBs such as video clips For this reason Java offers two interfaces to work with LOB data namely java sql Blob for BLOB data and java sql Clob for CLOB data Both offer similar functionality and differ only in that Blob treats the data as binary whereas Clob treats it as text java sql Blob The java sql Blob interface offers the following methods gt long length throws SQLException Returns the number of bytes in the BLOB gt byte getBytes long pos int length throws SQLException Reads up to length bytes from the BLOB starting at position pos gt java io InputStream getBinaryStream throws SQLException Retrieves the BLOB as a stream gt long position byte pattern long start throws SQLException long position Blob pattern long start throws SQLException Searches the BLOB for a byte pattern java sql Clob The java sql Clob interface offers the following methods gt long length throws SQLException Returns the number of characters in the CLOB gt String getSubString long pos int length throws SQLException Get a part of the CLOB value starting at position pos and up to length characters long gt java io Reader getCharacterStream throws SQLException Retrieves the CLOB as a Unicode stream gt java io InputStream getAsciiStream throws SQLException Retrieves the CLOB as a stream of ASCII bytes
172. QLState method rather than the getErrorCode method for error handling The SQLSTATE has another advantage Whereas the SQLCODE values are somewhat randomly scattered the SQL state is designed so that applications can easily check for a category of errors The first two characters of the five character string denote the error category and the remaining three characters denote a specific error within that category Appendix A SQLSTATE categories on page 291 summarizes the SQL state categories 14 3 Cleaning up resources Even in the case of an exception a well behaved program should take care to free all acquired resources In Example 14 2 an iterator is opened and used to iterate over the result set from a query When the entire result set has been processed the iterator is closed Now suppose an SQLException or any other exception for that matter is thrown in the loop The call to iter close will never be performed and the iterator will remain open until the current transaction ends This has two negative impacts Depending on the execution environment there is a limited number of iterators that can be open at the same time Suppose the foo method in Example 14 2 is called from a loop and foo always throws an exception in its loop body you will find the program running out of available iterators very soon gt Unless your application runs with uncommitted read isolation the row or page the iterator is currently p
173. R files that contain resource files or Java classes that a program can load dynamically at run time Class variable Variables that apply to the class as a whole rather than its instances also called a static field CLASSPATH In your deployment environment the environment variable keyword that specifies the directories and Jar files in which to look for class and resource files CLI See call level interface CLOB See Character large object 318 DB2 for z OS and OS 390 Ready for Java Codebase An attribute of the lt APPLET gt tag that provides the relative path name for the classes Use this attribute when your class files reside in a different directory than your HTML files Column function An SQL operation that derives its result from a collection of values across one or more rows Contrast with scalar function Commit The operation that ends a unit of work by releasing locks so that the database changes made by that unit of work can be perceived by other processes Common Connector Framework In the Enterprise Access Builder interface and class definitions that provide a consistent means of interacting with enterprise resources for example CICS and Encina transactions from any Java execution environment Connection handle The data object that contains information associated with a connection managed by DB2 CLI This includes general status information transaction status and diagnostic information Cursor A named
174. RIVER PATH gt Save Security E Environment Save your workspace changes to the master configuration Update Web Server Plugin Virtual Hosts Manage WebSphere Variable Save to Master Configuration Shared Libraries Click the Save button to update the master repository with your changes Click the Discard button to discard your Naming changes and begin work again using the master repository configuration Click the Cancel button to continue working System Administration COMET ennes Si Troubleshooting Total changed documents View items with changes Discard Cancel Figure 5 28 Save into the Master Configuration DB2 for z OS and OS 390 Ready for Java DB2JCCPROPERTIES variable Now do the same thing again and define an additional variable to indicate where the JCC driver properties are stored Variable DB2JCCPROPERTIES Value DB2JCC DRIVER PATHy db2jcct2 properties You must specify the full path name for the properties field Note that we use the variable that we previously defined The file name itself must be db2jcct2 properties Attention As mentioned before we use a beta version of the JCC driver on z OS It is very likely that there will be changes in the way you specify properties for the JCC driver in the GA version of the code Please refer to the appropriate GA documentation for details Setting up a JDBC provider Now we are ready to set up our JDBC provider de
175. RSTNAME IS NOT A COLUMN IDENTIFIED IN A FROM CLAUSE The column name is misspelled in this example it should have been be FIRSTNME The full text message is only returned because the retrieveMessagesFromServerOnGetMessage property is enabled on the URL Otherwise you will see the following error information DB2 SQL error SQLCODE 206 SQLSTATE 42703 SQLERRMC FIRSTNAME ILLEGAL SYMBOL There is a syntax error in the SQL statement for example a misspelled statement name The version of the IBM Universal JDBC driver in use is not licensed for connectivity to z OS databases To connect to this DB2 server please obtain a licensed copy of the IBM DB2 Universal Driver for JDBC and SQLJ The required license file is not on the runtime classpath Make sure that you included db2jcc license cisuz jar on the classpath 6 3 Running the Hello application from Unix System Services Until now we have not been running the application on the mainframe but on the development workstation In this section we deploy the code to the Unix System Services USS environment on z OS and run it directly on the mainframe If your OS 390 or z OS installation happens to run an SMB server such as DFS SMB or Samba http www samba org you are in luck This enables you to access the HFS file system as a shared network drive Otherwise you can use FTP to transfer your code to HFS We describe both methods in Exporting to a shared file system on page
176. RT UPDATE PreparedStatement setString PreparedStatement setCharacterStream PreparedStatement setClob T 7 Scrollable cursors Scrollable cursors a feature introduced in JDBC 2 0 allow you to move through a result set backward as well as forward and to position the cursor to an absolute or relative position within the result set To request a scrollable cursor use the three argument form of Connection prepareStatement PreparedStatement prepareStatement String sql int resultSetType int resultSetConcurrency Where gt resultSetType is one of TYPE FORWARD ONLY TYPE SCROLL INSENSITIVE or TYPE SCROLL SENSITIVE gt resultSetConcurrency is one of ResultSet CONCUR READ ONLY or ResultSet CONCUR UPDATABLE as explained in Positioned UPDATE and DELETE on page 127 All of the constants are defined in class ResultSet The resultSetType argument determines if the result set is scrollable and if so if it is aware of changes to the underlying table TYPE FORWARD ONLY results in a non scrollable result set which is the default if you use the one argument or two argument form of prepareStatement TYPE SCROLL INSENSITIVE results in a scrollable result set which is not aware of changes to the underlying result table TYPE SCROLL SENSITIVE results in a scrollable result set which is aware of changes to the underlying result table Chapter 7 JDBC revisited 129 Note Be careful to get the parameter order right
177. S 390 platform An important issue to note is that as the support for data source objects normally comes from the application server such as WebSphere For an example on how to set up a data source see Setting up a data source in WebSphere Studio on page 258 The program that creates and manages a DataSource object also uses the Java Naming and Directory Interface JNDI to assign a logical name to the DataSource object The JDBC application that uses the DataSource object can then refer to the object by its logical name and does not need any information about the underlying datasource see next section for details In addition your system administrator can modify the data source attributes and you do not need to change your application program To learn more about using WebSphere to deploy DataSource objects go to http www ibm com software webservers appserv Connecting with a DataSource object As mentioned before the advantage of using a DataSource is that you avoid having to hard code drivers and URLs in the program and that those values are stored in the data source on the server When connecting using a DataSource the first thing you must code in your program are the import statements for the class that support data source objects These are import javax naming Context import javax naming InitialContext import javax sql DataSource The first two import statements define the classes that support retrieving the data
178. S and OS 390 Ready for Java 6 2 2 Setting up the classpath To include the JDBC driver code in the runtime classpath of our launch configuration perform the following steps 1 Select Run Run or select the drop down beside the Running Man icon and select Run from there The Launch Configurations dialog opens This dialog lists all available launch configurations right now there is only one of them and allows you to create new configurations or modify existing ones Make sure that the launch configuration for the Hello program is selected then switch to the Classpath tab In the bottom half of the dialog the Use default class path box is checked which indicates that the runtime classpath is the same as the compile time classpath Since we want to add a library that does not need to be on the compile time classpath uncheck the box Now the buttons that allow you to modify the classpath will be enabled see Figure 6 4 Create manage and run configurations X Configurations Name flo 3R8 Java Applet FJ Java Application 3J Hello main 69 Arguments W IRE T Classpath E Source Common 4 Java Bean Ju JUnit U d ser classes FS Run time Workbench Bootstrap classes AK Server 18 5624 6435 ih WebSphere v4 Applicatic Rif WebSphere v5 Applicatic Add JARs Add External JARs Advanced I Use default class path New Delete Apply Revert oe Figure
179. SYSDA SPACE 4000 20 20 ROUND SYSPRINT DD SYSOUT SYSUDUMP DD SYSOUT Note that the APPLENV parameter in the procedure is the default WMLENV The parameter is overwritten at address space startup time with DB7YJCC as specified in the WLM application environment startup parameter Chapter 5 Setup 67 5 3 Unix System Services There are many different ways to run a Java program under Unix System Services USS on a z OS or OS 390 system You can run it as a batch job from a command line under the control of WAS etcetera In this section we show how to set up your USS environment to be able to run a sample Java program from the command prompt in USS 5 3 1 Setting up a USS session 68 To illustrate how to run our sample program under a USS command prompt we first have to establish a Unix session Here we chose to use a Telnet session to log onto USS using IBM Personal Communications PCOM If you use another tool the setup may look different from ours shown in Figure 5 10 Select Connection to Host Type of Host X Interface LAN X Attachment VT over Tenet e Link Parameters Session Parameters m Connection Overview Interface Attachment Type of Host SS TCPAP LAN VT over Telnet 390 This connection provides access to an IBM System 390 host over an Intranet or Internet network using a YT terminal command set protocol converter is required between your PC and the host
180. Specification level of 1 2 Applications developed For this J2EE level typically target a WAS version 5 0 server Back 1 Finish Cancel Figure 15 1 Creating a Web project step 1 WSAD now asks if you want to change to the Web perspective Click OK In the J2EE Navigator view you will see three projects listed Our original SG24 6435 project containing the Employee program the SG24 6435 Web project and a project called DefaultEAR which was automatically created by WSAD After expanding the SG24 6435 folder and its subfolders your screen should look similar to Figure 15 2 on page 251 Note In WSAD a Web project has to be associated with an EAR project which in turn groups several individual Web projects also called modules in this context into a larger application For the sake of this example we do not need the EAR functionality which is why we let WSAD create a dummy EAR project for us Notice that Web projects are organized in a slightly different way as opposed to Java projects In Java projects the compiled class files by default go into the same directories as their corresponding source files whereas in Web projects the source and binary files are kept separate the JavaSource and WebContent WEB INF classes folders respectively This reflects the packaging structure of Web applications Basically everything under the WebContent folder is packaged together for deployment DB2 for z OS and OS 390 Ready f
181. Symbols sql 176 ser file 42 147 151 172 194 2 106 Numerics 2 phase commit 55 A access path 150 accessor 201 ad hoc SQL 49 Ant 157 166 APAR PQ19814 26 PQ36011 26 PQ62695 54 PQ72453 54 API 5 24 app driver 27 applet methods destroy 17 init 16 start 16 stop 16 applet signing 16 applets 16 Application Environment 63 AR 34 ASCII 115 ASCII to EBCDIC conversion 147 ASE 32 asynchronous messaging 20 authorization control 46 auto commit 234 Auto generated keys 231 B batch updates 195 225 Bean Managed Persistence 20 21 BIND utility 150 BLOB 215 BMP 20 21 BMP methods ejbCreate 21 ejbLoad 21 ejbRemove 21 ejbStore 21 BPXBATCH 16 147 breakpoint 118 bytecode 14 Copyright IBM Corp 2003 All rights reserved C C 184 C 4 12 13 CACHEDYN 238 call level API 44 cancel 195 CAST construct 198 statement 198 catch methods 109 CGI 5 CICS 7 119 Class forName 37 105 classes 12 CLASSPATH 28 38 71 73 classpath 111 cleaning up resources 108 241 CILOB 215 close 109 Cloudscape Network Server 28 CMP 20 21 COBOL 150 184 collection 158 com ibm db2 jcc DB2ConnectionPoolDataSource 89 com ibm db2 jcc DB2Driver 27 COM ibm db2 jdbc app DB2Driver 27 COM ibm db2 jdbc net DB2Driver 27 COM ibm db20s390 sglj jdbc DB2SQLJDriver 26 27 command line arguments 109 command prompt 117 Common Gateway Interface 5 component transaction monitor 19 CONCUR READ ONLY 127 CONCUR UPDATABLE
182. T F 2 4 1 objects Disk free space 9 64 GB 2 08 KB E My Computer Figure 9 12 Directory structure after the export Now we are ready to go We show all individual steps that you have to do to get the program to run with static SQL Translate the program sqlj The first step is to translate the SQLJ into a Java program using the SQLJ translator using the following command C TestSQLJ SG24 6435 gt sqlj compile false com ibm itso sg246435 sq1j Hello sql j Make sure you are in the correct directory to do so You could also have gone to the C TestSQLJ SG24 6435 com ibmi itso sg246435 sqlj directory and issue C TestSQLJ SG24 6435 com ibm itso sg246435 sqlj gt sqlj compile false Hello sqlj Notice that we use the compile false option This prevents the Java compiler from being invoked by default We only do this here to illustrate that the result of the sqlj translation are the following files Hello java Hello SJProfile0 ser Out of curiosity let us have a look at the information that is stored in the serialized profile and use the db2sgqljprint utility to print the contents of the Hello SJProfileO ser file Example 9 5 Example 9 5 Uncustomized serialized profile printing contents of profile com ibm itso sg246435 sqlj Hello SJProfileO Chapter 9 The SQLJ program preparation process 167 168 created 1061630428017 2003 08 23 associated context is sqlj runtime ref DefaultContext profi
183. T 2003 zl An outline is not available e 8 g Servers db options COLLECTION 36G246435 B eri 5624 6435 db url jdbec db2 wtsc63 itso ibm com 33756 DB7Y hs Bil com ibm itso sg246435 jdbc A E J Hello java H Hello E1 Bil com ibm itso sg246435 sqlj J Hello java 3j Hello sqlj Hello SJProfileQ ser Edi Th sali zip C XSQLLIBljava erties opi wh JRE System Library eclipse Console lt terminated gt C wsad51 workspace 5G24 6435 SQLJAntScripts sqlj customize xml E Package Explorer Hierarchy Tasks Console 2 items selected Figure 9 4 Sqlj project properties 160 Important If you specified values for the User and Password fields in the previous step these values would have been stored in the properties file in plain text Obviously storing passwords in plain text is not a good idea especially when you are sharing the properties file in a CVS repository Next right click the project again and select Generate SQLJ Customization Script WSAD now generates the build script a file called sqlj customize xml It also modifies the properties file generated in the previous step adding one property for each SQLJ source file that stores the root package name for that SQLJ source file Now open the Project Properties dialog again right click the project and select Properties T
184. TNME MIDINIT EMPNO FIRSTNME MIDINIT LASTNAME HIREDATE SALARY LASTNAME HIREDATE SALARY VALUES CURRENT DATE VALUES ys empno firstname midinit stmt setString 1 empno lastname CURRENT DATE salary stmt setString 2 firstname stmt setString 3 midinit stmt setString 4 lastname stmt setBigDecimal 5 salary stmt close Also note that people with a static embedded SQL programming background in for example COBOL will find it very easy to start using SQLJ as iterators and SELECT INTO constructs look very much like those in embedded SQL 3 5 2 SQLJ catches errors sooner 44 Not only is SQLJ typically more concise and easier to read than JDBC it also helps you to detect errors in your SQL statements earlier in the program development process JDBC is a pure call level API This means that the Java compiler does not know anything about SQL statements at all they only appear as arguments to method calls If one of your statements is in error you will not catch that error until runtime when the database complains about it SQLJ on the other hand is not an API but a language extension This means that the SQLJ tooling is aware of SQL statements in your program and checks them for correct syntax and authorization during the program development process It also enforces strong typing between iterator columns and host variables In other words it prevents you for examp
185. To retrieve the result set of a SELECT statement you assign that result set to an instance of a previously declared iterator class You then iterate over the result set fetching one row at a time until all result set rows have been retrieved Exactly how you iterate over the result set and how you retrieve the values depends on whether you are using positioned iterators or named iterators We explain both variants in Using positioned iterators on page 182 and Using named iterators on page 183 respectively Normally iterators are automatically closed when the current transaction is committed When you need an iterator to remain open across transaction boundaries you have to declare them holdable Holdable iterators on page 186 shows how to do this You can also update or delete the row an iterator is currently positioned on This is explained in Positioned UPDATE and DELETE on page 186 10 5 1 Using positioned iterators To use a positioned iterator assign it the result from a SELECT statement Example 10 2 shows the technique Example 10 2 Assigning the result set of a query to an iterator fsql iterator MyPositionedIterator String int Timestamp MyPositionedIterator iter sql ctx iter SELECT NAME EDLEVEL DATEHIRED FROM DSN8710 EMP WHERE h The number of the columns in the iterator declaration must match the number of columns in the select list otherwise the SQLJ translator reports an error Howe
186. a directory structure matching the java package names is created as well as the naming structure used in WSAD to store the projects The folder should have the followning content noimage gif File that gets loaded when no picture exists in the database Copyright IBM Corp 2003 All rights reserved 313 Hello java Our first JDBC program Hello sqlj Our first SQLJ program Employee sqlj Our Employee class EmployeeTest java Employee class test program EmployeeListServlet java Servlet listing all employees EmployeeDetailServlet java Servlet lising some details of a certain employee EmployeePicServlet java Servlet retrieving an employee s picture EmployeeList jsp JSP listing all employees EmployeeDetail jsp JSP providing details about an individual employee Spufi java Poor man s SPUFI Getopt java A class for parsing command line arguments passed to programs 314 DB2 for z OS and OS 390 Ready for Java Abbreviations and acronyms AIX APAR APAR AR ARM AS ASCII BLOB CA CCA CCSID CD CDRA CEC CF CFCC CFRM CGI CICS CLI CLP CPU CSA CTT DASD DB2 Connect EE DB2 Connect PE DB2 PM DB2 UDB DB2RA DBAT DBD DBID DBMS DBRM DCL Advanced Interactive eXecutive from IBM Authorized Program Analysis Report Authorized program analysis report Application Requester Automatic restart manager Application Server American National Standard Code for Information Interchange Binary large ob
187. a element that specifies a value that can be changed A COBOL elementary data item is an example of a variable Contrast with constant Virtual machine A software or hardware implementation of a central processing unit CPU that manages the resources of a machine and can run compiled code See Java Virtual Machine Web See World Wide Web Web browser The Web uses a client server processing model The Web browser is the client component Examples of Web browsers include Mosaic Netscape Navigator and Microsoft Internet Explorer The Web browser is responsible for formatting and displaying information interacting with the user and invoking external functions such as Telnet or external viewers for data types that it does not directly support Web browsers are fast becoming the universal client for the GUI workstation environment in much the same way that the ability to emulate popular terminals such as the DEC VT100 or IBM 3270 allows connectivity and access to character based applications on a wide variety of computers Web browsers are available for all popular GUI workstation platforms and are inexpensive often included with operating systems or related products for no additional charge Web server Web servers are responsible for servicing requests for information from Web browsers The information can be a file retrieved from the server s local disk or generated by a program called by the server to perform a specific application
188. ables pane highlight the environment variable that you want to edit and click Edit System Variable PATH Make sure to include a supported Java SDK in your PATH environment variable for example C SQLLIB java jdk bin if you want to run Java programs from the command line The DB2 installation process does not do this If you want to use SQLJ and need to invoke the SQLJ translator SQLJ exe make sure that C SQLLIB bin is in the PATH as well Normally this should have been taken care of by the DB2 installation process DB2 for z OS and OS 390 Ready for Java CLASSPATH Make sure the following jar and zip files are included in the CLASSPATH The current directory The file C sqllib java db2jcc jar The file CAsgllibjavaNdb2jcc license cisuz jar license file to be able to connect to a DB2 for z OS and OS 390 using the Type 4 driver To build SQLJ programs the CLASSPATH should also include the file C sqllib java sqlj zip TCP IP listening port To build applications that access DB2 UDB for LUW with the JDBC Universal Type 2 or JDBC Universal Type 4 Driver or to build applets with the JDBC Universal Type 4 Driver the TCP IP listener must be running To ensure this do the following from a DB2 command window 1 Set the environment variable DB2COMM to TCPIP as follows db2set DB2COMM TCPIP 2 Update the database manager configuration file with the TCP IP service name as specified in the services file The services
189. ach parameter marker before executing the statement The parameter markers are set from within the code using the setXxx methods of the PreparedStatement object These methods have two parameters gt The first is the number of the parameter marker that you want to set gt The second parameter is the value that the parameter marker is to be set to So the PreparedStatement class has several methods of the form setType int paramIndex Type paramValue Where Type is a Java data type matching the DB2 column type For example to set the first parameter marker we use stmt setBigDecimal 1 new BigDecimal 30000 6 1 5 Executing the statement Now that the statement has been prepared and the variable parts have been populated with values we are ready to execute the statement Depending on the type of statement you use either the executeQuery method or the executeUpdate method of the PreparedStatement class The former is used with queries that is with SELECT statements while the latter is used with all other types of statement for example INSERT UPDATE DELETE and statements like CREATE TABLE which are known as DDL statements 6 1 6 Processing the result set 106 The executeQuery method returns a ResultSet object which is used to iterate over the rows returned from a query Typically you process a ResultSet in a loop while rs next Process current row The next method is used to advance the under
190. add a finishing touch to our program wouldn t it be nice if we could retrieve and create an employee s picture We do that in this section introducing the data types and methods for working with LOB Large OBject data LOBs come in two flavors gt Character Large OBjects CLOBs for holding character data such as XML documents gt Binary Large OBjects BLOBs for holding binary data such as multimedia files Working with LOB data is almost as easy in Java as working with simple data types as you will see in the following examples Retrieving employee photos Example 11 20 shows the first version of the getPicture method The SELECT statement looks exactly the same as with non LOB columns The LOB data is returned as a byte array which represents the employee s picture in GIF format Example 11 20 Employee getPicture first version Retrieves the employee s picture Qreturn An InputStream to read the picture in GIF format or code null code if no picture is available exception SQLException A database error occurred DB2 for z OS and OS 390 Ready for Java public byte getPicture Ctx ctx throws SQLException byte picture sql ctx SELECT BMP_PHOTO INTO picture FROM DSN8710 EMP_PHOTO_RESUME WHERE EMPNO empNo h return picture However there is one problem with the approach in Example 11 20 on page 214 When you retrieve the data into a byte array the S
191. ager getConnection url user password Prepare the SELECT statement stmt conn prepareStatement SELECT LASTNAME FIRSTNME SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN AND Set parameters for the SELECT statement stmt setBigDecimal 1 new BigDecimal 30000 stmt setBigDecimal 2 new BigDecimal 50000 Execute the query to retrieve a ResultSet rs stmt executeQuery Iterate over the ResultSet while rs next String lastname rs getString 1 LASTNAME String firstname rs getString 2 FIRSTNME BigDecimal salary rs getBigDecimal 3 SALARY Chapter 6 Getting started with JDBC 103 System out println lastname firstname salary Ww catch SQLException e Print exceptions to the console System err printIn e getMessage catch Exception e System err printIn e finally Clean up try if rs null rs close if stmt null stmt close if conn null conn close catch SQLException ignored ae Notes on Example 6 1 on page 103 we cover these items in more detail in the following sections 1 The retrieveMessagesFromServerOnGetMessage property when enabled directs all calls to the standard JDBC SQLException getMessage to invoke the server side stored procedure SQLCAMESSAGE which retrieves the readable message text for the error This property is disabled by default You can also us
192. ags from the database access tag library It declares a data source to be used for subsequent operations which can then refer to that data source using the name given in the ID attribute The dataSource attribute gives the JNDI name of the data source Executes an SQL SELECT statement using the data source declared in step 3 4 5 This causes the enclosed content to be generated several times once for each row 6 Access the first 7 and second column of the current row embedding them in a lt td gt element Of particular interest is the sq1 repeat tag for iterating over a result set Essentially this is a control structure a while loop masquerading as a JSP tag As described above it allows you to iterate over the result set without explicit Java coding For a full description and reference of the Database Access custom tag library refer to the WSAD documentation Compared to the Servlet version see Creating the EmployeeList Servlet on page 251 the JSP version is definitely easier to understand and to maintain Rather than burying HTML code in Java print statements the JSP version clearly shows the structure of the resulting output document All the database query and result set iteration is done for you by the custom tag library so there is no explicit coding at all apart from the SELECT statement of course 16 3 Creating the EmployeeDetail JSP The EmployeeDetail JSP page should be relatively straightforwar
193. al Tools Builders Available Web Project features Description Java Build Path Java Compiler Javadoc Location Java JAR Dependencies Java Task Tags JSP Fragment Links Validation Refactoring Project References Server Preference Web Project Features You will be able to create html and jsp Files with consistent look and feel by using Page Templates a age Template suppor 7 Add Page Templat t O Add Struts support Default style sheet C55 file efault synchronization policy For rep Default hronizati licy For CYS 7 Enable for Web Site Designer E Enable for web site Desi ED 35P Tag Libraries 1 ISP standard Tag Library O Tag Libraries For accessing JSP object Tag Libraries For database access Struts O Tag Libraries For internationalization Validation E utility Tag Libraries Web Web Content Settings Web Library Projects Web Project Features T4 Restore Defaults Apply EN Figure 16 1 Including the database access JSP tag libraries What happens is that WSAD copies the Jar files implementing the custom tags to your WebContent WEB INF lib directory ensuring that these Jar files are included with your application when you export it to a deployable format that is a war or ear file Next we create the JSP skeleton by selecting File gt New gt Other gt Web gt JSP file Click Next The the New JSP File dialog appears see Figure 16 2 on page 282 Chapter 16 JavaServer
194. alled the Servlet container The container manages the running of the Servlet instances and provides the threads when a request is made to execute a Servlet The beauty of this architecture is that all the business logic is coded and executed on the server itself All that is required to exist on the client machine is a browser and the mechanism to communicate with the server Another feature of this architecture is that all connections to resources required to process the Servlet only need to be present and available on the server platform itself If a process requires access to back end databases this is managed and executed from the server not the client so all database access software is present in one place and does not need to be replicated to each client To code a Servlet you extend one of two Servlet classes either the GenericServlet which is used to define a protocol independent Servlet and the more often used HttpServlet class which uses the HTTP protocol for use on the Web The significant methods of an HTTP Servlet are shown in Table 2 2 Table 2 2 Methods of a HTTP Servlet LI IMEEM C MENEENNEEEEN Performs the initialization of the Servlet when is it started This can occur when a Web server is started or on the first request for the Servlet if it has not already been started service HttpServletRequest req This method processes the request sent to the HttpServletResponse resp Servlet Usually it returns the output
195. alls against a DB2 database on z OS via dynamic SQL through JDBC running uncustomized In the next phase when the program is more or less ready the serialized profile can be customized from the workstation through WSAD if you want and further testing can be done from the workstation against the DB2 for z OS system using static SQL this time 3 When the program is ready to be taken into production you only have to copy for example using FTP the class files and the customized serialized profiles in binary from your workstation over to the mainframe platform and for example deploy them in your WebSphere for z OS environment and run a BIND COPY operation to bind your packages from the test system into the production system and you are ready to go Note that the taking into production can easily be set up to be implemented through a software that normally handles your change and version control environment Chapter 9 The SQLJ program preparation process 173 174 DB2 for z OS and OS 390 Ready for Java 10 SQLJ tutorial and reference This chapter provides a tutorial and reference to SQLJ syntax and usage It is not meant to be exhaustive For a detailed description of the individual SQLJ constructs please refer to Application Programming Guide and Reference for Java SC26 9932 We discuss the following The basic syntax of SQLJ statements Host variables and expressions Null values Data type mapping Queries i
196. alog This process enables the program to use static SQL at runtime Chapter 9 The SQLJ program preparation process 151 4 When the program is run the SQLJ runtime reads the serialized profile and uses the information in the profile to execute the SQL statements in the program When the profiles have been customized true static SQL is used otherwise the runtime uses dynamic SQL sqlj file Optional but highly input to recommended SQLJ translator Produces sql j produces D input to java file ser files Profile updates customizer input to db2sql j cust oni ze i Java Compiler uses bind produces EN ue ee class files _k Runtime uses i Uses environment package DB2 for z OS Figure 9 2 The SQLJ program preparation process Overview Although there are a lot of similarities if you compare the SQLJ program preparation process with the process for other languages you will note some important differences gt First unlike DBRMs the serialized profiles are needed at runtime you will receive an SQLException if the runtime cannot find them gt Second the customization and bind steps are optional but recommended whereas in traditional programming languages they are required In the following sections we explain these differences in more detai
197. ame empdetail index 1 gt lt td gt lt tr gt lt tr gt lt td gt Name lt td gt Appendix B Source code of sample programs 311 td sql getColumn name empdetail index 2 gt lt td gt lt tr gt lt tr gt lt td gt Hired lt td gt td sql getColumn name empdetail index 3 gt lt td gt lt tr gt lt tr gt lt td gt Salary lt td gt td sql getColumn name empdetail index 4 gt lt td gt lt tr gt lt table gt lt body gt lt html gt 312 DB2 for z OS and OS 390 Ready for Java Additional material This redbook refers to additional material that can be downloaded from the Internet as described below Locating the Web material The Web material associated with this redbook is available in softcopy on the Internet from the IBM Redbooks Web server Point your Web browser to ftp www redbooks ibm com redbooks S6246435 Alternatively you can go to the IBM Redbooks Web site at ibm com redbooks Select the Additional materials and open the directory that corresponds with the redbook form number SG246435 Using the Web material The additional Web material that accompanies this redbook includes the following files File name Description SG246435 zip Zipped code samples How to use the Web material Create a subdirectory folder on your workstation and unzip the contents of the Web material zip file into this folder When unzipping the file
198. an Example 13 1 Result of bind with online checking enabled vs disabled SELECT PROGNAME ACCESSTYPE MATCHCOLS ACCESSCREATOR ACCESSNAME FROM PLAN TABLE WHERE PROGNAME IXTEST2 Eccle PROGNAME ACCESSTYPE MATCHCOLS ACCESSCREATOR ACCESSNAME ee IXTEST2 I 1 SYSIBM DSNDTXO1 1 IXTEST2 R 0 2 DSNE610I NUMBER OF ROWS DISPLAYED IS 2 Notes on Example 13 1 1 Result of bind with online checking enabled The package uses index SYSIBM DSNDTX01 2 Result of bind with online checking disabled The package does a tablespace scan ACCESSTYPE R Note that not only CHARACTER columns can be affected but also numeric columns If you use a host variable of type long to match a column of type INTEGER the optimizer will DB2 for z OS and OS 390 Ready for Java choose a non matching index scan because the predicate has to be evaluated at Stage 2 rather than at Stage 1 13 3 4 Check explain tables As discussed in the previous section it is a good idea to always bind with EXPLAIN YES and to check the PLAN TABLE for potential performance problems See Chapter 26 of the Application Programming and SQL Guide SC26 9933 for information about how to set up and interpret a PLAN TABLE Alternatively or additionally you can use Visual Explain which is a non priced feature of DB2 for OS 390 in other words it is free It l
199. an execution context of its own when sharing a common connection context However each connection context has its own default execution context Therefore when you use a different connection context in each thread there usually is no need to explicitly create execution contexts When you are using the same connection context in different threads and you want to execute SQL statements in parallel you can create additional execution contexts You then specify on each SQLJ statement in which context you want this particular statement to be executed Unlike connection contexts there is only one class for execution contexts no construct is available to declare user defined execution context classes To create a new execution context simply create a new instance of class sq1j runtime ExecutionContext and store the reference to that instance import sqlj runtime ExecutionContext ExecutionContext execCtx new ExecutionContext To specify an execution context to be used for a particular SQLJ statement use the following syntax sql connCtx execCtx SQL statement It is also possible to only specify an execution context in which case the default connection context will be used sql execCtx SQL statement Summary of ExecutionContext methods Hereafter we give a brief summary of the different ExecutionContext methods that you have at your disposal cancel public void cancel throws SQLException Tries to cancel th
200. and executed within an application program while the program is executing In dynamic SQL the SQL source is contained in host language variables rather than being coded into the application program The SQL statement can change several times during the application program s execution Dynamic Web content Programming elements such as JavaServer Pages Servlets and scripts that require client or server side processing for accurate run time rendering in a Web browser EBCDIC Extended binary coded decimal interchange code An encoding scheme used to represent character data in the MVS VM VSE and OS 400N environments Contrast with ASCII Embedded SQL SQL statements coded within an application program See static SQL Enclave In Language Environment for MVS amp VM an independent collection of routines one of which is designated as the main routine An enclave is similar to a program or run unit Enterprise Java Includes Enterprise JavaBeans as well as open API specifications for database connectivity naming and directory services CORBA IIOP interoperability pure Java distributed computing messaging services managing system and network resources and transaction services Enterprise JavaBeans A cross platform component architecture for the development and deployment of multi tier distributed scalable object oriented Java applications Exception An exception is an object that has caused some sort of new condition such
201. and OS 390 Ready for Java B 3 B 5 B 6 B 8 B 9 Employee sdlz sisssiecier ersieexrveekecr eb vee eed Lbenseecis v Ris 297 EmployeeTestjava 0 0 0 0 ccc ln 303 EmployeeListServiet 00 000 eee 305 EmployeeDetailServlet 0 000 ccc tees 307 EmployeePicServlet 0 0 0c cect 309 EmployeeList ae crei nm RD uc eae RR RO idR ea D 310 EmployeeDetail 1 nere emp Vg ede do YS m pee mee 311 Examples xvii xviii DB2 for z OS and OS 390 Ready for Java Notices This information was developed for products and services offered in the U S A IBM may not offer the products services or features discussed in this document in other countries Consult your local IBM representative for information on the products and services currently available in your area Any reference to an IBM product program or service is not intended to state or imply that only that IBM product program or service may be used Any functionally equivalent product program or service that does not infringe any IBM intellectual property right may be used instead However it is the user s responsibility to evaluate and verify the operation of any non IBM product program or service IBM may have patents or pending patent applications covering subject matter described in this document The furnishing of this document does not give you any license to these patents You can send license inquiries in writing to IBM Director of Lic
202. and OS 390 Version 7 and DB2 for z OS Version 8 This feature consists of a component known as the DB2 Universal Database Driver for z OS Java Edition This is a pure Java Type 4 JDBC driver designed to deliver high performance and scalable remote connectivity for Java based enterprise applications on z OS to a remote DB2 for z OS database server The driver gt Supports JDBC 2 0 and 3 0 specification and JDK V1 4 to deliver the maximum flexibility and performance required for enterprise applications gt Delivers robust connectivity to the latest DB2 for z OS and WebSphere Application Server for z OS gt Provides support for distributed transaction support two phase commit support gt Enables custom Java applications that do not require an application server to run in a remote partition and connect to DB2 z OS With IBM z OS Application Connectivity to DB2 for z OS and OS 390 shown in Figure 3 8 you no longer need the local DB2 DB2A on the same machine as your a WebSphere Application Server as in Figure 3 7 on page 34 This no charge feature of DB2 for z OS and DB2 for z OS and OS 390 Ready for Java OS 390 V7 and DB2 for z OS V8 provides a Type 4 JDBC driver that supports two phase commit This driver is sometimes unofficially called the Type 4 XA driver Your Java applications running inside the a WebSphere Application Server talk to the Universal Type 4 JDBC driver that supports two phase commit and the driver talks di
203. and the Runtimes for both WAS Version 4 and WAS Version 5 We install WSAD on the default directory c Program Files IBM WebSphere Studio To start WSAD select Start gt IBM WebSphere Studio gt Application Developer The first screen to pop up should look like Figure 5 13 on page 74 Chapter 5 Setup 73 WebSphere Studio Eg WebSphere Studio stores your work in a directory called a workspace You can change the workspace each time you start WebSphere Studio Specify the directory to use for this session C wsad workspace Browse Use this workspace as the default and do not show this dialog box again TN Figure 5 13 Tell WSAD where to put its workspace We direct WSAD to save our work in C wsad workspace The default is c Documents and Settings During some of the sample executions we need to specify the location of the workspace After the WSAD logo screen and being patient we get into the actual WSAD workbench with a Welcome to WebSphere Studio panel To make sure we installed WSAD correctly and did a good DB2 and JCC setup we test the setup by using some of the functions of the WSAD Data Perspective 5 5 1 Using the data perspective 74 Choose the Data Perspective Window gt Open Perspective gt Other gt Data A screen like Figure 5 14 on page 75 with the data perspective normally appears DB2 for z OS and OS 390 Ready for Java Data IBM WebSphere Studio Application Developer File Edi
204. anguage Glossary Access path The method that is selected by the optimizer for retrieving data from a specific table For example an access path can involve the use of an index a sequential scan or a combination of the two American National Standards Institute ANSI An organization consisting of producers consumers and general interest groups that establishes the procedures by which accredited organizations create and maintain voluntary industry standards in the United States ANSI American National Standards Institute API See Application Program Interface Applet See Java Applet Application 1 A program or set of programs that perform a task for example a payroll application 2 In Java programming a self contained stand alone Java program that includes a static main method It does not require an applet viewer Contrast with applet Application plan The control structure produced during the bind process and used by DB2 to process SQL statements encountered during statement execution Application program interface API A functional interface supplied by the operating system or by a separately orderable licensed program that allows an application program written in a high level language to use specific data or functions of the operating system or licensed program Application requester AR See requester AR Application requester See requester ASCII 1 American Standard Code for Information Interchange A
205. as expected try while iter endFetch sql FETCH iter INTO name edLevel dateHired h System out println name t edLevel t dateHired finally iter close Either the loop will not be entered at all since no FETCH has been performed initially endFetch may return true or the last result row will be printed twice a FETCH operation leaves the host variables unchanged when there is no more row to retrieve 10 5 2 Using named iterators While the declaration of a named iterator looks very similar to a positioned iterator the usage is quite different You do not use the FETCH statement to retrieve the next row and assign the column values automatically to host variables Rather you use the next method of the iterator class to retrieve the next row and you use generated accessor methods of the iterator class to retrieve the individual columns The next method returns false when there is no next row to retrieve Chapter 10 SQLJ tutorial and reference 183 Unlike with named iterators see Using positioned iterators on page 182 the next method can be used as the control expression of the while statement since it tries to fetch a row and returns false there were no more rows in the result set Example 10 5 Using a named iterator sql iterator MyNamedIterator String lastName int edLevel Timestamp hireDate MyNamedIterator iter sql ctx iter SELECT LASTNAME E
206. as we say not as we did This option controls whether DB2 builds a DESCRIBE SQLDA when binding static SQL statements YES means that DB2 does generate a DESCRIBE SQLDA at BIND time so that DESCRIBE requests for static SQL can be satisfied during execution Specifying YES increases the size of some packages because the DESCRIBE SQLDA is now stored with each statically bound SQL SELECT statement In addition if you use named iterators in your SQLJ programs and you do not use online checking DESCRIBE FOR STATIC must be set to YES This DSNZPARM value may be changed online by issuing the following command SET SYSPARM LOAD dsnzparm name DSNTIJMS This post install job creates objects required for the DB2 JDBC and ODBC metadata methods It is straightforward to customize and run As we want to show you how to set up a WLM application environment later on in this chapter see Workload Manager WLM on page 61 we change the WLM ENVIRONMENT parameter on the CREATE PROCEDURE STATEMENTS to DB7YJCC T4 Universal Driver setup When you are directing SQL requests to a DB2 for z OS and OS 390 from a T4 driver you need to have some packages bound on the DB2 for z OS system This can either be done when setting up a database connection DB2 Connect using the DB2 for LUW configuration wizard or by invoking the DB2Binder utility for example from a Windows command prompt Chapter 5 Setup 59 60 provided your Windows environment is set up p
207. asses sqjj zip i A list of paths or JAR file names which together form the location for the resource provider classes Classpath entries are separated by using the ENTER key and must not contain path separator characters such as or Classpaths may contain variable symbolic names which can be substituted using a variable map Check your drivers installation notes for specific JAR file names which are required Native Library Path S DB2JCC DRIVER PATH Mib i An optional path to any native libraries dll s so s Native path entries are separated by using the ENTER key and must not contain path separator characters such as or Native paths may contain variable symbolic names which can be substituted using a variable map Implementation Classname Eom ibm db2 joc DB2ConnectionPooll i The Java classname of the JDBC driver implementation Apply OK Reset Cancel Figure 5 33 User defined JDBC Provider variables 2 DB2 for z OS and OS 390 Ready for Java Defining the data source We are now ready to create a data source for the new DB2 JCC T2 z OS JDBC Provider we created in the previous section Using Resources JDBC Providers select DB2 Jcc T2 zOS JDBC Provider which is now listed as an available provider Click this provider to display it Figure 5 34 E WebSphere Ad
208. at we did not create a setter method for the employee number so the only way to assign it is via the constructor Example 11 3 Employee constructor public Employee String empNo this empNo empNo Insert the constructor somewhere in the class body The preferred Java convention is to put constructors right between the field declarations and the other methods After saving the file the error message about the missing initialization of empNo will have gone away since now there exists a constructor that initializes it 11 2 3 Implementing the insert method Now we are ready to code the individual methods for inserting new employees into the table for deleting employees and for updating employee data We begin with the insert method which creates a new employee The most common form of an INSERT statement looks like this INSERT INTO table_name column_list VALUES value_list Where gt lable nameis the name of the table or view into which you want to insert gt column list is a comma separated list of column names from that table Chapter 11 SQLJ revisited 203 gt value listis a comma separated list of values to be inserted into the respective columns The values supplied in the value ist in turn fall into two very different categories They can be gt SQL expressions that is expressions defined by the SQL language gt host variables that is references to a variable defined in the host languag
209. atabase Example 11 11 shows the source code Example 11 11 Employee delete method public void delete Ctx ctx throws SQLException sql ctx DELETE FROM DSN8710 EMP WHERE EMPNO empNo h In our TestEmployee class we add a call to delete to the main method We also verify that delete worked by trying to retrieve the employee record again This should raise an SQLException which we catch Example 11 12 shows the updated main method Example 11 12 EmployeeTest main after adding the call to delete public static void main String args try initialize testInsert DB2 for z OS and OS 390 Ready for Java Employee emp Employee findByPrimaryKey 000042 System out printIn Successfully retrieved emp emp delete ctx try emp Employee findByPrimaryKey ctx 000042 catch SQLException expected if expected getSQLState equals 02000 System out println Employee record deleted successfully else Different SQL exception we didn t really expect it throw expected catch SQLException e System err println e SQLCODE e getErrorCode catch Exception e System err printIn e 11 2 8 Implementing the update method The update method writes back any changes made to an Employee object In the WHERE clause of the UPDATE statement we specify that we want to update the single row identified by the table s primary key t
210. atch update facility is one of the features provided with the JDBC 2 0 API and is now supported with the DB2 Universal Driver for SQLJ and JDBC Chapter 12 The DB2 Universal Driver 225 Example 12 2 from the JDBC 3 0 specification demonstrates how to use batch updates Note that autocommit should be turned off when using batch updates Also each of the statements in the batch must be one that returns an update count for example a SELECT statement in the batch is not allowed and will cause an SQLException to be thrown Example 12 2 Creating and executing batched INSERT statements Turn off autocommit conn setAutoCommit false Statement stmt conn createStatement stmt addBatch INSERT INTO employees VALUES 1000 Joe Jones stmt addBatch INSERT INTO departments VALUES 260 Shoe stmt addBatch INSERT INTO emp dept VALUES 1000 260 Submit a batch of update commands for execution int updateCounts stmt executeBatch 12 3 3 Improved security for DB2 authentication Authentication security has been improved significantly with the DB2 Universal Driver for SQLJ and JDBC It supports the following authentication techniques gt User ID and password in plain text gt User ID and password encrypted Kerberos security For encrypted and Kerberos security the DB2 Universal Driver for SQLJ and JDBC uses the following Java services gt IBM Java Generic Security Service JGSS gt IBM Java Authen
211. ate serialized profile ser file for each set of statements in the program that executes on a different context class This allows subsequent customization and bind to occur against different locations for each set of statements Context instances that will connect to the same database should be instances of the same context class and context instances that will connect to different databases should be instances of different context classes 10 6 7 Summary of ConnectionContext methods 194 Hereafter we give a brief summary of the different ConnectionContext methods that you have at your disposal gt getConnection public java sql Connection getConnection Returns the JDBC connection object associated with this connection context gt close void close boolean flag throws SQLException Closes the connection context releasing all database resources it currently uses If flag ConnectionContext CLOSE CONNECTION it also closes the underlying JDBC connection If flag ConnectionContext KEEP CONNECTION the JDBC connection remains open DB2 for z OS and OS 390 Ready for Java public void close throws SQLException Closes the connection context and also closes the underlying JDBC connection Equivalent to close ConnectionContext CLOSE CONNECTION gt isClosed public boolean isClosed Returns true if this connection context is closed otherwise returns false gt getExecutionContext public sqlj runtime ExecutionC
212. ate two connection context instances and then start three threads one of them updates a row in the database the other two retrieve a column of that row One of those in turn uses the connection context that had also been used by the updating thread the other uses another context Example 10 9 Using different connection contexts package com ibm itso sg246435 sqlj import java sql import sqlj runtime ref import java math BigDecimal DB2 for z OS and OS 390 Ready for Java public class ConnectionContextTest private static final String url jdbc db2 wtsc63 itso ibm com 33756 DB7Y private static final String user bartr1 private static final String password secret private static final String empno 000130 sql static context Ctx 1 private static Ctx ctxl 2 private static Ctx ctx2 private static boolean updated false private static Object semaphore new Object public static void main String args try Class forName com ibm db2 jcc DB2Driver Set up connection contexts with autocommit disabled ctxl new Ctx url user password false 3 ctx2 new Ctx url user password false new Thread 4 public void run updateOnCtx1 public String toString return Thread 1 ctx1 start new Thread 5 public void run selectOnCtx1 public String toString return Thread 2 ctx1 start new Thread 6 public void run selec
213. ation and how to set up connectivity from the workstation to your DB2 subsystem on z OS Part 3 is the heart and soul of this publication It demonstrates how to develop DB2 Java applications on the workstation and how to deploy and run them on the mainframe We cover Copyright IBM Corp 2003 All rights reserved xxi stand alone applications using JDBC and SQLJ and provide a tutorial and reference to SQLJ syntax and usage Finally Part 4 talks about applications running in an application server environment using the Servlet and JSP technologies The team that wrote this redbook xxii This redbook was produced by a team of specialists from around the world working at the International Technical Support Organization San Jose Center Bart Steegmans is a DB2 Product Support Specialist from IBM Belgium currently on assignment at the ITSO in San Jose He has over 14 years of experience in DB2 Before joining IBM in 1997 Bart worked as a DB2 system administrator at a banking and insurance group His areas of expertise include DB2 performance database administration and backup and recovery Ulrich Gehlert is an IT Architect with IBM Global Services Germany He has worked at IBM for seven years His areas of expertise include the Java platform with a strong focus on database and Web application server technologies and the OS 390 and z OS operating systems He holds a degree in Computing Science from the University of Erlangen Germa
214. ative Java programs as well as through the use of Servlets and JSPs running on a WebSphere Application Server Who should read this publication If you are a seasoned mainframe developer this publication will help you understand that Java is now a first class member of the programming language portfolio on the mainframe You will find that developing in Java for the z OS platform combines the best of two worlds The performance and reliability of the mainframe with the sophisticated development tools on the workstation You will also find that Java is the ideal development environment for enabling your DB2 system on z OS for the World Wide Web If on the other hand you are an experienced Java programmer with no z OS background this publication will show you that the mainframe is as good a platform for running your applications as any other Also this publication has a strong emphasis on SQLJ which currently does not have as much attention in the Java community as it should have The first part of the publication is an introduction to the Java programming language and environment and is intended for people that are really new to Java If you have prior experience with Java you may wish to skip this part or just review Chapter 3 Accessing DB2 from Java on page 23 which covers the JDBC API In the second part we cover the installation process describing which software you are going to need on both the mainframe and the development workst
215. ats to use when connecting to a DB2 for z OS and OS 390 data source Table 3 2 JDBC Drivers and URL formats WR NN Com ibm db2 sglj jdbc DB2SQLJDriver jdbc db20s390sq1j database COM ibm db2 jdbc app DB2Driver jdbc db2 database com ibm db2 jcc DB2Driver jdbc db2 database com ibm db2 jcc DB2Driver jdbc db2 server port database 38 DB2 for z OS and OS 390 Ready for Java URL format Notes gt database represents the name of the target subsystem or DB2 location name gt Server represents the TCP IP address or host name of the target system gt portrepresents the SQL port number used by the DB2 server It can be omitted if this server uses the DB2 default port 446 To connect to a database using the Type 4 driver the code is shown in Example 3 2 Example 3 2 Connecting using the Type 4 driver Connection con DriverManager getConnection jdbc db2 wtsc63 itso ibm com 33756 DB7Y sflynn girliel The DriverManager interface is part of the java sql package As we have mentioned earlier the DriverManager interface lacks portability as the application needs to specify specific JDBC drivers URLs and databases in the code Using the DB2 Universal Driver for Java Common Connectivity has simplified this as you can point to the same DB2 OS 390 or z OS database from both the Windows platform as well as z OS or OS 390 However with other drivers code must be changed directly before it can be executed
216. ava beans can be parts of Java programs or they can exist as self contained applications Java beans can be assembled to create complex applications and they can run within other component architectures such as ActiveX and OpenDoc Java Development Kit JDK The Java Development Kit is the set of Java technologies made available to licensed developers by Sun Microsystems Each release of the JDK contains the following the Java Compiler Java Virtual Machine Java Class Libraries Java Applet Viewer Java Debugger and other tools Java Naming and Directory Interface JNDI A set of APIs that assist with the interfacing to multiple naming and directory services Definition copyright 1996 1999 Sun Microsystems Inc All Rights Reserved Used by permission Java Native Interface JNI A native programming interface that allows Java code running inside a Java Virtual Machine VM to interoperate with applications and libraries written in other programming languages such as C and C Java Platform The Java Virtual Machine and the Java Core classes make up the Java Platform The Java Platform provides a uniform programming interface to a 100 Pure Java program regardless of the underlying operating system Definition copyright 1996 1999 Sun Microsystems Inc All Rights Reserved Used by permission Java Remote Method Invocation RMI Java Remote Method Invocation is method invocation between peers or between client and server
217. available The ServerTime is based on the DRDA server elapsed time feature that was introduced in DB2for z OS and OS 390 Version 7 As a consequence DRDA has to be involved in the transaction to get the ServerTime If you are running your application local to DB2 you always get O zero from the getServerTimeMicros method This is true both for DB2 LUW and DB2 for z OS when running the JCC Driver as a Type 2 driver When run as a Type 4 driver JCC always uses DRDA to connect to the server Please also note that the ApplicationTime is in milliseconds using System currentTimeMillis wheras the other times are presented in microseconds 12 3 7 Native DB2 server SQL error messages Each DB2 server including DB2 for OS 390 V6 and V7 now provides stored procedures that allow applications to retrieve the native error message text for a given error or warning The name of the stored procedure is SYSIBM SQLCAMESSAGE See Required DB2 for z OS changes to enable the Universal Driver on page 58 12 3 8 Multiple open cursors 230 Currently DB2 for z OS and OS 290 Version 7 it is not possible for an application to have more than one instance of an open cursor An attempt to open a cursor that is already open fails with SQLCODE 502 DB2 returns an error because the cursor is already open from the previous call Although this may be OK for most cursors in embedded applications it poses a big problem for SQLJ iterators The SQLJ API syntax allo
218. b2jccmain BARTRI DISTSERV 0050 1170 V445 G9012728 G5AA 00F199927719 1170 ACCESSING DATA FOR 9 1 39 40 V447 LOCATION SESSID A ST TIME V448 9 1 39 40 33756 1450 W R2 0232218533048 Figure 12 1 DISPLAY THREAD command showing basic thread information After supplying client information the output also shows the supplied user name application name and workstation name see Figure 12 2 DISPLAY THREAD DETAIL DSNV4O1I DB7Y DISPLAY THREAD REPORT FOLLOWS DSNV402I DB7Y ACTIVE THREADS NAME STA REQ ID AUTHID PLAN ASID TOKEN SERVER RA 4 db2jccmain BARTR1 DISTSERV 0050 1166 V437 WORKSTATION a23wpm64 USERID Ulrich Gehlert APPLICATION NAME ClientInfoTest V445 G9012728 G5A4 00F1996EB1A2 1166 ACCESSING DATA FOR 9 1 39 40 V447 LOCATION SESSID A ST TIME V448 9 1 39 40 33756 1444 W R2 0232218144291 Figure 12 2 DISPLAY THREAD command showing extended client information Tip Instead of setting the client information from your program you can also set it up in the connection URL or in a DataSource definition using the extended URL syntax described in Setting connection properties in the URL on page 222 and the respective property keys listed in Table 12 1 on page 223 12 3 6 Java API for application monitoring To help you isolate performance problems with your applications the DB2 Universal Driver for SQLJ and JDBC provides a proprietary API DB2SystemMonitor class to enable application monitoring
219. base server to which you are currently connected Most of these methods are of interest mainly to tool developers Rather than explain each and every method from this interface we present a short demonstration program which calls each method that takes no parameters and prints the result of the invocation Example 7 1 Try and run this sample against your DB2 database server Example 7 1 Printing database metadata package com ibm itso sg246435 jdbc import java lang reflect Array import java lang reflect InvocationTargetException import java lang reflect Method import java sql Connection import java sql DatabaseMetaData import java sql DriverManager Demonstrate the database metadata API by invoking all metadata methods that take no parameters and printing the results author Ulrich Gehlert DB2 for z OS and OS 390 Ready for Java public class DatabaseMetaDataDemo Invokes all no argument methods of the DatabaseMetaData object and prints the result of the invokation unless it s an array param md DatabaseMetaData to be printed private static void printMetaData DatabaseMetaData md Object noargs Method methods DatabaseMetaData class getMethods for int i 0 i lt methods length i Method m methods i It s a method taking a parameter Skip this one if m getParameterTypes length gt 0 continue try Object result m invoke
220. be appropriate to set up a filter Figure 5 17 amp P Confirm Filters x 4 vendor of type DB2 Universal Database For 05 390 V7 usually contains large amounts of data For performance reasons you should set appropriate filters to include only the data items which you are interested in To update the filter click Update Filter below otherwise click Continue Update filter Cancel Figure 5 17 Confirm Filters As our DB2 system is very small we click Continue Figure 5 15 on page 76 appears again with the indication at the bottom Establishing connection to DB7Y If everything is ok Figure 5 19 on page 79 appears Otherwise you receive an error message like Figure 5 18 In this case the network is down and we cannot get to the host system that is running our DB7Y subsystem IWASD126E Problems encountered while trying to establish connection DB7Y Reason Unknown server wtsc63 itso ibm com Figure 5 18 Database connection error In the lower left hand side DB7Y was added to DB2 Servers 78 DB2 for z OS and OS 390 Ready for Java Data IBM WebSphere Studio Application Developer File Edit Source Refactor Navigate Search Project Run Window Help IS g 8 gsgs amp ls amp ls 8 4 Bie amp amp ry O Data Definition x JJudydass java X 9 3 129 5624 6435 public Glass Judyclass 4 J fy 8 gy judy public static void mainiStri
221. beans With these beans the definition of the physical data store is configured in the container deployment descriptors using XML and all access to the bean and between the bean and the physical data is managed using this descriptor file Message driven beans These beans were added in the EJB 2 0 specification and handle the interfacing of asynchronous JMS messages and synchronous EJB components Chapter 2 An introduction to Java 2 Enterprise Edition 21 22 DB2 for z OS and OS 390 Ready for Java Accessing DB2 from Java This chapter of the publication describes how a Java application can access DB2 for or OS 390 and z OS databases It describes the processes and components that can be used from either a Windows development platform or running under Unix System Services on z OS or OS 390 The topics discussed are Java and JDBC Selecting and using JDBC drivers The Universal Driver for SQLJ and JDBC Different ways to connect to a DB2 for z OS and OS 390 system Using the DriverManager and DataSource APIs SQLJ versus JDBC v www NY Copyright IBM Corp 2003 All rights reserved 23 3 1 JDBC basics When accessing relational data from a Java application you cannot go far without first understanding JDBC All database access occurs through JDBC from connecting to a database to actually processing the data in that database Java Database Connectivity JDBC is an application programming interface API that the Java
222. ber to use Tip To get an overview of the options for remote debugging type java Xrunjdwp help To remotely debug our sample program follow these steps 1 As described in Running the Hello application from Unix System Services on page 113 log into USS and change to the directory to which you exported the code 2 Start the program in z OS USS with the following command line java Xdebug Xnoagent Xrunjdwp transport dt socket server y suspend y address 8000 com ibm itso sg246435 jdbc Hello The JVM will start up and then suspend waiting for a debugger to attach to it 3 In WSAD select Run Debug The Launch Configuration dialog opens Select Remote Java Application and click New In the Name field enter a name for the new launch configuration for example Remote Hello In the Connection Properties fields enter the mainframe s TCP IP host name or IP address If you used a port number different from the default of 8000 enter that as well see Figure 6 12 dp Launch Configurations Eg Create manage and run launch configurations Launch Configurations Name Remote Hello Attach to a Running Proc PA Debug a Compiled Applic 3 Java Application SF Connect E Source y Common FJ Hello 4 Java Bean Project du JUnit 5G24 6435 Browse a Remote Java Application a 5624 6435 Connection Type Run time Workbench Standard Socket Attach 7 PR Server 0 Stored Procedure Debug N WebSphere Applica
223. better understand the program preparation process for SQLJ we first discuss how the program preparation process works for other languages with embedded SQL support such as COBOL This process is illustrated in Figure 9 1 on page 151 The straight lines indicate steps that occur at compile time the dashed line indicates runtime 1 The original source file is translated by the DB2 precompiler into a compilable COBOL file in which the embedded SQL statements from the original source program have been replaced by calls to the DB2 language interface The precompiler generates a consistency token into the COBOL file which is later used at runtime to verify that the information in the DB2 catalog is still in sync with the program The precompiler also generates a DataBase Request Module DBRM which is basically a file containing all SQL statements in the program The DBRM contains the same consistency token The precompiled COBOL program is then translated into an object module not explicitly shown in the figure and the object module is then linked to form a load module The DBRM produced in step 1 is bound against the database The BIND utility checks for correct SQL statement syntax that all required objects tables etc exist and that the person performing the bind step has sufficient permissions to execute the statements in the DBRM DB2 also determines an access path to execute the SQL statements in the DBRM An access pat
224. bility to properly close the iterator when done with it Example 11 15 Employee findAll method public static EmployeeIterator findAll Ctx ctx throws SQLException EmployeeIterator iter sql ctx iter SELECT EMPNO FIRSTNME gt MIDINIT gt LASTNAME gt HIREDATE CASE SEX WHEN M THEN 1 ELSE O END SALARY FROM DSN8710 EMP ys return iter Next we declare the iterator class The iterator columns must correspond in number and data type to the columns in the SELECT list see Iterator declarations on page 177 for a detailed explanation of iterator declarations The iterator declaration can go anywhere in the class body for example right before the findAl1 method Example 11 16 Declaration of Employeelterator sql public static iterator EmployeeIterator String EMPNO String FIRSTNME String MIDINIT String LASTNAME Date HIREDATE boolean SEX BigDecimal SALARY E Tip Note that WSAD will flag the statement when you code the findA11 method for the Employeelterator class Employeelterator cannot be resolved You can happily ignore this message This message is displayed because WSAD does not understand that an SQLJ iterator declaration results in a class file until after translation To put this to the test save the SQLJ file in WSAD This will invoke the SQLJ translator and the Java compiler If everything is fine you should not receive any errors In WSAD
225. cation binaries saved in WebSphere 5ROMO0 BS01 AppServerAvstemp 0 workspace cellsicell 11 applications SG24 6435 static_war ear SG24 Environment 5435 static war ear System Administration ADMASD11I Cleanup of temp dir for app SG24 B435 static war done Troubleshooting ADMASO13l Application SG24 6435 static_war installed successfully Application SG24 6435 static_war installed successfully If you want to start the application you must first save changes to the master configuration Save to Master i Figure 15 20 Save to master configuration Chapter 15 Using Servlets to access DB2 275 Click the Save button on the next window Figure 15 21 to actually update the master repository trative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Back gt a Qsearch Favorites Media amp B 3 m i Address amp http wtsc63 itso ibm com 9080 admin secure logon do WebSphere Application Server Administrative Console l Version 5 Home Save Preferences Logout Help User ID Message s cent amp y Changes have been made to your local configuration Click Save to apply changes to the master configuration Servers E The server may need to be restarted for these changes to take effect E Applications Enterprise Applications Enterprise Applications gt Install New Application Save Resources Security Save your workspace changes to the mas
226. ce passing the JDBC connection 138 DB2 for z OS and OS 390 Ready for Java 4 All remaining arguments on the command line if any are file names Add the files to the Spufi object to process To obtain the full poor man s SPUFI program see Appendix C Additional material on page 313 Chapter 7 JDBC revisited 139 140 DB2 for z OS and OS 390 Ready for Java Getting started with SQLJ In one of the previous chapters we developed a simple JDBC program to list the contents of a DB2 table In this chapter we develop the same program using SQLJ instead of JDBC Again we run the application first from the development workstation then on the z OS machine Note that in order to keep things simple we only show the translation part of the SQLJ program preparation process meaning that the application runs uncustomized and therefore uses dynamic SQL behind the scenes Therefore do not expect the application to run faster than its JDBC equivalent Copyright IBM Corp 2003 All rights reserved 141 8 1 Creating the source file 142 Our sample SQLJ program will reside in the same project as the JDBC version developed earlier in this publication To keep things nicely separated we create a different Java package for the SQLJ version Note If you are using the WSAD team support or plan to do so later it is a good idea to tell WSAD to treat files ending with the sqlj extension as text not binary files Select Window
227. ce of a JavaBeans component See JavaBeans Binary large object BLOB A sequence of bytes with a size ranging from 0 bytes to 2 gigabytes This string does not have an associated code page and character set Image audio and video objects are stored in BLOBs Compare to character large object CLOB Bind The process by which the output from the DB2 precompiler is converted to a usable control structure called a package or an application plan During the process access paths to the data are selected and some authorization checking is performed BLOB See Binary large object Browser An Internet based tool that lets users browse Web sites Built in function A function that is supplied by DB2 Contrast with user defined function Bytecode Machine independent code generated by the Java compiler and executed by the Java interpreter CAF See call attachment facility Call attachment facility CAF A DB2 attachment facility for application programs running in TSO or MVS batch The CAF is an alternative to the DSN command processor and allows greater control over the execution environment 317 Call level interface CLI Acallable application program interface API for database access which is an alternative to using embedded SQL In contrast to embedded SQL DB2 CLI does not require the user to precompile or bind applications but instead provides a standard set of functions to process SQL statements and related services at run
228. cess DB2 257 258 at sqlj runtime error Errors raiseError Errors java 73 at sqlj runtime error RuntimeRefErrors raise NULL CONN CTX RuntimeRefErrors j ava 136 at com ibm itso sg246435 sq1lj Employee findAl 1 Employee sqlj 223 at com ibm itso sg246435 web EmployeeListServlet printTable EmployeeListServlet java 91 at com ibm itso sg246435 web EmployeeListServlet doGet EmployeeListServlet java 38 n To solve this problem we can set up a data source in the WebSphere configuration by the name of jdbc empdb as we have an explicit context declaration with that name in the Employee class Setting up a data source in WebSphere Studio To set up a data source in WebSphere Studio perform the following steps 1 Reopen the WebSphere v5 0 Test Environment in the Servers view 2 In the editor for the server configuration select the Data Source tab From the JDBC provider list click the Add button to the right of the JDBC provider list Figure 15 5 At the time of writing WSAD did not support the new Universal Driver for DB2 for z OS so you should not select Default DB2 JDBC Provider J EmployeeListServlet java cx Websphere We x 39 Web Browser Data sources Scope localhost localhost server1 gt Node Settings Server Settings Create and manage data sources JDBC provider list Name Implementation class name Add D cloudscape JDBC Driver com ibm db2j jdbc DB2jConnectionPoolDataSource
229. control structure used by an application program to point to a row of interest within some set of rows and to retrieve rows from the set possibly making updates or deletions Cursor stability CS An isolation level that locks any row accessed by a transaction of an application while the cursor is positioned on the row The lock remains in effect until the next row is fetched or the transaction is terminated If any data is changed in a row the lock is held until the change is committed to the database Data Definition Language DDL A language for describing data and its relationships in a database Data Manipulation Language DML A subset of SQL statements used to manipulate data Database management system DBMS A software system that controls the creation organization and modification of a database and access to the data stored within it Database request module DBRM A data set member that is created by the DB2 UDB for OS 390 precompiler and that contains information about SQL statements DBRMs are used in the bind process DB2 thread The DB2 structure that describes an application s connection traces its progress processes resource functions and delimits its accessibility to DB2 resources and services DBCLOB Asequence of bytes representing double byte characters where the size can be up to 2 gigabytes Although the size of double byte character large object values can be anywhere up to 2 gigabytes in general the
230. correct type If you get this wrong you can cause runtime errors and ultimately program crashes Adding to this complexity is that platforms have what is called memory models This defines how memory is allocated and used within that platform stating for example that all variables must start on word boundaries while other memory models just use the next available space This makes the uses of pointers not very portable across different platforms Chapter 2 An introduction to Java 2 Enterprise Edition 13 As Java manages the memory requirements of the objects it creates the need for pointers to manage objects is removed Beyond this the designers of Java decided to remove the use of pointers altogether to remove the complexity of their use and to increase the overall stability of the language Also the garbage collection processing going on behind the scenes would make the use of pointers very dangerous as it moves objects around within memory to increase memory access performance 2 3 5 No more GOTOs One of the first things you are taught when learning a programming language is a statement called GOTO which allows you to jump from one area of the program to another The next thing they teach you is do not use it as it makes debugging and error tracing difficult Even though it was considered bad most newer languages include a type of GOTO statement as it can make error processing easier Once an error occurs such as a negative SQLCODE
231. ct oriented programming 12 ODBC 8 25 online checking 45 236 Oracle 24 P package 150 page directive 280 parameter markers 106 PATH 72 PCOM 68 performance 25 Persistent Reusable JVM 7 PKLIST 48 pointers 4 13 positioned DELETE 128 positioned iterator 145 177 182 236 declaration 177 usage 182 positioned UPDATE 127 PQ19814 26 PQ36011 26 PQ62695 54 58 PQ72453 54 59 PreparedStatement 106 prepareStatement 104 105 primitive data types 13 byte 13 char 13 double 13 float 13 int 13 long 13 short 13 PrintWriter 17 profile customizations 153 deserializing 153 profile entry 153 property description 97 property name 97 property type 97 property value 97 Q QMF 46 R READ COMMITTED 176 read stability 176 READ UNCOMMITTED 176 Redbooks Web site 328 Contact us xxiv release resources 235 remote debugging 119 Remote Method Invocation 12 REPEATABLE READ 176 repeatable read 176 resauth Container 93 resource pooling 19 ResultSet 107 ResultSetMetaData 135 ResultSetMetaData API 123 retrieveMessagesFromServerOnGetMessage 104 RMI 12 rootpkgname 169 RR 176 RRS 33 88 RRS Attachment Facility 29 31 RRSAF 29 RS 176 Running Man 109 S Samba 113 SAVEPOINT support 231 scrollable cursor 225 scrollable cursors 129 SDK 7 SDSNEXIT 70 SDSNLOAD 70 SDSNLOD2 70 sensitivity 179 SERIALIZABLE 176 serialized profile 42 145 194 service 17 servlet container 17 servlet methods destroy
232. ction using the SET TRANSACTION ISOLATION LEVEL clause see Executable statements on page 176 However each package in DB2 is created to use a specific isolation level Therefore the profile customizer by default creates four packages one for each isolation level The names of these packages consist of the root package name which you specify when invoking the profile customizer and a suffix of 1 through 4 corresponding to isolation levels UR CS RS and RR respectively Since a package name in DB2 can be up to eight characters long the root package name must not exceed seven characters For example if your root package name is HELLO the profile customizer creates the following packages HELLO1 For isolation level UR uncommitted read HELLO2 For isolation level CS cursor stability HELLO3 For isolation level RS read stability HELLO4 For isolation level RR repeatable read If you are sure that your program will always use one specific isolation level you can free the packages created for the other isolation levels Alternatively you can tell the profile customizer to only create one package for a specific isolation level Of course if you switched to a different transaction isolation level at runtime you will receive a 805 error Package not found You do so by specifying the following option on the bind command singlepkgname pkgname bindoptions isolation isolation level Where gt pkgname is the name of the package
233. cts their published announcements or other publicly available sources IBM has not tested those products and cannot confirm the accuracy of performance compatibility or any other claims related to non IBM products Questions on the capabilities of non IBM products should be addressed to the suppliers of those products This information contains examples of data and reports used in daily business operations To illustrate them as completely as possible the examples include the names of individuals companies brands and products All of these names are fictitious and any similarity to the names and addresses used by an actual business enterprise is entirely coincidental COPYRIGHT LICENSE This information contains sample application programs in source language which illustrates programming techniques on various operating platforms You may copy modify and distribute these sample programs in any form without payment to IBM for the purposes of developing using marketing or distributing application programs conforming to the application programming interface for the operating platform for which the sample programs are written These examples have not been thoroughly tested under all conditions IBM therefore cannot guarantee or imply reliability serviceability or function of these programs You may copy modify and distribute these sample programs in any form without payment to IBM for the purposes of developing using marketing or distribut
234. cture retrieved for employee empno in dir with name Emp empno bmp finally if in null in close if out null out close public static void main String args try initialize testInsert DB2 for z OS and OS 390 Ready for Java Employee emp Employee findByPrimaryKey ctx 000042 System out printIn Successfully retrieved emp emp setLastName Dentist emp update ctx System out printIn Successfully updated emp emp createPicture ctx C temp arthur dent bmp System out printIn Successfully stored picture of emp testGetPicture 000042 new File c temp emp delete ctx try emp Employee findByPrimaryKey ctx 000042 catch SQLException expected if expected getSQLState equals 02000 System out printIn Employee record deleted successfully else Different SQL exception we didn t really expect it throw expected testFindAl1 testGetPicture 000130 new File c temp catch SQLException e System err println e SQLCODE e getErrorCode catch Exception e System err printIn e Serviets We also provide listings for the three Servlets that are used in this publication Again the version presented here is the final version that is after all Servlets are linked to each other EmployeeListServlet Example B 5 shows the EmployeeListServlet
235. cuted 3 If its a SELECT statement 4 call executeQuery which displays the result set and returns the number of rows found which we log to the output stream 5 Else call executeUpdate and log the number of rows affected to the output stream 6 If auto commit is turned on commit the current transaction 7 If an error occurred log it to the error stream The exception is not thrown again so our caller can happily continue with the next SQL statement 8 Clean up using a utility method not shown here see the complete source code in the Appendix The executeUpdate method is easy it simply calls stmt executeUpdate and returns the number of rows affected Example 7 9 Recall that PreparedStatement executeUpdate returns the number of rows affected in the case of an INSERT UPDATE or DELETE statement and zero for DDL statements such as CREATE TABLE Example 7 9 Spufi executeUpdate Execute an update that is non SELECT statement param stmt Statement to execute Qreturn Number of rows affected throws SQLException xf private int executeUpdate PreparedStatement stmt throws SQLException return stmt executeUpdate Displaying result sets The executeQuery method is more interesting It first executes the database query receiving a ResultSet Then it prints a column header showing the column names Next it processes the result set one row at a time printing the
236. d in Using more than one context class on page 194 We describe how to declare and use an explicit context in the following sections 10 6 3 Declaring a context class The declaration of an SQLJ context class is similar to an SQLJ iterator declaration sql modifiers context ContextClassName with keyword value Where gt modifiers is a list of Java modifiers as explained in Iterator declarations on page 177 gt ContextClassName is a valid Java class name The optional with clause causes constants to be inserted in the generated connection context class as explained in Modifying iterator behavior using the implements and with clauses on page 178 allowing for vendor specific extensions There are also some predefined keywords most notably dataSource which is explained in Creating an instance of the context class on page 190 Chapter 10 SQLJ tutorial and reference 189 As with iterator declarations the context declaration is translated into a class declaration by the SQLJ translator Therefore a context declaration is only allowed where a Java class declaration would be allowed 10 6 4 Creating an instance of the context class To set up a context you create an instance of the context class and assign it to a Java variable The context class has several constructors gt public MyCtx java sql Connection conn gt public MyCtx sqlj runtime ConnectionContext other throws java sql SQLException v
237. d it yet Important However if the profile has been customized for DB2 customization information present in the serialized profile and for some reason the package has not been bound against the database during or after customization you receive a 805 SQL error package not found at runtime You will also receive this error when the package had been bound before but the SQLJ file was retranslated and re customized without rebinding the package In this case the consistency token recorded in the profile does not match the one in the database So once the profile has been customized for DB2 you must have a matching package bound in the database In this case SQLJ runtime will not revert to using JDBC The profiles remain platform independent even after customization because SQLJ implementations are required to ignore customization information they do not know about In fact it is possible to customize one profile several times once for each target database system This makes it possible to run the program unchanged against say DB2 and Oracle databases However all DB2 implementations across the DB2 family use the same customization You do not have to customize again when switching from a DB2 for Linux Unix and Windows to DB2 for z OS and OS 390 when using the DB2 Universal Driver DB2 for z OS and OS 390 Ready for Java 9 3 1 Isolation levels SQLJ allows you to change the isolation level at the beginning of a new transa
238. d now see Example 16 3 Since the SQL tag library does not have support for LOB data we cannot re write the EmployeePicServlet as a JSP which is why we still use that Servlet from the new JSP Example 16 3 EmployeeDetail jsp lt DOCTYPE HTML PUBLIC W3C DTD HTML 4 01 Transitional EN gt lt html gt lt head gt lt taglib uri jspsql prefix sql gt lt page language java contentType text html charset IS0 8859 1 o 4 gt Chapter 16 JavaServer Pages 285 meta http equiv Content Type content text html charset 1S0 8859 1 meta name GENERATOR content IBM WebSphere Studio gt meta http equiv Content Style Type content text css gt lt link href theme Master css rel stylesheet type text css gt lt title gt EmployeeDetail jsp lt title gt lt head gt lt body gt sql dataSourceSpec id MyConnection dataSource jdbc empdb gt lt sql select id empdetail connectionSpec MyConnection maxRows 1 gt 1 lt sql sql gt SELECT EMPNO LASTNAME FIRSTNME MIDINIT HIREDATE gt SALARY FROM DSN8710 EMP WHERE EMPNO sgl sql lt sql parameter position 1 type CHAR value lt request getParameter empno gt gt 2 lt sql select gt lt table gt lt tr gt lt td colspan 2 gt 3 lt img src EmployeePicServlet empno 5 request getParameter empno gt gt lt td gt lt tr gt lt tr gt lt td gt Serial lt td gt td
239. d on Note that we update a column that is not part of the SELECT list 5 Commits every 100 rows 6 Commits any remaining uncommitted changes 7 Itis good practice to close the iterator in a finally block so the iterator will be closed even if an exception is thrown This is especially important for holdable iterators 10 5 6 Calling stored procedures To call a stored procedure you use the CALL statement sql CALL procname argl arg2 3 Since stored procedures have input output and input output variables it is possible that the values of the host variables are changed by invoking the procedure 10 6 Connection contexts Each SQLJ statement runs in a given connection context which is a Java object that encapsulates a database connection similar to a JDBC Connection object and manages the use of serialized profiles in the program 10 6 1 Setting up and using an implicit connection context Unless you specify it on the SQLJ statement each statement will be executed under an implicit connection context which in turn is usually an instance of class sqlj runtime ref DefaultContext This class is part of the SQLJ runtime library Important Except maybe in stand alone programs that do not share a JVM with other applications using an implicit context is strongly discouraged Why the connection context is important on page 189 explains why Depending on the environment your program is running in a default connectio
240. d to EBCDIC when the data was externalized Also enable mainframe software products such as DB2 for example to handle Unicode data Change the OS 390 hardware to support IEEE floating point gt Extend the IBM development kit to make use of mainframe facilities such as a adding interfaces to support principal based security These changes were implemented across releases of the IBM development kit and IBM hardware A major concern with running Java on the mainframe was the performance of the JVM Transaction processing on the z OS and OS 390 platforms is characterized by short repetitive transactions that are run in subsystems such as CICS and DB2 These environments focus on sharing resources across activities by loading system wide resources at subsystem startup time and then sharing resources across transactions thus minimizing transaction startup and completion Compare this with the initial JVM port in 1996 where activity would kick off a full copy of the JVM reloading all classes every time To illustrate the cost of starting a JVM each initialization the JVM would load sixty system classes allocate over 1000 non array objects and use over 700 array objects with a majority of these objects not being used by the application being executed The latest release of IBM s Java Development Kit now called IBM Software Development Kit SDK for z OS Java 2 Technology Edition V1 4 introduces persistent reusable Java Virtual
241. data on z OS or OS 390 DB2 databases You are kindly encouraged to start using the new JCC Type 2 driver Can be used with DB2 for Linux Unix and Windows This is the old net driver You should look at replacing this driver with the Type 4 flavor of the new Universal Driver o Can be used on Linux Unix and Windows platform as well as z OS or OS 390 platforms Pure Java code driver that uses TCP IP across DRDA to directly connect to database Does not require DB2 Connect code to be present on client machine to access remote databases T e Available on Linux Unix and Windows and soon on z OS and OS 390 N A 9v N N N Driver type o o level db2jcc jar com ibm db2 jcc DB2Driver 3 1 2 The IBM DB2 Universal Driver for SQLJ and JDBC As mentioned before IBM has released a brand new JDBC driver with DB2 UDB Version 8 for Linux Unix and Windows called the JBM DB2 Universal Driver for SOLJ and JDBC also known as the DB2 Universal Driver or the DB2 Universal Driver for Java Common Connectivity or JCC Chapter 3 Accessing DB2 from Java 27 28 It supports Type 2 and Type 4 connectivity to DB2 Version 8 for Windows AIX 32 bit and 64 bit Solaris 32 bit and 64 bit Linux IA 32 and z31 bit and HP 32 bit and 64 bit The Type 4 driver has been available since V8 went generally available The Type 2 driver support was added with FixPak 2 V8 1 2 Using the Type 4 driver you cannot only access
242. dio Application Developer WSAD we also discuss the WSAD and Windows setup Copyright IBM Corp 2003 All rights reserved 51 52 DB2 for z OS and OS 390 Ready for Java Products and levels Now and soon This chapter provides a brief overview of the products we used during the development of this publication as well as which versions of the software products we used Copyright IBM Corp 2003 All rights reserved 53 4 1 Products and levels With the widespread demand for Java functionality advances arrive on a frequent basis This means that the Java aspect of almost every product is in constant flux Few enhancements arrive on product release boundaries This is a fact of life and will remain so We list in this section the products we used Now on page 54 when we wrote this publication We are aware of some future enhancements to the products we used and list them for you in Soon on page 54 It is likely that some of them may arrive sooner or later than we think but we want you to understand the implications 4 1 1 Now At the time of writing this publication 4 1 2 Soon 54 DB2 UDB V8 for Linux Unix and Windows FixPak 2 was available at the time we wrote this publication It introduces the Type 2 Java Universal Driver The Type 4 SQLJ and JDBC Driver of the Universal Driver family was introduced at DB2 for LUW V8 GA time In the meantime FixPak 3 became available in August 2003 and testing for FixPak
243. e convert M to true and F to false 11 2 1 Implementing attributes accessors and constructors The next step is to create accessors for the fields An accessor is a method to retrieve or modify a field s value also called a getter or setter method respectively In Java it is good practice to always access fields using an accessor method rather than referring to the field by name WSAD can create the accessor methods for you automatically Right click anywhere in the editor window then select Source gt Generate Getter and Setter The dialog that opens allows us to specify which attributes to generate accessors for Figure 11 1 on page 202 Chapter 11 SQLJ revisited 201 202 4 Generate Getter and Setter Gif x Select getters and setters to create a educationLevel Oo getEducationLevel Oo setEducationLevel Short 5 a empNo o getEmpNo Oo setEmpNo String E a firstName o getFirstName o setFirstName String E a hireDate o getHireDate o setHireDate Date E 2 lastName Oo getLastName Oo setLastName String E o male o isMale o setMale boolean a middletnitial o getMiddleInitial o setMiddleInitial String n salary o getSalary o setSalary BigDecimal Deselect All i 16 methods selected OK Cancel Figure 11 1 Generate getter and setter We click Select All to check all of them and then OK Since you ca
244. e empNo String sex null 1 DB2 for z OS and OS 390 Ready for Java sql ctx SELECT FIRSTNME gt MIDINIT gt LASTNAME gt HIREDATE gt SEX SALARY INTO emp firstName emp middleInitial emp lastName emp hireDate gt 1Sex 2 emp salary FROM DSN8710 EMP WHERE EMPNO empNo ls if sex equals M 3 emp male true else emp male false return emp Notes on Example 11 8 on page 208 1 This declares the local variable to receive the value of the SEX column 2 Here we SELECT INTO the local variable instead of an Employee instance variable 3 Inspect the local variable s value and set the value of the male attribute accordingly However there is a more elegant solution There is a built in conversion in SQLJ from numeric data types to boolean The value 0 zero maps to false whereas any non zero value maps to true So we need an SQL expression that converts the string F to zero and any other string value to non zero The following CASE expression does the trick CASE SEX WHEN M THEN 1 ELSE 0 END Example 11 9 shows the updated source code Note that we can now specify the instance variable directly as a host variable in the SQL statement Example 11 9 Employee findByPrimaryKey second version public static Employee findByPrimaryKey Ctx ctx String empNo throws SQLException Employee emp new Employee empNo sql ctx SELECT FIRSTNME gt MIDINIT gt
245. e in our case Java variables which can in turn be local variables method parameters or fields A sample SQLJ insert statement is shown in Example 11 4 Example 11 4 The Employee insert method public void insert Ctx ctx throws SQLException 1 2 sql ctx 3 INSERT INTO DSN8710 EMP EMPNO 4 FIRSTNME gt MIDINIT LASTNAME HIREDATE SEX SALARY VALUES empNo 5 firstName middleInitial lastName hireDate male M F 6 salary v v v v v wv Notes on Example 11 4 1 The insert method takes an instance of the context class as a parameter We could also have stored that instance in a static variable of the class but this is not considered good practice 2 Since executing the SQLJ statement may fail we have to declare that the method may throw an SQLExcept ion 3 Use ctx as the connection context for the INSERT operation 4 The list of columns to be inserted Note that for brevity we do not supply values for all of the columns in the EMP sample table 5 The list of values for the corresponding columns The number of values must match the number of columns to be inserted 6 We make use of the Java conditional operator to convert the boolean value of the male attribute to the corresponding string value Note that expressions as opposed to host variables must be parenthesized 11 2 4 Creating a test driver At this point we are ready to run a first test and verify
246. e Connection Establish a JDBC connection to a database ll Connection name pm sOOO CiSCS Database pw gs User ID bart Password Ls RR Database vendor type D82 Universal Database for 05 390 v7 H JDBC driver Other Driver 0 Host E Optional Port number Server name Database Location JDBC driver class com ibm db2 jcc DB2Driver Class location c TSQLLIBYjavaldb2jcc license cisuz je z Browse Connection URL jdbc db2 wtsc63 itso ibm com 33756 DB7Y Filters cne Figure 5 15 Creating a new connection using the DB2 UDB V8 Type 4 Universal Driver We have to change many things on this panel We need the information from the DSNLOO4I message issued by DDF as it starts It can be found in the job log of the DB2 master address space DB7YMSTR in our configuration as shown in Example 5 7 You can also use the output of the DISPLAY DDF command if your DB2 is Version 7 or later gt Connection name You can specify any descriptive string as the connection name Example 5 7 DSNLO004I DB2 390 DDF information used for Control Center and WSAD DSNLOO4I DB7Y DDF START COMPLETE 867 LOCATION DB7Y LU USIBMSC SCDB7Y GENERICLU NONE DOMAIN wtsc63 itso ibm com TCPPORT 33756 RESPORT 33757 DSN90221 DB7Y DSNYASCP STA DB2 NORMAL COMPLETION Database You must specify the DB2 location name here gt User ID and password for our DB2 for OS 390
247. e Console Type in your user ID and click OK 5 6 2 Setting up system variables This takes you into the Administrative Console application Figure 5 24 E WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Bak gt G A Qsearch Favorites media 4 E5 5j E Address amp http wtsc63 itso ibm com 9080 admin secure logon do Links search the Web with Google amp IBM BluePages amp IBM Standard Software Installer Customize Links 4 amp IBM Internal Help amp Free Hotmail WebSphere Application Server Administrative Console ME Version 5 y 1 Home Save Preferences Logout Help User ID bart ceit11 WebSphere Application Servers Server on IBM com Applications IBM WebSphere Application Server for z OS 5 0 0 imi Build Number 501000 Build Date 9415 03 Resources Security E Environment Update Web Server Plugin Virtual Hosts Manage WebSphere Variableg Shared Libraries Naming De WebSphere Developer 9 InfoCenter main Figure 5 24 WebSpere Administrative Console Chapter 5 Setup 83 84 Before we can start with the definition of the DB2 datasource we first need to set up a few system variables inside WAS To do so expand the Environment tree in the left pane and select Manage WebSphere Variables This takes you to the window shown in Figure 5 25 Make sure t
248. e SQL operation currently executing under this ExecutionContext This method has no effect if the execution context does not currently execute an SQL operation getMaxFieldSize DB2 for z OS and OS 390 Ready for Java public int getMaxFieldSize Returns the maximum number of bytes that are returned for any character column in queries that use the given execution context A value of 0 means that the maximum number of bytes is unlimited getMaxRows public int getMaxRows Returns the maximum number of rows that are returned for any query that uses the given execution context A value of 0 means that the maximum number of rows is unlimited getQueryTimeout public int getQueryTimeout Returns the maximum number of seconds that SQL operations using this context may take to complete getNextResultSet public ResultSet getNextResultSet throws SQLException After a stored procedure call this method returns a result set from the stored procedure Each call to getNextResultSet closes the result set that was retrieved by the previous call A value of null means that there are no more result sets to be returned getWarnings public SQLWarning getWarnings Returns the first warning that was reported by the last SQL operation that was executed using this context Subsequent warnings are chained to the first warning setBatching public void setBatching boolean batching Turns batching for that execution context on or off It doe
249. e client which must remember the data between calls and pass it to the bean with each method call Another solution is to use stateful session beans Stateful session beans maintain data for the whole conversation with the client and across client requests A good example of such a bean is a shopping cart EJB which remembers all the products that a person has requested in their current session and maintains information such as the total cost of all the items purchased Entity beans Entity beans are beans that maintain persistent data that is typically data stored in a database Although it is not necessarily the case normally relational databases are the target for entity beans The entity controls persistence by maintaining both a copy of data as a stateful component in memory and a matching entry in persistent storage DB2 for z OS and OS 390 Ready for Java There are two types of entity EJBs Bean Managed Persistence BMP entity beans and Container Managed Persistence CMP entity beans Bean managed persistence BMP In the entity EJB with bean managed persistence the bean itself implements the code for managing the connection between the in memory representation of the bean and the persistent representation in the database The coder of BMP beans has a fixed set of methods that he or she fills with the code that will physically process the database row Some of these methods are gt ejbRemove contains the code to physical
250. e concept of compile once run anywhere had its biggest impact with the growth of the Internet browser and the World Wide Web Programs that had been hardware and software specific would now have to run on any platform that would support a browser Internet developers would have to develop applications to run on Windows Unix and Macintosh platforms seamlessly When writing C applications programs have to be written or at least compiled to target the individual platform With Java on the other hand a single version of a program can be written to run on all the required platforms without change by compiling it into platform independent bytecode This bytecode can be moved to any platform that has a JVM and run there This shortens the development cycle for Internet based applications It was this that sold the technical world on Java more than any other issue DB2 for z OS and OS 390 Ready for Java a Compiling with C Compiling with Java Figure 2 2 Java Compile once run anywhere Much effort has been spent on improving the efficiency of the JVM The first major improvement was the introduction of the Just in Time compiler JIT Originally the JVM was just a microcode interpreter It read
251. e database The driver types are numbered 1 to 4 Figure 3 1 Important The naming of these driver types does not represent improvements or versioning of the drivers Type 2 drivers are not a later version of Type 1 drivers and Type 4 is not a fuller implementation of a standard than a Type 2 driver Java application JDBC JDBC API JDBC DriverManager or DataSource Object JDBC Drivers i i lt Type 1 Type 2 Type 3 Type 4 JDBC ODBC Partial Pure Java Pure Java Bridge Native JDBC JDBC Driver JDBC Driver Driver Driver ae Figure 3 1 Java Database Connectivity JDBC API and driver types Type 1 This is the oldest type of driver It was provided by Sun to promote JDBC when no database specific drivers were available With this driver the JDBC API calls an ODBC driver to access the database This driver type is commonly referred to as a JDBC ODBC bridge driver Its shortcoming is that ODBC must be implemented and installed on all clients using this driver type This restricts it to platforms that have ODBC support and as such is mostly Windows centric As it also has to translate every JDBC call into an equivalent ODBC call the performance of a Type 1 driver is not all that great This type is no longer commonly used as virtually every database vendor nowadays supports JDBC and provides their own vendor specific driver For DB2 it is not off
252. e number of rows inserted that is the number of rows that were returned from the SELECT clause 7 1 2 UPDATE 122 The UPDATE statement has the basic form UPDATE TABLENAME SET COLI gt COL2 WHERE CONDITION The executeUpdate method returns the number of rows that have been updated that is the number of rows that matched the condition in the WHERE clause DB2 for z OS and OS 390 Ready for Java 7 1 3 DELETE DELETE has the form DELETE FROM TABLENAME WHERE CONDITION Again the executeUpdate method returns the number of rows that have been deleted in other words that matched the WHERE clause 7 2 NULL handling To indicate the absence of a value a column in a DB2 table may contain the special value NULL unless it has been declared NOT NULL It is important to note that the NULL value is different from the numeric value zero or the empty string to say it again it is used to record the fact that the value is unknown or not applicable Now Java makes a difference between primitive types such as short and int and object types also called reference types such as String While there is a special null value in Java for object types this is not the case with primitive types When you retrieve a column that is mapped to an object type in Java such as a CHAR column that maps to a String there is no problem If the column was NULL a Java null reference will be returned The problem is that many of the
253. e program inside WSAD The output of the entire process compile execute and result is shown in Figure 6 10 on page 118 Chapter 6 Getting started with JDBC 117 C TestJDBC SG24 6435 gt javac com ibm itso sg246435 jdbc Hello java C TestJDBC SG24 6435 gt java com ibm itso sg246435 jdbc Hello THOMPSON MICHAEL 41250 00 KWAN SALLY 38250 00 GEYER JOHN 40175 00 STIEN IRVING 32250 00 PULASKI EVA 36170 00 LUCCHESI VINCENZO 46500 00 HEMMINGER DIAN 46500 00 C TestJDBC SG24 6435 gt Figure 6 10 Compiling and running a JDBC program from the command prompt 6 5 Debugging the application on the workstation 118 WSAD comes with a powerful debugger that allows you to debug programs running both locally that is within WSAD itself and remotely programs that run on a JVM on another machine To debug a program you first add one or more breakpoints in the code then start the program in debug mode To add a breakpoint do the following 1 Open the source file for example Hello java where you want to set the breakpoint 2 On the line where you want to set the breakpoint for example the first line of the main method place your mouse pointer in the gray bar to the left of the editor area 3 Double click to set the breakpoint The breakpoint will be indicated by a marker in the gray bar Note Alternatively you can check the Stop in main option in the launch configuration which causes t
254. e project was not built since the classpath is incomplete Cannot find the class file for sqlj runtime ref ResultSetIterImp1 4 Select Web Library Projects then click Add 5 In the dialog box that opens click Browse Select the SG24 6435 project then click OK 6 WSAD automatically suggests a name of SG24 6435 jar This is the name of the Jar file that will be packaged together with the Web application containing the binary code in the SG24 6435 project Click OK 7 Click OK to end the Project Properties dialog Now we are ready to use the Employee class from our Servlet We start by implementing a simple utility method that embeds a value passed in as argument in HTML lt td gt and lt td gt tags Example 15 3 Example 15 3 The EmployeeListServlet printCol utility method Print the supplied value to the servlet output stream embedded in a lt code gt td lt code gt element param out The servlet output stream param value The value to be printed private void printCol PrintWriter out String value out printIn td DB2 for z OS and OS 390 Ready for Java out printIn value out printIn lt td gt The next step is to write a method that generates one table row representing an employee Add an import statement for the Employee class import com ibm itso sg246435 sqlj Employee Then add the printRow method Example 15 4 This method makes use of the utility met
255. e the proprietary method DB2Sglca getMessage to retrieve the fully formatted message text 2 This line causes the JVM to load the class named in the argument namely the JDBC driver As you can see we load the new Universal Driver 3 Establishes the connection to the database 4 To prepare an SQL statement for execution you invoke the prepareStatement method of the Connection object 5 The variable parts of the statement in this case the lower and upper limits for the SALARY column are indicated by parameter markers which syntactically are represented by question marks 6 Before the statement can be executed successfully we have to supply values for all parameter markers in the statement Note that in this example we could have hardcoded the lower and upper bounds directly in the statement 7 Populate the variable parts of the statement in other words supply values for the parameter markers 8 This is where the statement is actually executed To execute a statement that returns a result such as the SELECT statement in our example you use the executeQuery method whereas statements that do not return a result are executed via the executeUpdate method The executeQuery method returns a ResultSet object which allows you to iterate over the query results 9 This is where we iterate over the result set The next method tries to fetch the next result row from the database returning true if there actua
256. ection Loading the DB2 driver To use the DriverManager interface the program first loads the required JDBC driver by invoking the Class forName method The Class class is in the java lang package which Java loads automatically and therefore no import statement is required when using this method The method can be coded as shown in Example 3 1 on page 37 Example 3 1 Loading the DB2 JDBC driver try Load the DB2 driver Class forName com ibm db2 jcc DB2Driver catch Throwable e e printStackTrace e Chapter 3 Accessing DB2 from Java 37 What happens here is that the Java Virtual Machine brings in the code for that particular JDBC driver The driver automatically registers itself with the JDBC runtime and can now be used to connect to a database This code sample loads the JCC Type 4 JDBC driver which implements JDBC 3 0 features The actual driver that will be used depends on how your CLASSPATH is set up at the time when the application is run See Table 3 1 on page 26 for the combinations of classes referenced in the code and which file needs to be included in the CLASSPATH to designate which class is actually used The DB2 Universal Driver for SQLJ and JDBC Type 4 driver is the driver that we will be using throughout most of this publication although we do use the Type 2 drivers where specific functionality is not available in the Type 4 driver Remember that when using the Type 4 driver db2jcc jar
257. ectory that contains the commands or shell scripts that invoke Java JDBC and SQLJ program preparation and debugging functions such as javac java db2sqljcustomize must be installed in the PATH environment variable Under Unix System Services these are commonly in the directory pp db2javadriver bin The LIBPATH and LD LIBRARY PATH environment variables Dynamic load libraries DLLs contain the native code portion of Type 2 drivers If you are using such a driver then the directory containing these DLLs must be added to the LIBPATH and LD_LIBRARY_PATH environment variables Under Unix System Services these DLLs are commonly in the directory pp db2javadriver lib The CLASSPATH environment variable This is probably the most important environment variable when using JDBC and Java in general This environment variable points to the specific class file s for the JDBC driver you are using in your application This entry needs to match the driver that you are using in your application To see the relation between the drivers and the class files refer to Table 3 1 on page 26 The CLASSPATH can be coded directly with the java command when running a Java application from the command line or from within a shell script using the cp option Example 5 5 shows how to set the CLASSPATH when executing a Java program from the command line it assumes that the variable DB2HOME has been set to your DB2 installation directory usually usr lpp db2 db
258. ed UPDATE and DELETE statements that is the rows that are affected by the UPDATE or DELETE are those matching the predicate in the WHERE clause Using positioned UPDATE and DELETE it is possible to change the underlying table on the fly while iterating through the result set This is useful in situations where the criteria for rows to be updated cannot be expressed in SQL for example because they depend on interactive user input By default result sets are not updateable To create an updateable result set use the three argument form of Connection prepareStatement PreparedStatement prepareStatement String sql int resultSetType int resultSetConcurrency Where resultSetConcurrency is one of gt ResultSet CONCUR READ ONLY resulting in a non updateable cursor the default or gt ResultSet CONCUR UPDATABLE resulting in an updateable cursor The second parameter resultSetType determines whether you get a forward only cursor or a scrollable cursor see Scrollable cursors on page 129 The default is ResultSet TYPE FORWARD ONLY 7 5 1 Positioned UPDATE To update the row a ResultSet is currently positioned on you call one of the updateXxx methods of class ResultSet Like the corresponding getXxx methods you can use either a column index or a column name to refer to the column Chapter 7 JDBC revisited 127 To set a column to a NULL value call the updateNull method or one of the updateXxx methods taking a refere
259. ed a memory leak A somewhat worse problem that often arises in C programs is when a program tries to delete an object that was already deleted often resulting in a crash Java avoids this high maintenance issue by managing the objects that have been created Java objects cannot be explicitly deleted as in C and they will be retained as long as they are referenced within a program This could lead to a massive blow out in memory usage Therefore Java periodically sets off a background task that goes through the current list of objects to verify whether they are still referenced or not If they are not then those objects and the memory allocated by them is freed This process is called garbage collection 2 3 4 Removal of pointers Another area of complexity that exists in older programming languages is the concept of a pointer A pointer contains the physical address of an area in memory It can then be used to access that memory location In C when an object is allocated on the heap a pointer is used to maintain a reference to that object and to ultimately free it Additionally you can use pointers to move through memory using pointer arithmetic and then to use pointer variables to point to a specific data type within memory Although this is a powerful construct it is an area of programming that has to be highly managed to ensure that you are actually pointing to the area you really want to and that the area actually contains the
260. ed as command line arguments as well 6 Declare a connection context instance 7 Declare an iterator variable to hold the result of the query 8 Call the initialization method passing the required connection information and storing the connection context instance into the variable declared in step 6 9 Perform the database query and assign the result to the iterator variable declared in step 7 The query runs under the connection context obtained in step 8 10 Declares Java variables to receive the column values For technical reasons the variables must also be initialized otherwise the Java compiler will complain about using a potentially uninitialized variable 11 Populates the variables declared in 10 with the values from the current row 12 Terminates the loop if no more rows were found 13 In the final block clean up resources by closing the iterator and the context As soon as you save the file WSAD automatically invokes the SQLJ translator which then produces both the translated Java file which in turn is automatically compiled to bytecode by WSAD and a serialized profile Hello SJProfileO ser The SQL translator is discussed in detail in Chapter 9 The SQLJ program preparation process on page 149 8 2 Running the Hello application from WSAD To run the SQLJ sample we create a new launch configuration as we did in Running the Hello application from WSAD on page 109 Then we modify the launch configuration to
261. ed at which it was accepted amongst the development community Along with this acceptance came a flurry of activity to develop new functionality What was perhaps most surprising was that this development was not just coming from the lone computer developer the usual supporter of open systems but also from industry giants such as IBM These companies were involved in the process from working on the base specifications with Sun and releasing Java based products to plug into their existing product line DB2 for z OS and OS 390 Ready for Java These additions were brought together in the release in 1998 of Java 2 As with most stages in the development of Java this was only a step forward and not an end in itself 1 1 2 Accessing the data Over time the development of Java was extensive with the development of language specifications and extensive class libraries to expand on multiplatform functionality The development in this area that is of most significance to us is the development and enhancement of the Java Database Connectivity JDBC API specification Originally database access was not a significant aspect in the development of Java applications as it was focused on small consumer products With the rise of Java as a primary Internet development tool the links to data increased and the need for standard cross platform database calls became necessary Over time the functionality of the JDBC API has increased dramatically as has the i
262. ed by HELLO2 0 0 00 eee eee 165 9 10 Freeing the package 0 cece eee ee 165 9 11 Export to the file system 1 0 2 liliis 166 9 12 Directory structure after the export 0 2 0 eee ee 167 11 1 Generate getter and setter liliis 202 11 2 Skip setter Methods cj ety eet eo ee ia dene eae TEE UE 202 11 3 Sample contents with Dent inserted 0 00 eee eee 208 12 1 DISPLAY THREAD command showing basic thread information 228 12 2 DISPLAY THREAD command showing extended client information 228 12 83 Times collected by DB2 Universal Driver for SQLJ and JDBC monitoring 229 15 1 Creating a Web project step 1 0 0 eee 250 15 2 The WSAD Web perspective uaua unanenn n 251 15 8 Running a Servlet in the WebSphere test environment liuius 253 15 4 Server view indicating that the server should be restarted 257 15 5 Creating a new data SOUCE 1 eee eens 258 15 6 Create a JDBC Provider 0 ccc eens 259 15 7 Create a JDBC Provider 2 0 0 0 ccc eres 260 15 8 Create a data source ren 261 15 9 Create a Data Source 2 1 1 tenes 262 15 10 Add userName and password properties to the data source configuration 262 x DB2 for z OS and OS 390 Ready for Java 15 11 Output from running the EmployeeList Servlet nananana aaan 263 15 12 Output from EmployeeDetailServlet final version
263. efixed by a colon but must additionally be enclosed in parentheses 10 3 Null values SQL supports the distinct value NULL to denote the absence of a value in a column Note that the NULL value is not the same as the numeric value 0 that is zero Unless a column is declared NOT NULL you can insert NULL values into and your queries must be prepared to receive NULL values from that column In other languages with embedded SQL support such as COBOL or C you have to use an additional construct called a NULL indicator Java however does have a distinct null value for reference types the Java null is equivalent to the SQL NULL Whenever you want to retrieve data from a column with NULLs allowed make sure that the corresponding host variable is of reference type as opposed to the primitive types for example int and boolean That is use the wrapper classes in the java lang package For example consider the following code snippet short edlevel sql ctx SELECT EDLEVEL INTO edlevel Chapter 10 SQLJ tutorial and reference 179 FROM DSN8710 EMP WHERE h Since the EDLEVEL column in the DSN8710 EMP sample table is declared nullable the program will fail with an SQLNuTI Exception a subclass of SQLException when a NULL value is encountered To fix this use the wrapper class corresponding to the primitive type Short edlevel sql ctx SELECT EDLEVEL INTO edlevel FROM DSN8710 EMP WHERE h if edlevel nul
264. ege In order for Diane to be able to successfully run the JDBC application she must be given UPDATE privilege on the table either by granting her the privilege individually or by granting it to the PAYROLL group Figure 3 12 The problem with this is that Diane can now update the EMP table directly for example by using QMF with obvious security implications JDBC application UPDATE table UPDATE V EMPID LASTNAME SALARY 000130 QUINTANA 23800 00 000140 NICHOLLS 28420 00 000150 ADAMSON 25280 00 UPDATE y SELECT y PAYROLL has SELECT and UPDATE privilege Figure 3 12 Dynamic SQL with SELECT and UPDATE privilege What we really want is to enable Diane to successfully run the application without giving her direct UPDATE privilege This can be done with static SQL as shown in Figure 3 13 on page 48 User FRED the database administrator who has UPDATE privilege on the table binds a package for the SQLJ application and grants EXECUTE privilege on the package to the PAYROLL group Since Diane is a member of the PAYROLL group she can run the application and execute the package which can then update the database using Fred s privilege Diane cannot however update the table directly through for example QMF Chapter 3 Accessing DB2 from Java 47 In this configuration the trusted entity is no longer a user but an application FRED binds package and GR
265. emp setSalary new BigDecimal 80000 insert Data Definition Navigator 2 I DB7Y jdbc db2 Jwtsce3 it DB Y DB7Y DB2 Universal Dat BF BARTRI 000270 MARIA L PEREZ D21 9001 1980 09 30 CLERK 15 m 000280 ETHEL R SCHNEIDER E11 8997 1967 03 24 OPERATOR 17 eB BARTRS 000290 JOHN R PARKER E11 4502 1980 05 30 OPERATOR 12 m BA BBO 000300 PHILIP x SMITH E11 2095 1972 06 19 OPERATOR 14 9X BBOLDAP 000310 MAUDE F SETRIGHT E11 3332 1964 09 12 OPERATOR 12 BF CBASRUZ 000320 RAMLAL v MEHTA E21 9990 1965 07 07 FIELDREP 16 8 pswacc 000330 WING LEE E21 2103 1976 02 23 FIELDREP 14 BA DSNRGCOL 000340 JASON R GOUNOT E21 5698 1947 05 05 FIELDREP 16 5 9 D5N8710 000042 Arthur Dent 200010 DIAN J HEMMINGER A00 3978 1965 01 01 SALESREP 18 Gj Tables 200120 GREG ORLANDO A00 2167 1972 05 05 CLERK 14 H DSN8710 ACT 200140 KIM N NATZ co1 1793 1976 12 15 ANALYST 18 DSN8710 DEPT 200170 KIYOSHI YAMAMOTO D11 2890 1978 09 15 DESIGNER 16 DSN8710 EACT 200220 REBA K JOHN D11 0672 1968 08 29 DESIGNER 18 2 2 DSN8710 EDEP 200240 ROBERT M MONTEVERDE D21 3780 1979 12 05 CLERK 17 4 DSNB710 EEMF 200280 EILEEN R SCHWARTZ Ell 8997 1967 03 24 OPERATOR 17 4 A DSN871 ED A 200310 MICHELLE F SPRINGER E11 3332 1964 09 12 OPERATOR 12 a HELENA WONG E21 1976 02 23 FIELDREP garai DSN9710 EMP FA
266. ences Logout Install New Application User ID ceit11 Allows installation of Enterprise Applications and Module Servers E Applications 7Step 1 Provide options to perform the installation Enterprise Applications Install Nevv Application Resources Security Pre compile JSP r Environment Directory to Install Application System Administration Specify the various options available to prepare and install your application AppDeployment Options Enable Distribute Application IV Troubleshooting Use Binary Configuration r Deploy EJBs r Application Name 5G24 B435 static war Create MBeans for Resources iv Enable class reloading r Reload Interval Cancel Figure 15 16 Install new a application 3 Same thing for this window Figure 15 17 Just accept the defaults by clicking Next Z WebSphere Administrative Console Microsoft Internet Explorer Fie Edit View Favorites Tools Help do Back gt 0 AA Search Favorites EfMedia 4 a itso ibm com 9080 admin secure logon do WebSphere Application Server Administrative Console Version 5 Home Save Preferences Logout User ID Install New Application cel111 Allows installation of Enterprise Applications and Module Servers Applicat E Applications Step1 Provide options to perform the installation Enterprise Applications matal New Apclcafion Step 2 Map virtual hosts for web modules
267. ensing IBM Corporation North Castle Drive Armonk NY 10504 1785 U S A The following paragraph does not apply to the United Kingdom or any other country where such provisions are inconsistent with local law INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THIS PUBLICATION AS IS WITHOUT WARRANTY OF ANY KIND EITHER EXPRESS OR IMPLIED INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF NON INFRINGEMENT MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE Some states do not allow disclaimer of express or implied warranties in certain transactions therefore this statement may not apply to you This information could include technical inaccuracies or typographical errors Changes are periodically made to the information herein these changes will be incorporated in new editions of the publication IBM may make improvements and or changes in the product s and or the program s described in this publication at any time without notice Any references in this information to non IBM Web sites are provided for convenience only and do not in any manner serve as an endorsement of those Web sites The materials at those Web sites are not part of the materials for this IBM product and use of those Web sites is at your own risk IBM may use or distribute any of the information you supply in any way it believes appropriate without incurring any obligation to you Information concerning non IBM products was obtained from the suppliers of those produ
268. ent one someone may have to do this for you as access to PROCLIB is usually restricted Example 5 4 on page 67 shows the cataloged procedure we use After entering all values described above we press Enter Figure 5 6 appears Application Environment Notes Options Help Application Environment Selection List Row 1 to 17 of 21 Command gt Action Codes 1 Create 2 Copy 3 Modify 4 Browse 5 Print 6 Delete Menu Bar Action Application Environment Name Description Le BARTSRV J2bEE Application Server for Bart Zz BBOASR1 WAS IVP Server BBOASR2 J2EE Application Server CBINTFRP WAS Interface Repository Server gt CBNAMING WAS Naming Server CBSYSMGT WAS System Management Server M DB7YJCC DB7Y all JCC SPs DB7YJSPP db7y DSNTJSPP SPB m DB7YREXX For Rexx SPs DSNTPSMP DSNTBIND DB7YSPB2 DSNTPSMP proc for V1 15 update DB7YSQL Execution proc for SQL SPs n DB7YUTIL DB7Y stored proc Utility PKINTFRP PKI WAS Interface Repo Server PKNAMING PKI WAS Naming Server Figure 5 6 DB7YJCC environment now created The Application Environment Selection List is displayed showing our new environment DB7YJCC Now we click Save PF3 normally multiple times until we get back to the panel displayed in Figure 5 7 on page 66 Chapter 5 Setup 65 File Utilities Notes Options Help Functionality LEVELO11 Definition Menu WLM Appl LEVELO13 Command gt Definition data set none Definition na Specify
269. equester AR The source of a request to a remote RDBMS the system that requests the data RMI Remote Method Invocation See Remote Method Invocation Rollback The process of restoring data changed by SQL statements to the state at its last commit point All locks are freed Contrast with commit RPC See Remote Procedure Calls RRSAF Recoverable Resource Manager Services attachment facility which is a DB2 UDB for OS 390 subcomponent that uses OS 390 Transaction Management and Recoverable Resource Manager Services to coordinate resource commitment between DB2 UDB for OS 390 and all other resource managers that also use OS 390 RRS in an OS 390 system Runtime system The software environment where compiled programs run Each Java runtime system includes an implementation of the Java Virtual Machine Sandbox A restricted environment provided by the Web browser in which Java applets run The sandbox offers them services and prevents them from doing anything naughty such as doing file I O or talking to strangers servers other than the one from which the applet was loaded The analogy of applets to children led to calling the environment in which they run the sandbox Scalar function An SQL operation that produces asingle value from another value and is expressed as a function name followed by a list of arguments enclosed in parentheses See also column function Secure Socket Layer SSL SSL is a security protocol which allo
270. er configuration you either see a generic built in error message or the original error message from the Web server To display the server error message open Internet Explorer outside WSAD and select Tools gt Internet Options Switch to the Advanced tab and uncheck Show friendly HTTP error messages We feel the friendly message is rather unfriendly at least if you are a Web developer El servers de HB x Server Server State The server should be republished The server should be restarted Quick Edit Tasks Colors Servers Console Figure 15 4 Server view indicating that the server should be restarted Run the Servlet one more time by clicking the Refresh button in the Web browser This time we receive another error message indicating that no default context has been set up for SQLJ The browser window displays Error 500 Found null DefaultContext Again you can see the complete error message and stack trace by inspecting the WebSphere log in the Console view Example 15 7 Example 15 7 Sample error message from console 10 26 03 23 12 59 852 PST 3228927e WebGroup I SRVEO180I SG24 6435 Web SG24 6435 Servlet LOG EmployeeListServlet init 10 26 03 23 13 00 784 PST 3228927e WebGroup E SRVE0026E Servlet Error found null connection context java sql SQLException found null connection context at sqlj runtime error Errors raiseError Errors java 125 Chapter 15 Using Servlets to ac
271. erty This is an actual database name and its not the log The JDBC connectivity type of a data source If you want to use type 4 driver The TCP IP address or host name for the DRDA server If custom property dri portNumber The TCP IP port number where the DRDA server resides IF custom property d i enableSoLJ This value is used to indicate whether SOLJ operations may be performed ta 4 Name databaseName Type java lang String Required Yes Value DB Y Description This is a required property This is an actual database name and its not the locally catalogued database n lt Back Next Cancel Figure 15 9 Create a Data Source 2 9 We are now back in the Data Source tab of the server configuration The final configuration step is to add a default user and password for the data source We decide to define it as a resource property Beside the Resource properties list click the Add button and fill out the dialog as shown in Figure 15 10 Repeat this step for the password property x x Name user Mame password Type fiavalang string Type fiavalang string Value bart Value Se n 1 Description Po Description NENNEN Required z ca Figure 15 10 Add userName and password properties to the data source configuration 10 Save the server configuration select File gt Save or press Ctrl S 11 Restart the server see page 257
272. ervlet code Example B 6 EmployeeDetailServlet package com ibm itso sg246435 web import java io IOException import java io PrintWriter added import javax servlet Servlet import javax servlet ServletException import javax servlet http HttpServlet import javax servlet http HttpServletRequest import javax servlet http HttpServletResponse import com ibm itso sg246435 sqlj Employee added version 1 0 author public class EmployeeDetailServlet extends HttpServlet implements Servlet private static Employee Ctx ctx Appendix B Source code of sample programs 307 see javax servlet http HttpServlet void javax servlet http HttpServletRequest javax servlet http HttpServletResponse public void doGet HttpServletRequest req HttpServletResponse resp throws ServletException IOException PrintWriter out resp getWriter String empno 000130 String empno req getParameter empno out println DOCType HTML PUBLIC W3C DTD HTML 4 01 Transitional EN gt out printIn html out printIn lt head gt lt title gt Employee detail lt title gt lt head gt out printIn body out printIn table try ctx new Employee Ctx Employee emp Employee findByPrimaryKey ctx empno out println tr img src EmployeePicServlet empno empno gt lt tr gt printRow out Serial emp getEmpNo printRow out Name emp getLastName
273. es gt The three different technology editions for the Java 2 platform Java 2 Platform Enterprise Edition Java 2 Platform Standard Edition J2SE and Java 2 Platform Micro Edition J2ME gt The J2EE specification and the technologies that are defined within including the JDBC specification used to access DB2 for OS 390 and z OS databases gt The features of the Java 2 programming language looking at issues such as object oriented coding the significance of Java bytecode and the Java Virtual Machine gt The different ways in which Java applications can be designed developed and executed by looking at the Java application components Copyright IBM Corp 2003 All rights reserved 9 2 1 The three technology editions for the Java 2 platform The Java 2 platform has been separated into three editions Each of these editions focuses on a specific architecture and contains specifications for the technologies required to fulfill these The three editions are gt Java 2 Platform Micro Edition J2ME Defines the technologies required for the highly optimized environment of consumer products such as mobile phones car navigation systems and pagers gt Java 2 Platform Standard Edition J2SE Provides the base environment for developing and executing Java applications and applets It includes the Java Development Kit and Java Runtime Environment By its nature it is focused on development of code that is executed clien
274. es the WebSphere old ConnectionManager architecture All the EJB1 x modules must use this data source Figure 5 35 Select data sources A JDBC Providers gt DB2 JccT2 zOS JDBC Provider gt Data Sources screen with no data sources is displayed On this screen select New to create a new datasource Figure 5 36 3 WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Back gt 9 A Qsearch Favorites media lt 4 F gt 5 ig Address r3 http wtsc63 itso ibm com 9080 admin secure logon do is L 3 WebSphere Application Server Administrative Console P e E Version 5 e T Home Save Preferences Logout Help User Mk ibert JDBC Providers gt DB2 JCC T2 20S JDBC Provider gt cel141 Data Sources Servers k um Data Source is used by the application to access the data from the database amp data source is created under a JDBC Applications provider which provides the specific JDBC driver implementation class i E Resources JDBC Providers Generic JMS Providers WebSphere JMS Provider WebSphere MQ JMS Provider Mail Providers Resource Environment Provida URL Providers Resource Adapters Security Figure 5 36 Create new data source Total 0 Fitter Preferences Delete Description v This takes you to the JDBC Providers
275. et session 6 4 Running a Java program from a Windows command prompt Another option is to run the program from a Windows command prompt If you have WSAD there is absolutely no point in doing so but just out of curiosity and for those people that have to do without we also show how to run our Hello JDBC program using commands 6 4 1 Compile the Java program javac If you have WSAD and just want to try this you can use the same export technique we used in Exporting via FTP on page 115 but choose the File system option instead of FTP see also Doing it yourself Manual program preparation for static SQLJ on page 166 to get the data to the local file system on your machine for example into a directory called c TestJDBC SG24 6436 To compile a Java program and turn it into byte code we use the javac command from a Windows command prompt javac com ibm itso sg246435 jdbc Hello java Make sure that you have set up your environment as described in DB2 Universal Driver Setup for a Windows environment on page 71 We have to specify the same structure defined by the Java package in our source for the program to compile correctly and turn it into a Java class 6 4 2 Run the Java program java To run a Java program in a JVM we use the java command java com ibm itso sg246435 jdbc Hello Note that we do not specify the extension of the program class this time The result should be identical to the execution of the sam
276. etString FIRSTNME BigDecimal salary rs getBigDecimal SALARY The second form is slightly slower because the JDBC runtime ultimately refers to columns by index and has to look up the index for the column first if you referred to it by name However maintenance is easier since you do not need to change anything if you add or remove a column from the SELECT list Note that if a column in the SELECT list is not the name of a table column but an expression for example the result of a scalar function such as TRIM or of a column function such as MAX DB2 generates a default column name COL1 COL2 and so on Obviously it would be a bad idea to refer to these columns by their generated name To override the default column name you should use a correlation name for the column using the AS clause PreparedStatement stmt conn prepareStatement SELECT MAX SALARY AS MAXSALARY FROM DSN8710 EMP Table 6 1 compares the various getXxx methods of ResultSet with the DB2 column types indicating whether the method can be used to retrieve a column of that type The preferred method or methods are indicated in boldface Table 6 1 DB2 column types and ResultSet getXxx methods VARCHAR dr ees BIENES EIE Gass Errem Chapter 6 Getting started with JDBC 107 getFloat getDouble getBigDecimal getBoolean getString getBytes getDate getTime getTimestamp getAsciiStream getUnicodeStream cz ES ae ee EEESNEBLDDU
277. ets you graphically analyze the access paths that DB2 chooses which eliminates the need to manually interpret the plan table output You can download Visual Explain at http www ibm com software data db2 0s390 db2ve 13 3 5 Rebind packages regularly You should rebind your packages after administrative tasks that may affect program performance such as a REORG or the creation of a new index or after a significant amount of INSERT UPDATE activity 13 4 System level performance tuning This section briefly summarizes several system level performance recommendations collected by IBM Silicon Valley Lab experts For a more in depth discussion of the various DB2 parameters that influence dynamic SQL performance see Squeezing the Most Out of Dynamic SQL with DB2 for z OS and OS 390 SG24 6418 13 4 1 Tune the JVM heap size In a Java database workload using either JDBC or SQLJ to access relational data a lot of Java objects are created and then destroyed The JVM heap size plays an important role in overall Java application performance The default initial heap size is 1 MB and the default maximum heap size is 8 MB In almost all cases these default sizes are not sufficient and lead to poor performance Studies at the IBM Silicon Valley Lab and at customer installations have shown that setting the heap size and the maximum heap size to an equal large value increases the throughput dramatically This is because scanning for garbage collectio
278. finition Before we begin it is important to note that if you use both the old DB2 390 driver and the new DB2 JCC driver under WAS for Z OS you need to ensure that DB2 JDBC providers associated with these two drivers are not inter mixed in the same server A given server may only have DB2 JDBC providers of a single type Either the DB2 390 JDBC driver type or the DB2 JCC JDBC driver type Because of this you should restrict all DB2 JDBC provider defintions for these two drivers to a scope of server so you can better manage the DB2 providers to ensure they are not inter mixed in a server The reason for this restriction is that there are identical class names in both drivers To use the new DB2 JCC JDBC driver you need to create a new JDBC provider that is associated with the new driver To do so using the WAS Administrative Concole go to Resources left pane JDBC Providers Figure 5 29 E WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Back gt Q A Bsearch Favorites media lt 4 B G ig WebSphere Application Server Version 5 i E Home Save Preferences Logout Help User ID bart ceti WebSphere Application About your WebSphere Servers Server on IBM com Application Server Applications F IBM WebSphere Application Server for z OS 5 0 0 Build Number Vv501000 Build Date 9415 03 E Resources JDBC um Gene NMS Pro
279. flow control and does not guarantee the reliability of the physical network interpreter A tool that translates and executes code line by line Intranet A private network inside a company or organization that uses the same kinds of software that you would find on the Internet but that are only for internal use As the Internet has become more popular many of the tools used on the Internet are being used in private networks for example many companies have Web servers that are available only to employees IP See nternet Protocol JAR file format JAR Java Archive is a platform independent file format that aggregates many files into one Multiple Java applets and their requisite components class files images sounds and other resource files can be bundled in a JAR file and subsequently downloaded to a browser in a single HTTP transaction Java An object oriented programming language for portable interpretive code that supports interaction among remote objects Java was developed and specified by Sun Microsystems Incorporated The Java environment consists of the JavaOS the Virtual Machines for various platforms the object oriented Java programming language and several class libraries Java applet A small Java program designed to run within a Web browser It is dowloadable and executable by a browser or network computer Java beans Java s component architecture developed by Sun IBM and others The components called J
280. for a Windows environment DB2 UDB for Linux Unix and Windows JDBC support is provided as part of the Java Enablement option on DB2 clients and servers With this support you can build and run Chapter 5 Setup 71 72 JDBC applications and applets These contain dynamic SQL only and use a Java call interface to pass SQL statements to DB2 DB2 Java embedded SQL SQLJ support is provided as part of the DB2 AD Client With DB2 SQLJ support in addition to DB2 JDBC support you can build and run SQLJ applets and applications These contain static SQL and use embedded SQL statements that are bound to a DB2 database The SQLJ support provided by the DB2 AD Client includes gt The DB2 SQLJ translator sqlj which replaces embedded SQL statements in the SQLJ program with Java source statements and generates a serialized profile that contains information about the SQL operations found in the SQLJ program gt The DB2 SQLJ profile customizer db2sgljcustomize which precompiles the SQL statements stored in the serialized profile customizes them into runtime function calls and by default generates a package in the DB2 database gt The DB2 SQLJ profile binder db2sqljbind which generates packages from a previously customized SQLJ program gt The DB2 SQLJ profile printer db2sqljprint which prints the contents of a DB2 customized version of a profile in plain text The IBM DB2 Universal Driver for SQLJ and JDBC was first shipped when DB
281. for each attribute we want to display Do not forget to include the import com ibm itso sg246435 sqlj Employee statement for the Employee class 15 5 Creating the EmployeePic Servlet Finally we create a Servlet to display the employee s picture As in Creating the EmployeeDetail Servlet on page 263 we hardcode the employee number for the time being Again create and test the Servlet as described in Creating the EmployeeList Servlet on page 251 The EmployeePicServlet doGet method is shown in Example 15 9 Example 15 9 EmployeePicServlet doGet first version package com ibm itso sg246435 web import java io IOException import java io InputStream import java io QutputStream import javax servlet Servlet import javax servlet ServletException import javax servlet http HttpServlet import javax servlet http HttpServletRequest import javax servlet http HttpServletResponse import com ibm itso sg246435 sqlj Employee version 1 0 author xf public class EmployeePicServlet extends HttpServlet implements Servlet see javax servlet http HttpServlet void javax servlet http HttpServletRequest javax servlet http HttpServletResponse private static Employee Ctx ctx public void doGet HttpServletRequest req HttpServletResponse resp throws ServletException IOException resp setContentType image bmp 1 OutputStream out null InputStream pic null String empno 000130 2
282. for standard output and standard error 6 Invoke TSO and run the ocopy program to copy the standard output and standard error HFS files to a SYSOUT dataset When the step completes successfully the HFS files will be deleted Make sure your profile is set up correctly and includes the place where the Hello program is located in the CLASSPATH Note that JCL restricts the length of the PARM positional parameter to 100 characters so the combined JVM executable name main class name and arguments cannot exceed 95 characters 100 minus length of SH and three blanks DB2 for z OS and OS 390 Ready for Java The SQLJ program preparation process In the previous chapter we wrote our first SQLJ application and executed it We limited the discussion to the steps you need to perform without getting into the details of why these steps are required and what the SQLJ statements really mean This chapter explains why we had to perform the steps we did It provides a discussion of the SQLJ program preparation process that is the steps that have to be performed in order to precompile an SQLJ file to customize the generated profiles and to bind the generated packages against the database The following chapter Chapter 10 SQLJ tutorial and reference on page 175 deals with what all the SQLJ language constructs mean Copyright IBM Corp 2003 All rights reserved 149 9 1 Program preparation in other languages 150 In order to
283. function Web servers are sometimes referred to as httpd servers or daemons A number of Web servers are available for most platforms including most UNIX variants OS 2 Warp OS 390 and Windows NT In addition commercial Web servers that offer higher levels of vendor support and additional function are available IBM has released the IBM Internet Connection Secure Server ICSS and its follow on the Domino Go Web Server DGW for the AIX OS 2 Warp Windows NT and OS 390 platforms WebSphere WebSphere is the cornerstone of IBM s overall Web strategy offering customers a comprehensive solution to build deploy and manage e business Web sites The product line provides companies with an open standards based Web server deployment platform and Web site development and management tools to help accelerate the process of moving to e business World Wide Web A network of servers that contain programs and files Many of the files contain hypertext links to other documents available through the network WWW See World Wide Web XML The Extensible Markup Language XML is an important new standard emerging for structured documents on the Web XML extends HTML beyond a limited tag set and adapts SGML making it easy for developers to write programs that process this markup and providing for a rich more complex encoding of information The importance of XML is indicated by support from companies such as Microsoft and Netscape Glossary 3
284. ger interface the names of both the driver and database are coded directly into the method calls within the application code Using a DataSource object instead of DriverManager removes this direct coding of driver and database names from the application This information is stored in an external object that is set up and administered independently from the application that the application accesses through the use of a logical name The differences between both interfaces are where the data source definitions can be defined and the levels of portability that it provides A thing to remember is that the term database means different things on different platforms With DB2 for z OS and OS 390 when you are connecting to a database you are actually connecting to a whole DB2 subsystem or a data sharing group which will contain multiple databases Depending on the security within that subsystem when you connect you can potentially access all databases tables and views in that subsystem With a DB2 for Windows or Unix database you are connecting to a specific database and you will be able to only access the tables and views within that database This is illustrated in Figure 3 9 DB2 for z OS and OS 390 Ready for Java CONNECT TO DB2T CONNECT TO DBAPP1 SELECT NAME SELECT NAME FROM TEST EMP v FROM TEST EMP v SELECT NAME SELECT NAME FROM TEST BRANCH FROM TEST BRANCH x SELECT NAME SELECT NAME FROM PROD EMP
285. ges developed by Netscape to simplify dynamic Web site development As JavaScript operates as an extension of HTML it is completely executed on the client under the control of the browser JavaScript code is interpreted by the browser and is not compiled or executed under the JVM 2 4 6 Java Beans The Java Beans API defines a software component model for Java to support the development of applications using visual builder tools The most obvious use of Java Beans is the development of GUI applications where you use a tools palette to drag and drop visual components such as buttons text entry fields and list boxes onto a free form surface You then set up the component s properties such as a button s text and connections between the components for example to enable or disable a button depending on whether or not the user entered some text in a text entry field 2 4 7 Enterprise Java Beans Despite the similar name Enterprise Java Beans EJBs have a very different focus compared to Java Beans While Java Beans are essentially client side components EJBs set a platform independent standard for the development of server side business logic components Using a set of classes that conform to the EJB standard a developer can create assemble and deploy component objects that implement the business logic of the enterprise Once deployed the component is managed by the component transaction monitor CTM which looks after the infras
286. getXxx methods of ResultSet return a value of primitive type namely those for database types that map to Java primitive types The problem arises how can you tell that the column you want to examine actually has a value or is NULL Note If the column was NULL the getXxx methods return zero for numeric primitive types or false in the case of getBoolean Still you cannot know if the column s value was indeed zero or was NULL There are two possible solutions Either you can use the method ResultSet getObject which returns an object of the closest matching type For example when the column is a SMALLINT column you would retrieve an instance of java lang Short or nu11 if the column was NULL The other possibility is to call ResultSet wasNu11 right after retrieving the column with for example ResultSet getShort If the column was NULL the wasNu11 method returns true and the return value from getShort is zero otherwise it returns false 7 3 Examining result sets Suppose that you develop an interactive application that allows the user to construct a database query quickly and you want to present the results of the query nicely formatted In this case you do not know the number width and types of the columns in the result set This is where the ResultSetMetaData API comes into play It has one method taking no parameters getColumnCount which returns the number of columns in the result set All ot
287. guration when running your WebSphere Application Server WAS on z OS Your Java application can run under the control of WAS and talks to DB2 through the Type 2 JDBC driver and a local RRS attachment 3 2 2 Using the Type 4 driver to talk to a local DB2 for z OS and OS 390 In this configuration Figure 3 4 on page 31 we use a Type 4 driver to connect to a local DB2 for z OS and OS 390 subsystem through DRDA and DDF Note that the application and the DB2 system are running on the same z OS image LPAR 30 DB2 for z OS and OS 390 Ready for Java z OS image Figure 3 4 Local application using Type 4 driver This is not a recommended setup for a production environment as all SQL requests go through DRDA over TCP IP so through the TCP IP stack into DB2 s DDF address space Even though DRDA is a very efficient database communication protocol it is normally more expensive than going through the local RRS attachment that we discussed in the previous section The main reason why this configuration is shown here is that at the time of writing of this publication the new JCC Type 2 driver was not available for DB2 for z OS so we used the Type 4 driver during most of our samples For testing purposes like our examples this configuration is OK As the functionality of the Universal Driver is almost identical for the Type 2 and Type 4 driver you only need to change the URL to switch between both driver types and you are done
288. h Configurations dialog 111 6 5 Output from running the Hello program 0000 cece eee ee 112 6 6 Accessing the HFS file system as a shared network drive llus 114 6 7 Export to file system wizard llle eh 115 6 8 Export to FTP Server wizard 0 2 0 0 ccc e 116 6 9 Running the program in a USS telnet session 0 0000 cee eee eee 117 6 10 Compiling and running a JDBC program from the command prompt 118 6 11 Remote debugging architecture 1 0 2 0 ee 119 6 12 Launch configuration for remote debugging eee ee eee 120 8 1 Create a new SQLJ file llli tee 142 8 2 Create a new SQLJ file 2 scies eee 143 8 3 Error message from running the Hello program llis else 146 8 4 Launch Configurations dialog s es sasaaa aaea ee 146 8 5 Output from running the Hello program liiis eese eene 147 9 1 Program preparation in COBOL 0 00 ccc eee 151 9 2 The SQLJ program preparation process OvervieW 0000 c eee eee 152 9 8 SQLJ customization script properties 2 0 0 ee 159 9 4 Sqlj project properties 0 2 cee 160 9 5 Changing the root package names 00 cece eee eee 161 9 6 Setting the user name and password properties liliis eee eee 161 9 7 DB2PE thread SUMMAN rends p llis nn 163 9 8 Currently execution SQL statement lllll iilis eese 164 9 9 Type of DML statements execut
289. h is the method selected by the DB2 optimizer for accessing data from a table When binding the DBRM into a package you also specify the isolation level to be used for that package UR CS RS or RR The result of the bind step is known as a package which is an object in the database it is stored in the DB2 catalog and directory containing all the information from the DBRM file as well as the access path The information in the package includes the consistency token produced in step 1 At runtime DB2 uses the information in the package in order to execute the SQL statements in the program It compares the consistency token in the program with the one in the DB2 catalog in case of a mismatch an error is reported because this indicates that the program had been retranslated precompiled again but the DBRM had not been bound into a package DB2 for z OS and OS 390 Ready for Java COBOL with SQL source file input to produces DB2 precompiler D produces 1 3 COBOL DBRM input to DB2 BIND source file utility input to COBOL Compiler Linker 2 produces uses package Load module po pe ea ea DB2 for z OS Figure 9 1 Program preparation in COBOL The important thing to understand about packages and DBRMs is that packages are needed at runtime whereas DBRMs are not If the bind step is not performed after precompilation the program will fail
290. hat the scope is Node indicated by the red arrow If not select Node and click Apply lt Back A A Asearch Gjravorites meda lt 4 B 5 fw 8 Address Lx http jwtsc 3 itso ibm com 9080 admin secure logon do ge WebSphere Application Server Administrative Console Version 5 we un Home Save Preferences Logout Help User ID bart WebSphere Variables cel111 Substitution variables allow specifying a level of indirection for values defined in the system such as filesystem roots Servers Variables can be defined at the server node or cell level When variables in different scopes have the same name the order of resolution is server variables then node variables then cell variables Li Applications Resources Security Total 37 Page 1 Total Pages 2 Next E Environment E Scope Cell cel111 Node nd111sc63 Update Web Server Plugin Virtual Hosts C Cell cel111 REN settings to limit the availability of resources to a particular cell node or Manage WebSphere Variableg Node ndilisc63 When new items are created in this view they will be created within the current scope Shared Libraries Naming C Server ws111sc63 System Administration Apply Troubleshooting Filter Preferences Sev Delete Mame Value v Scope v amp PP INSTALL ROOT S USER INSTALL ROOT jinstalled amp pps celis cel111 nodes nd1
291. he Servlet model makes it difficult to separate the responsibilities of the Web designer and the Java developer in the development process This in turn complicates the whole development and maintenance process of Servlets 2 4 4 JavaServer Pages JavaServer Pages JSPs are a natural extension of the Java Servlet They came about to make the separation of responsibilities between the Web designer and Java developer more distinct They also allow for a more dynamic method of change for dynamically generated Web pages JSPs achieve this by reversing the concepts of the Servlet In the Servlet hardcoded HTML is slotted in between the logic of a Java program A JSP is a text document made up of static HTML and XML like tags and scriptlets that encapsulate the logic that generates the dynamic content One of the significant features of a JSP is that it is translated into Java Servlet code which is then compiled using the standard Java compiler before it is executed in the JVM The JSP is stored in a section of the Web server called the JSP container This container manages the translation and compilation of the page into the Servlet It checks whether a translation of the page is required due to the page being updated since the last execution If it has not been changed the Servlet is called directly Although the JSP came about to rebalance the development of Java Servlets towards the Web designer by making it an HTML page surrounding the Java pr
292. he actual reason for the error 7 25 03 22 06 01 871 CEST 350b62f8 WebGroup E SRVE0026E Servlet Error EmployeeListServlet java lang NoClassDefFoundError sq1j runtime ref ResultSetIterImpl at java lang Class newInstanceO Native Method at java lang Class newInstance Class java 262 at java beans Beans instantiate Beans java 233 at java beans Beans instantiate Beans java 77 DB2 for z OS and OS 390 Ready for Java at com ibm ws webcontainer webapp WebAppServ etManager 1 oadServ1et WebAppServletManager jav a 188 This indicates that the SQLJ runtime library is not on the server s classpath To fix this do the following 1 Switch to the Servers view select the Servers tab in the bottom window 2 Double click the WebSphere v5 0 Test Environment entry An editor opens that allows you to modify the server s configuration 3 In that editor select the Environment tab then press Add External JARs 4 Navigate to your DB2 installation directory and select db2jcc jar and the license file db2jcc license cisuz jar Press Open 5 Save the server configuration select File gt Save or press Ctrl S 6 Since its configuration has changed the server should be restarted as indicated in the Servers view see Figure 15 4 Do so by selecting Restart from the context menu The server shuts down then restarts Tip The browser window in WSAD for Windows is an embedded Internet Explorer Depending on your Internet Explor
293. he definition of a referential constraint Because of the foreign key the table is a dependent table The key must have the same number of columns with the same descriptions as the primary key of the parent table FTP See File Transfer Protocol Function A specific purpose of an entity or its characteristic action such as a column function or scalar function See column function and scalar function Furthermore functions can be user defined built in or generated by DB2 See built in function cast function user defined function external function sourced function Garbage collection Java s ability to clean up inaccessible unused memory areas garbage on the fly Garbage collection slows performance but keeps the machine from running out of memory Hierarchy The order of inheritance in object oriented languages Each class in the hierarchy inherits attributes and behavior from its superclass except for the top level Object class HTTPS HTTPS is a de facto standard developed by Netscape for making HTTP flows secure Technically it is the use of HTTP over SSL Hypertext Markup Language HTML A file format based on SGML for hypertext documents on the Internet Allows for the embedding of images sounds video streams form fields and simple text formatting References to other objects are embedded using URLs enabling readers to jump directly to the referenced document Hypertext Transfer Protocol HTTP The Inter
294. he employee number Other than that the update method looks pretty similar to the insert method again we use the conditional operator to convert the boolean attribute to an M or P Example 11 13 Employee update method public void update Ctx ctx throws SQLException sql ctx UPDATE DSN8710 EMP SET FIRSTNME firstName gt LASTNAME lastName gt MIDINIT middleInitial HIREDATE hireDate gt SEX male M F gt SALARY salary WHERE EMPNO empNo If you for example want to change the last name after retrieving the employee you can add the following code Figure 11 14 to your EmployeeTest program Example 11 14 Invoking the Employee update method Employee emp Employee findByPrimaryKey 000042 System out printIn Successfully retrieved emp emp setLastName Dentist emp update ctx System out println Successfully updated emp Chapter 11 SQLJ revisited 211 11 2 9 Implementing the findAIl method Next we need a method to retrieve all employee records from the table Again since we do not necessarily have an Employee object to work with it is going to be a static method of the Employee class Example 11 15 Because we retrieve multiple records we need a way to iterate through them one at a time The findAll method therefore returns an iterator which the caller can pass to the fetch method described later It is the caller s responsi
295. he functionality of the browser Also as they are browser based they tend to be graphical in nature although they can be used to access and execute all features of a browser such as playing audio files and processing user input As applets are client based applications they are restricted in what they can do to the client machine executing them The biggest restriction is that an applet cannot access the client s hard drives to either read or write data This is to protect the client from intrusions from applications which they may or may not know that are loaded from the Web Java does have a method to allow applets to perform disk access It is called applet signing This verifies that the applet comes from a certified source and it is up to the user to decide whether he trusts that source enough to give the applet access to his or her computer s resources Applets are an extension of the Applet class and use different methods to allow them to react to the states of the host browser page The four main methods of the applet are shown in Table 2 1 Table 2 1 Methods of a Java applet L I ______ se Performs the initialization of the applet when it is first executed This is used to set the layout of the applet and is always overridden Called whenever the applet on the browser comes into view Also called after the init is executed This method can be overridden if you wish to start running expensive processes that only need to run
296. he library exactly like the built in tags that are part of the JSP core language Although custom tag libraries are a relatively new feature of JSP there are already quite a number of custom tag libraries around which you can use for things like Formatting strings dates and timestamps Internationalization Sending e mail Processing XML documents Accessing databases vvvvy WSAD comes with several tag libraries that are ready to use We will use a tag library for database access in this chapter rewriting our employee list application to use JSPs Note WSAD comes with a powerful wizard that allows you to automatically create JSPs for database access However in this section we will code the JSPs manually in order to help you better understand what is going on If you want to experiment with the Database Web Pages wizard you can find it under File gt New gt Web gt Database Web Pages 16 2 Creating the EmployeeList JSP 280 First of all we have to tell WSAD that our Web application wants to use one of the tag libraries that come with the product 1 Open the Properties page for the SG24 6435 Web project 2 Select the Web Project Features category 3 Check Tag libraries for database access see Figure 16 1 on page 281 DB2 for z OS and OS 390 Ready for Java 4 Click the Apply button This may take a while to complete Then click OK amp Properties for SG24 6435 Web i Ant xl Info BeanlInfo Path Extern
297. he list box in the lower half of the panel now shows all the SQLJ profiles in the project and the corresponding package root name that has been assigned to that profile Since the default root name Hello 0 is somewhat arbitrary you should change it to HELLO see Figure 9 5 on page 161 The changes will be reflected in the sglj project properties file which you also could have changed manually instead of using the properties dialog Then click OK DB2 for z OS and OS 390 Ready for Java Properties for SG24 6435 Jol x Info BeanInfo Path External Tools Builders URL idbcidb2 jwtsce3 itso ibm com 33756 DB7Y Java Build Path Java Compiler sere Java JAR Dependencies Pasenadi Javadoc Location Java Task Tags SQLJ Customization Script Project References Options COLLECTION 5G246435 Server Preference SQLJ Customization Script SOLJ Profile Package Root Name Validation com ibm itso sg246435 sqlj Hello SJProfile HELLO M Figure 9 5 Changing the root package names Running the customization script Now we are ready to run the customization script Right click the script sqlj customize xml and select Run Ant In the dialog that opens switch to the Properties pane Here we configure the database username and password Using the Add button add two properties called db user and db password and their respective values Figure 9 6 4 SG24 6435 sqlj customize xml x Modify attribute
298. he program to stop at the first executable statement in the main method when started in debug mode To start the program in debug mode select the drop down beside the Debug icon BIEI and pick the launch configuration for the program you want to debug When the program hits the breakpoint program execution is suspended and WSAD switches to the Debug Perspective The current line in the source file is highlighted Now you can use the icons in the Debug view toolbar to control the program s execution gt gt Resume Continues execution until the next breakpoint is hit gt J Suspend Suspends a running program or thread gt E Terminate Terminates a running program or thread gt Step into Steps into the current statement gt Step over Steps over the current line DB2 for z OS and OS 390 Ready for Java gt BE Step out Steps out of the current method 6 6 Remote debugging Both the Java Virtual Machine on z OS and WSAD support Java Platform Debugger Architecture JPDA which is a Java standard for remote debugging It allows you to connect via TCP IP to a Java Virtual Machine running on a remote computer provided that the JVM had been started with remote debugging enabled Figure 6 11 depicts the JPDA architecture When the JVM starts up in debug mode it creates a Debug listener thread This thread listens on a TCP IP port and receives debug control instructions in a standard format called Java Debug Wire Prot
299. her methods expect as parameter a column index starting with 1 For the indicated column you can retrieve gt The column s name gt The name of the table this column came from useful if it was a JOIN of several tables gt The column s type one of the constants defined in java sql Types Chapter 7 JDBC revisited 123 gt The column s nullability whether or not it may contain NULL values Several other pieces of information about the column for example whether or not it is signed We demonstrate some of these methods in A complete example Poor man s SPUFI on page 130 7 4 Database metadata As mentioned in before the JDBC API allows applications to retrieve database metadata These metadata fall into three broad categories gt Information about the JDBC driver Driver name major and minor version gt Information about the database server SQL level supported handling of NULL values etc gt Information about database objects Number names and types of columns in a table 7 4 1 Information about the JDBC driver For diagnostic purposes you can retrieve the JDBC driver s name and version using the methods getDriverName getDriverVersion getDriverMajorVersion and getDriverMinorVersion in the DatabaseMetaData interface 7 4 2 Information about the database server 124 The DatabaseMetaData interface declares a whole barrage of methods to retrieve information about various aspects of the data
300. hod above making the code easier to read and maintain Example 15 4 The EmployeeListServiet printRow method Print employee data to the servlet output stream in an HTML table row param out The servlet output stream param emp The employee to be displayed private void printRow PrintWriter out Employee emp out printIn tr printCol out emp getEmpNo printCol out emp getLastName printCol out emp getFirstName emp getMiddleInitial out printIn tr Finally we implement a method printTable which iterates through all employees calling the printRow method in turn for each employee Example 15 5 The EmployeeListServlet printTable method private static Employee Ctx ctx Prints the table of employee data Gparam out The servlet output stream exception SQLException A database error occurred ok F HF X private void printTable PrintWriter out throws SQLException out printIn lt table gt ctx new Employee Ctx Employee EmployeeIterator iter Employee findAl1 ctx try Employee emp while emp Employee fetch iter null printRow out emp out printIn table finally iter close Chapter 15 Using Servlets to access DB2 255 Do not forget to add the import statement for java sql SQLException to the program The last thing to add is the invocation of the printTable method to the Servlet s d
301. hod for each column and padding the output with blanks Example 7 12 Example 7 12 Spufi printRow Prints a single row of the result set param rs Result set to process param columnWidths Array of column display widths throws SQLException private void printRow ResultSet rs int columnWidths throws SQLException buf setLength 0 Clear buffer int buflen 0 final int columnCount rs getMetaData getColumnCount for int col 1 col lt columnCount col buflen columnWidths col 1 buf append getColumnValue rs col Truncate buffer if too long a column s value was longer than MAXCOLWIDTH if buf length buflen buf setLength buflen while buf length buflen Pad with blanks buf append log buf toString Print current row DB2 for z OS and OS 390 Ready for Java Finally the getColumnValue method returns an object representation of a column or null if a NULL value was encountered LOB columns get special treatment because we do not want to print or even retrieve the entire LOB column but rather only the first few bytes or characters Example 7 13 Spufi getColumnValue Get a column s value BLOB and CLOB columns are treated separately all other column types use the default representation param rs ResultSet to process param col Column index return The column s value may be code null code if tne column was NULL
302. hods to parse the command line load the JDBC driver and display usage information Example 7 14 Spufi main public static void main String args try int optind eval0ptions args 1 loadDriver 2 Connection conn openConnection conn setTransactionIsolation isolationLevel Spufi spufi new Spufi conn i Add SQL statements supplied from the s command line option if sql length gt 0 spufi addSql sql No filename arguments given if optind args length if sql length 0 No command line SQL statements given as well Read SQL statements from standard input spufi addStream new InputStreamReader System in else Only command line SQL statements Turn auto commit on autoCommitSwitch true spufi setAutoCommi t autoCommi tSwi tch Add files to process while optind args length 4 spufi addFile args optind Ready to go spufi execute conn close catch Exception e e printStackTrace System err Notes on Example 7 14 1 Evaluate the command line options storing relevant information in static variables Remember the index of the first non option item on the command line To parse the command line we use the GNU getopt package not shown here 2 Load the JDBC driver open the connection and set the isolation level the default level may have been overridden by a command line switch 3 Create a Spufi instan
303. htly misleading it is used not only for the SQL UPDATE statement but for each kind of statement that does not return a ResultSet including statements that create or drop objects in the database such as CREATE TABLE and DROP TABLE that are known as DDL statements In this section we cover the three most common statements to find in an executeUpdate namely gt INSERT to add new rows to a table UPDATE to change the value of columns in existing rows DELETE to delete rows from a table The executeUpdate method returns the number of rows affected if it executed an INSERT UPDATE or DELETE statement as explained in the following sections If it executed a DDL statement such as CREATE TABLE it always returns zero 7 1 1 INSERT There are two basic forms of the INSERT statement One to insert a single new row into a table and one to insert several rows that have been SELECTed from another table The first form looks like this INSERT INTO TABLENAME COL1 COL2 VALUES In this case the executeUpdate call always returns 1 When you want to copy data from another table you can use the second form of the INSERT statement INSERT INTO TARGET TABLE COL1 COL2 SELECT S1 S2 FROM SOURCE TABLE Obviously the number of columns in the SELECT clause must match the number of columns to be inserted and the respective columns must have compatible types Here the executeUpdate method returns th
304. ibes the setup necessary to execute the samples for the following products DB2 for OS 390 and z OS V7 Workload Manager WLM Unix System Services DB2 Universal Driver Setup for a Windows environment WSAD setup WAS for z OS V5 data source setup vvvvvy Copyright IBM Corp 2003 All rights reserved 57 5 1 DB2 for OS 390 and z OS V7 Our samples use the employee sample table DSN8710 EMP Make sure you have been granted INSERT SELECT UDPATE and DELETE access or grant access to PUBLIC The DB2 sample database is created by running the DSNTEJ job in the hig SDSNSAMP data set We also ran sample job DNSTEJ7 for use in the LOB samples 5 1 1 Installing DB2 SQLJ JDBC support We assume that DB2 has been installed correctly including the JDBC SQLJ drivers The JDBC SQLJ drivers are installed through SMP E as a separate FMID called JDB7712 for DB2 Version 7 The SMP E installation steps are described in Chapter 6 of Application Programming Guide and Reference for Java SG26 9932 03 As the Java area is a very dynamic area please make sure you have the latest version of this publication It is available from the Web at http www 3 ibm com software data db2 0s390 v7books html 5 1 2 Installing the Universal Driver on a z OS or OS 390 platform At the time of writing of this publication the Universal Driver was not generally available on the z OS and OS 390 platform On the z OS platform the Universal Driver will be pa
305. ibm db2 jcc DB2Driver Class forName driver String url properties getProperty url Connection conn DriverManager getConnection url properties Appendix B Source code of sample programs 303 304 Set up the connection context with autocommit disabled ctx new Employee Ctx conn public static void testInsert throws SQLException Employee emp new Employee 000042 emp setFirstName Arthur emp setLastName Dent emp setMale true emp setSalary new BigDecimal 200000 Wish it were so emp insert ctx System out println Employee emp created successfully public static void testFindAll throws SQLException EmployeeIterator iter null try iter Employee findAl1 ctx for 33 Employee emp Employee fetch iter if emp null iter close break System out printIn emp finally if iter null iter close public static void testGetPicture String empno File dir throws SQLException IOException Employee emp Employee findByPrimaryKey ctx empno InputStream in null OutputStream out null try in emp getPicture ctx if in null System out println No picture available for employee empno else out new FileOutputStream new File dir Emp empno bmp int nread byte buf new byte 1024 while nread in read buf gt 0 out write buf 0 nread System out printIn Pi
306. icially supported Type 2 Here the JDBC API calls platform and database specific code to access the database This is the most common driver type used and offers the best performance However as the driver code is platform specific a different version has to be coded by the database vendor for each platform Chapter 3 Accessing DB2 from Java 25 This type of driver is provided with Version 7 of DB2 for z OS and OS 390 It is available for Version 5 through APAR PQ19814 for Version 6 through APAR PQ36011 and it is part of the base code with DB2 Version 7 and Version 8 when it becomes generally available Both Type 1 and Type 2 drivers are partially written in Java They also require a platform specific client library usually written in C To be able to run a Java application that wants access to a database through a Type 1 or Type 2 driver you need both the driver and the platform specific client library on the client machine This is in contrast to Type 3 and Type 4 drivers They are pure Java drivers which makes them easier to port and to deploy as they do not require a platform specific client library to go with the driver Type 3 With this driver type the JDBC API calls are routed through a middleware product using standard network protocols such as TCP IP The driver itself is written in Java The middleware translates these calls into database specific calls to access the target database and returns data to the calli
307. ictureFile length EmployeeTest java Example B 4 is the program that is used for testing the Employee class listed before We recommend commenting and uncommenting different sections of the program when running it to have better control over its execution Example B 4 EmployeeTest java package com ibm itso sg246435 sq1j import java import java import java import java import java import java File not in here before FileInputStream FileNotFoundException FileOutputStream IOException InputStream Ooooooo import java io QutputStream import java math BigDecimal import java sql Connection import java sql DriverManager import java sql SQLException import java util Properties import com ibm itso sg246435 sqlj Employee EmployeeIterator public class EmployeeTest private static Employee Ctx ctx Load JDBC driver and initialize SQLJ connection context Driver name URL and driver specific properties are read from a configuration file private static void initialize throws ClassNotFoundException SQLException FileNotFoundException IOException Read the properties file FileInputStream propsFile new FileInputStream EmployeeTest properties Properties properties new Properties properties load propsFile propsFile close Get driver property and load the driver String driver properties getProperty driver com
308. in your application Modules can be Environment installed on the same server or dispersed among several servers System Administration Troubleshooting phere cell cel111 node nd111s1 Clusters and Servers Apply Module URI Server G24 5435 SG24 5435 static war WEB JUR vi x M Web INFAveb xml WebSphere cell cel1 11 node nd111sc63 server ws111sc63 Previous Cancel Step4 Summary Figure 15 18 Install a new application 5 The next window Figure 15 19 on page 275 gives you an overview of the options you previously selected Scroll to the bottom of the top window and click Finish 274 DB2 for z OS and OS 390 Ready for Java strative Console Microsoft Internet Explorer File Edit View Favorites Tools Help EJ lt Back gt A A Qsearch yravorites Meda D 5 m4 8 Address http Jwtsce3 itso ibm com 9080 admin secure logon do Go Links WebSphere Application Server Administrative Console Version 5 Home Save Preferences Logout M CX TH Gare STIUUIE pe iken wenen CUI IG iese puey mes ILES auviseu LU use User ID ii the policytool provided by the JDK for editing the policy files I WAS HOME avajre inipolicytool cel111 i Servers grant codeBase file application E Applications X was policy data Enterprise Applications grant codeBase file jars Install New Application X Resources
309. ing return printable representation public String toString return getLastName getFirstName getMiddleInitial public void insert Ctx ctx throws SQLException sql ctx INSERT INTO DSN8710 EMP EMPNO FIRSTNME gt MIDINIT gt LASTNAME gt HIREDATE gt SEX SALARY VALUES empNo firstName middleInitial gt lastName thireDate male M F salary Appendix B Source code of sample programs 299 h public static Employee findByPrimaryKey Ctx ctx String empNo throws SQLException Employee emp new Employee empNo sql ctx SELECT FIRSTNME gt MIDINIT gt LASTNAME gt HIREDATE CASE SEX WHEN M THEN 1 ELSE 0 END gt SALARY INTO emp firstName emp middleInitial emp lastName emp hireDate emp male emp salary FROM DSN8710 EMP WHERE EMPNO empNo h return emp public void delete Ctx ctx throws SQLException sql ctx DELETE FROM DSN8710 EMP WHERE EMPNO empNo h public void update Ctx ctx throws SQLException sql ctx UPDATE DSN8710 EMP SET FIRSTNME firstName LASTNAME lastName gt MIDINIT middleInitial HIREDATE hireDate SEX male M F SALARY salary WHERE EMPNO empNo h sql public static iterator Employeelterator String EMPNO String FIRSTNME String MIDINIT String LASTNAME Date HIREDATE
310. ing application programs conforming to IBM s application programming interfaces Copyright IBM Corp 2003 All rights reserved xix Trademarks The following terms are trademarks of the International Business Machines Corporation in the United States other countries or both AIXG IBM eServer QMF CICS IBM RACF Cloudscape ibm com Redbooks DB2 Universal Database IMS Redbooks logo Ca DB2 Connect iSeries S 390 DB2 Language Environment SAA Domino MVS System 390 DRDA MVS ESA WebSphere e logo server Notes z OS M Encina amp OS 28 zSeries eServer 0S 390 The following terms are trademarks of International Business Machines Corporation and Rational Software Corporation in the United States other countries or both Rational The following terms are trademarks of other companies ActionMedia LANDesk MMX Pentium and ProShare are trademarks of Intel Corporation in the United States other countries or both Microsoft Windows Windows NT and the Windows logo are trademarks of Microsoft Corporation in the United States other countries or both Java and all Java based trademarks and logos are trademarks or registered trademarks of Sun Microsystems Inc in the United States other countries or both C bus is a trademark of Corollary Inc in the United States other countries or both UNIX is a registered trademark of The Open Group in the United States and other
311. into a WAR Web ARchive file This file contains all your code static content configuration information and other resources needed by the application at runtime Then you use the Web Application Server s administrative tools to make the application known to the server To create the WAR file from WSAD do the following 1 Select the Web project SG24 6435 Web 2 Select File gt Export gt WAR file 3 In the Destination field enter S Bart SG246435 static war Chapter 15 Using Servlets to access DB2 269 4 Click Finish The WAR file is now ready for deployment to WebSphere Application Server WAS using the WAS administrative console 15 7 3 Installing a new application on WAS for z OS As mentioned earlier you use the WAS administrative console to manage your WeSphere for z OS environment Log onto the administrative console The WAS admin console uses a Web interface Open a browser session and type in the URL to connect to the admin console application In our case the URL is http wtsc63 itso ibm com 9080 admin A screen similar to Figure 15 13 should appear E IBM WebSphere Application Server Administrative Login Microsoft Internet Explorer File Edit View Favorites Tools Help Bak gt A Bsearch Favorites GyjMeda lt 4 Gv 3 m Address amp http wtsc63 itso ibm com 9080 admin Links amp Search the Web with Google amp IBM BluePages amp IBM Standard Software Installer
312. ion runs on the same LPAR as the DB2 It uses the RRS Attachment Facility RRSAF when connecting to z OS or OS 390 based DB2 subsystems The use of the new Universal Driver also greatly simplifies the development and deployment of SQLJ applications see Preparing SQLJ programs to use static SQL through WSAD on page 157 for details and offers several useful but proprietary extensions which are covered in Chapter 12 The DB2 Universal Driver on page 221 Chapter 3 Accessing DB2 from Java 29 3 2 Different ways to connect to a DB2 for z OS and OS 390 In this section we show some of the commonly used ways to connect a Java application to a DB2 for z OS and OS 390 subsystem For this discussion the type of Java application is not important Therefore when referencing a Java program that can be a Java application an applet a Servlet or an EJB 3 2 1 Direct T2 connection to a local DB2 subsystem This configuration is illustrated in Figure 3 3 In this case the Java application runs under Unix System Services USS and talks to DB2 through a Type 2 driver A JDBC call is translated by the Type 2 driver into SQL statements and executed through the RRS attachment facility by DB2 z OS image UN Figure 3 8 Local T2 connection As the application is local to the DBMS and we use a Type 2 driver that can do native calls to DB2 this configuration normally provides the best performance You also use this confi
313. ions Type 2 and 4 connections may be made simultaneously if desired using this single driver instance Programs select the desired type of connectivity by using different URL syntax when making the connection see Establishing a connection on page 38 for details on how to code the different URLs DB2 for z OS and OS 390 Ready for Java T4 pBotor z OS DB2 for LUW Figure 3 2 Universal Driver architecture DRDA D Direct Tx Driver type Type 4 connectivity The Type 4 driver is based on an open distributed database protocol known as Distributed Relational Database Architecture DRDA This driver connects to remote and local subsystems using DRDA over TCP IP This eliminates the need for DB2 Connect but all servers must be configured for TCP IP communication However for licensing purposes you still need a DB2 Connect to be able to use Type 4 connectivity to a DB2 for Os 390 and z OS Type 2 connectivity For the Type 2 driver the story is a little bit more complicated Connecting to a DB2 for Linux Unix and Windows When the application for example WAS on Windows is running on the same machine as the DB2 UDB database resides the Type 2 driver uses a shared memory connection When the application is running on a different machine the Type 2 driver uses a DRDA connection to get to the database Connecting to a DB2 for z OS The use of a Type 2 driver on z OS or OS 390 assumes that the applicat
314. is can be a big exposure The application may be coded to observe the company s business rules but the interactive user can make any change he or she wants bypassing the company s business rules enforced through the application The use of static SQL solves this problem With static SQL a user can be authorized to run a program containing statements such as UPDATES for which that user himself does not have sufficient privileges In other words the point of control is not persons but programs This is illustrated in Figure 3 11 on page 47 Figure 3 12 on page 47 and Figure 3 13 on page 48 In Figure 3 11 on page 47 user DIANE is a member of the PAYROLL group which has SELECT but no UPDATE privilege on the EMP table Diane wants to run a JDBC application that needs to update the EMP table Her attempt to update the EMP table through the JDBC application will fail since the application runs using Diane s insufficient privileges Diane does however have SELECT privilege on the table since she is a member of the PAYROLL group Therefore she can SELECT from the table using interactive tools such as QMF DB2 for z OS and OS 390 Ready for Java JDBC application UPDATE table UPDATE eve LASTNAME SALARY QUINTANA 23800 00 NICHOLLS 28420 00 000140 000150 25280 00 ADAMSON PAYROLL has SELECT privilege Figure 3 11 Dynamic SQL with SELECT but no UPDATE privil
315. itioned and named iterators respectively Positioned iterators In its simplest and most common form the declaration of a positioned iterator looks like this sql modifiers iterator ClassName type list Where gt modifiers is an optional list of Java class declaration modifiers such as public static final and synchronized gt ClassName is a legal Java class name gt lype listis a comma separated list of Java types which can be both primitive types such as int or reference types such as String For example to declare a positioned iterator for a result set of CHARACTER INTEGER NOT NULL and TIMESTAMP fsql iterator MyPositionedIterator String int Timestamp For each iterator declaration the SQLJ translator generates a corresponding Java class Therefore an iterator declaration is allowed only where declaring a Java class is allowed If you do not specify a modifier the generated iterator class will have default visibility that is it is visible only to classes in the same package If you want to use it in other packages you have to add the public modifier before the iterator keyword fsql public iterator ClassName Type list However the Java language specification mandates that you cannot have two top level that is non nested public classes in the same source file Therefore if you declare your iterator public this declaration must either be contained in a separate source file whose base name must be the
316. iver Bind plan As mentioned before when you use the Type 2 driver you need to have a local plan to execute This applies to both SQLJ and JDBC As you can see we use our own plan BARTSQLJ when using the local Type 2 driver It is bound as shown in Example 5 3 It specifies two collections gt SG246435 that will contain all our SQLJ packages gt NULLID that contains all our JDBC packages Example 5 3 Bind plan BIND PLAN BARTSQLJ PKLIST SG246435 NULLID ISOLATION CS Important It is necessary to include the NULLID collection in the PKLIST of your plan Otherwise executions will fail This is because the JCC driver needs to do some initial setup work before it starts executing you static package To to so it uses a package in the NULLID collection Therefore if that package from the NULLID collection is not part of the PKLIST program execution will fail with a 805 SQLCODE As part of the JDBC installation steps documented in Application Programming Guide and Reference for Java SC26 9932 you also bind a default JDBC plan called DSNJDBC See prefix SDSNSAMP DSNTJJCL job for details You can also use that plan to run your JDBC and SQLJ programs as long as the PKLIST contains all collections and or packages that are needed by the applications 5 2 Workload Manager WLM As the stored procedures used by the DB2 Universal Driver for SQLJ and JDBC are WLM managed stored procedures you have to make sure that your WLM
317. iver for SQLJ and JDBC has the following advantages over the previous solutions Onesingle driver for the Unix Windows and z OS platforms improving portability and consistent behavior of applications gt Improved integration with DB2 gt Type 4 driver functionality for thin clients as well as Type 2 functionality gt Easier installation and deployment As a Type 4 driver is completely written in Java it is easy to deploy you only have to ship the jar files containing the driver code to the required machine change the classpath and you are done In addition the use of a Type 4 driver does not require any setup in DB2 such as cataloging entries in the Database Node and DCS directories gt 100 percent Java application development process for SQLJ as demonstrated in Preparing an application to use static SQL on page 157 gt 100 percent JDBC V3 compliance Since DB2 UDB V8 1 3 FixPak 3 the Universal Driver is JDBC 3 0 compliant The version that will ship on z OS will be JDBC 3 0 compliant from day one gt Significant performance improvements for both JDBC and SQLJ applications on the Linux Unix and Windows platforms gt Trace improvements A architectural overview as well as some configuration on how to use the new Universal Driver can be found in The IBM DB2 Universal Driver for SQLJ and JDBC on page 27 In this chapter we look in a bit more detail at some of the enhancements that come with the DB2 Un
318. iversal Driver for SQLJ and JDBC 12 2 Setting connection properties in the URL 222 Recall from Connecting to a database on page 36 that a JDBC URL has the form jdbc driverselection database spec Where driverselection is db2 for the JCC driver and database spec is of the form location name for Type 2 connections or hostname portnumber instance name for Type 4 connections This was not the whole truth The IBM DB2 Univerasl Driver for SQLJ and JDBC allows you to specify additional connection properties after the instance name part or location name part separated by colons and taking the form propertyKey value For the list of supported property keys see Table 12 1 on page 223 These properties can be extremely useful especially for debugging and tracing purposes as discussed in Tracing on page 244 DB2 for z OS and OS 390 Ready for Java Table 12 1 Property keys for the DB2 Universal Driver for SQLJ and JDBC Property key driverType Determines the JDBC connectivity type to a data source If driverType is not set Type 2 connectivity is selected by default This property is a data source property and not a connection property JDBC 1 connectivity selection is based on the URL syntax user Specifies the user ID when connecting using the DriverManager getConnection String url Properties properties method password Specifies the password when connecting using the DriverManager getConnection String url Properties
319. ivileges on the table Each of these methods returns a ResultSet with standardized column names to retrieve the information you wanted We illustrate this with two examples one directly querying the DB2 catalog Example 7 2 and the other using the DatabaseMetaData API Example 7 3 Example 7 2 Find tables in a schema querying the DB2 catalog public static void db2FindTablesInSchema Connection conn String schemaPattern throws SQLException PreparedStatement stmt conn prepareStatement SELECT NAME FROM SYSIBM SYSTABLES 1 WHERE CREATOR LIKE 2 2 AND TYPE T 3 ORDER BY NAME stmt setString 1 schemaPattern ResultSet rs stmt executeQuery while rs next System out printIn rs getString 1 rs close stmt close Notes on Example 7 2 1 We want to select the name of all tables 2 Where the schema in DB2 terminology the creator matches schemaPattern 3 and where the object is a base table as opposed to a view or alias for example Obviously this example will work only on DB2 database servers Contrast this with Example 7 3 Example 7 3 Find tables in a schema using DatabaseMetaData public static void jdbcFindTablesInSchema Connection conn String schemaPattern throws SQLException DatabaseMetaData md conn getMetaData ResultSet rs md getTables null 1 schemaPattern 2 Won 3 new String TABLE 4 while rs next System out printl
320. j runtime ExecutionContext 195 sqlj runtime ForUpdate 178 188 sqlj runtime ref DefaultContext 188 189 sqlj runtime Scrollable 178 SQLSTATE 241 stack based 13 stand alone Java applications 16 Standard Edition 10 start 16 stateful session beans 20 stateless session beans 20 statement cache size 92 static SQL 234 334 DB2 for z OS and OS 390 Ready for Java STEPLIB 70 stop 16 strong typing 44 Sun Microsystems 4 SVCENAME 73 SYSIBM SQLCAMESSAGE 59 SYSIBM SQLCOLPRIVILEGES 58 SYSIBM SQLCOLUMNS 58 SYSIBM SQLFOREIGNKEYS 59 SYSIBM SQLGETTYPEINFO 59 SYSIBM SQLPRIMARYKEYS 59 SYSIBM SQLPROCEDURECOLS 59 SYSIBM SQLPROCEDURES 59 SYSIBM SQLSPECIALCOLUMNS 59 SYSIBM SQLSTATISTICS 59 SYSIBM SQLTABLEPRIVILEGES 59 SYSIBM SQLTABLES 59 SYSIBM SQLUDTS 59 T T2 Universal Driver setup 60 T4 Universal Driver setup 59 tag 16 tag libraries for database access 280 TCP IP 26 31 TCP IP listening port 73 Telnet 68 troubleshooting 112 try catch 240 try catch finally 14 Type 1 25 Type 2 25 278 Type 2 connectivity 29 Type3 26 Type 4 26 Type 4 connectivity 29 TYPE_FORWARD_ONLY 127 129 TYPE_SCROLL_INSENSITIVE 129 130 TYPE_SCROLL_SENSITIVE 129 U uncommitted read 176 uncustomized 46 uncustomized serialized profile 198 Unicode 7 Unix System Services 8 16 30 68 113 147 unqualified SQL 194 updateable iterator 186 updateColumns 179 187 UQ72081 28 UQ72082 28 UQ72083 28 UR 176 URL 17 158 url 103 user defined JDBC
321. jects Coordinator Agent Client configuration assistant Coded character set identifier Compact disk Character Data Representation Architecture Central electronics complex Coupling facility Coupling facility control code Coupling facility resource management Common Gateway Interface Customer Information control System Call level interface Command line processor Central Processing Unit Common storage area Created temporary table Direct access storage device DB2 Connect Enterprise Edition DB2 Connect Personal Edition DB2 performance monitor DB2 Universal Database DB2 Request Access Database access thread Database descriptor Database identifier Database management system Database request module Data control language Copyright IBM Corp 2003 All rights reserved DDCS DDF DDL DDM DLL DML DNS DRDA DSC DTT DUW EA EBCDIC ECS ECSA EDM ERM ESA ESP ETR FD OCA FTD FTP GB GBP GRS GUI HPJ HTML HTTP VO IBM ICF Distributed database connection services Distributed Data Facility Data Definition Language Distributed Data Management Dynamic load library manipulation language Data manipulation language Domain name server Distributed Relational Database Architecture Dynamic statement cache local or global Declared temporary tables Distributed Unit of Work Extended addressability Extended binary coded decimal interchange code Enhanced catalog sharing Exte
322. kS neceeieturvsevsemseiuexre ikb sbuim4re bru Biante da lee Pe 327 Other reSOUrCES uu sti 2 a iro ers dep Pb pn ee Re eee tae 327 Referenced Web sites 0 0 0 0 te Tie eens 328 How to get IBM Redbooks 0 ccc teeta 328 IBM Redbooks collections llle 328 Index eue e o t M etu atico ir ce Rt x aie nata 329 viii DB2 for z OS and OS 390 Ready for Java Figures 2 1 2 2 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 5 26 5 27 5 28 5 29 5 30 5 31 5 32 5 33 J2EE application model 0 eee 11 Java Compile once run anywhere 0000 cece 15 Java Database Connectivity JDBC API and driver types 2 25 Universal Driver architecture 0 0 eae 29 Local T2 connection 0 0 cee rn 30 Local application using Type 4 driver 2 2 2 0 cee ee 31 Type 4 connectivity from a non z OS platform llli 0c eee ee 32 Type 2 connectivity from a non z OS platform illllillelselsnn 33 DB2 for z OS and OS 390 as DRDA AR 20 0 eese 34 z OS Application Connectivity to DB2 for z OS and OS 390 isuu 35 Scope Of conn ctiona ca is sidere crc gods wigs Lene epu ni UZsicqe eu xin 37 Execution of dynamic vs static SQL statements 0 000 45 Dynamic SQL with SELECT but no UPDAT
323. l 9 2 1 The SQLJ translator 152 The first step in SQLJ program preparation is to invoke the SQLJ translator which by the Way is a pure Java program The translator takes an SQLJ source file checks for correct Java and SQLJ syntax and produces a compilable Java source file and one or more SQLJ profiles In WSAD 5 1 the SQLJ translator is automatically invoked whenever you save an SQLJ file or re build the project provided the enclosing project had been enabled for SQLJ support and you have selected to Perform build automatically on resource modification in the Workbench preferences Note Only when your SQLJ source file does not contain any SQL statements no profile will be produced DB2 for z OS and OS 390 Ready for Java During preprocessing the translator invokes a Java parser and an SQL parser to check Java and SQLJ constructs for syntactical correctness However the translator does not catch all possible errors in the source file For example it does not detect missing variable declarations except if these variables are used as host variables or in host expressions It also does not recognize syntax errors in the SQL statements proper this type of error will be caught by the profile customizer and or binder The generated Java file looks pretty much the same as the source SQLJ files except gt SQLJ iterator and context declarations are converted into class declarations Embedded SQL statements are converted in
324. l System out printIn Unknown education level else System out printIn Education level edlevel Alternatively you could still use a primitive and handle the SQLNullException short edlevel try sql ctx SELECT EDLEVEL INTO edlevel FROM DSN8710 EMP WHERE h System out println Education level edlevel catch SQLNullException e System out printIn Unknown education level But this is considered bad programming style in most cases since exceptions should not be used for regular control flow Use this technique only when you do not really expect the column to be NULL although it technically could be for example due to some business constraint that cannot be expressed in DDL In all other cases using the wrapper type is the cleaner solution 10 4 Data type mapping Your choice of Java data types can affect performance because DB2 picks better access paths when the data types of your Java variables map closely to the DB2 data types Also unlike other languages with SQL support JDBC and SQLJ provide data types to map to the SQL date time column types The best mappings of Java to DB2 data types are summarized in Table 10 2 on page 181 When the Java type column gives two Java types use the first one the primitive type for NOT NULL columns and the second one the corresponding wrapper type for nullable columns 180 DB2 for z OS and OS 390 Ready for Java Table 10 2 Best mappings of Java
325. language used by database engines and servers for data acquisition and definition SQL authorization ID SQL ID The authorization ID that is used for checking dynamic SQL statements in some situations SSL See secure socket layer Static bind A process by which SQL statements are bound after they have been precompiled All static SQL statements are prepared for execution at the same time Contrast with dynamic bind Static SQL SQL statements embedded within a program that are prepared during the program preparation process before the program is executed After being prepared the SQL statement does not change although values of host variables specified by the statement might change Stored procedure A user written application program that can be invoked through the use of the SQL CALL statement Structured Query Language SQL A standardized language for defining and manipulating data in a relational database Table A named data object consisting of a specific number of columns and some number of unordered rows Synonymous with base table or temporary table Task control block TCB An MVS control block used to communicate information about tasks within an address space that are connected to DB2 An address space can support many task connections as many as one per task but only one address space connection See address space connection TCB See Task control block TCP IP See Transmission Control Protocol based on
326. language was to use the good features of C while removing the areas that created potential for disasters The new language was to be fully object oriented and therefore based on reusable classes and objects At the same time it had to be less complex by removing the difficult areas in the old languages The new language would not use pointers and multiple inheritance It would handle the cleaning up of memory allocation requests internally and not rely on the programmer to code for it Memory leakage where programmers request memory in a program but not free it when finished with it is a problem in many programs and adds to the instability of these programs Along with this the new language had to generate code that was truly reusable It needed the ability to be run on all platforms with no change from Windows or Unix machines to small microprocessor based machines with no operating systems at all In the early 1990 s work was started on a new language called Oak which was renamed to the more marketable name Java in January 1995 Technology was possibly not the most significant reason for the acceptance of Java Throughout its development there was a perceived need that to achieve a critical mass of acceptance the whole architecture and all its elements would have to be supplied to the developers for free across the emerging Internet Java was first released by Sun Microsystems in 1995 What even surprised the developers was the spe
327. lational database management system RDBMS A relational database manager that operates consistently across supported IBM systems Remote Refers to any object maintained by a remote DB2 subsystem that is by a DB2 subsystem other than the local one A remote view for instance is a view maintained by a remote DB2 subsystem Contrast with local Remote Method Invocation RMI RMI is a specific instance of the more general term RPC RMI allows objects to be distributed over the network that is a Java program running on one computer can call the methods of an object running on another computer RMI and java net are the only 100 pure Java APIs for controlling Java objects in remote systems Remote Object Instance Manager n Remote Method Invocation a program that creates and manages instances of server beans through their associated server side server proxies Remote Procedure Calls RPC RPC is a generic term referring to any of a series of protocols used to execute procedure calls or method calls across a network RPC allows a program running on one computer to call the services of a program running on another computer Repeatable read RR An isolation level that locks all the rows in an application that are referenced within a transaction When a program uses repeatable read protection rows referenced by the program cannot be changed by other programs until the program ends the current transaction Requester Also application r
328. le lt body gt lt html gt EmployeeDetail JSP Example B 9 shows the source code of the EmployeeDetail JSP Note that this one invokes the EmployeePicServlet as part of its processing Example B 9 EmployeeDetail IDOCType HTML PUBLIC W3C DTD HTML 4 01 Transitional EN gt lt html gt lt head gt lt taglib uri jspsql prefix sql gt lt page language java contentType text html charset 1S0 8859 1 gt lt meta http equiv Content Type content text html charset IS0 8859 1 gt lt meta name GENERATOR content IBM WebSphere Studio gt lt meta http equiv Content Style Type content text css gt lt link href theme Master css rel stylesheet type text css gt lt title gt Emp loyeeDetail jsp lt title gt lt head gt lt body gt lt sql dataSourceSpec id MyConnection dataSource jdbc empdb gt lt sql select id empdetail connectionSpec MyConnection maxRows 1 gt lt sql sql gt SELECT EMPNO LASTNAME FIRSTNME MIDINIT HIREDATE SALARY FROM DSN8710 EMP WHERE EMPNO sql sql lt sql parameter position 1 type CHAR value lt request getParameter empno gt lt sql select gt lt table gt lt tr gt lt td colspan 2 gt lt img src EmployeePicServlet empno lt request getParameter empno gt gt lt td gt lt tr gt lt tr gt lt td gt Serial lt td gt td sql getColumn n
329. le from assigning a numeric column to a String host variable Common errors that will be caught earlier with SQLJ but will only be detected at runtime by JDBC include Misspelled SQL statements for example INERT instead of INSERT The SQLJ translator will catch and report this error However the translator does not parse the entire SQL statement so most syntax errors will only be detected by the profile customizer gt No parameter supplied for parameter marker Consider Example 3 3 on page 43 If you forgot to supply a value for one of the two parameter markers in JDBC a runtime error will occur In SQLJ there are no parameter markers rather the host variables are embedded in the statement DB2 for z OS and OS 390 Ready for Java Misspelled table name A misspelled table name will not be caught by the SQLJ translator after all it cannot know if a table does exist or not however the profile customizer will complain unless it was invoked with online checking disabled For a discussion of profile customizing and online checking see Chapter 9 The SQLJ program preparation process on page 149 gt Not all columns retrieved Assume that you add another column to the SELECT list in Example 3 3 on page 43 but forget to retrieve the corresponding column in the loop that processes the result set This cannot happen with SQLJ since the number of host variables in the FETCH statement and the number of columns in the SELE
330. le loader is sqlj runtime profile DefaultLoader 2909cbd6 contains 0 customizations original source file com ibm itso sg246435 sqlj Hello sqlj contains 1 entries profile com ibm itso sg246435 sqlj Hello SJProfileO entry 0 sql SELECT LASTNAME FIRSTNME gt SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN AND ORDER BY LASTNAME FIRSTNME hi line number 49 PREPARED STATEMENT executed via EXECUTE QUERY role is QUERY descriptor is null contains 2 parameters 1 mode IN java type java math BigDecimal java math BigDecimal sql type NUMERIC name min marker index 90 2 mode IN java type java math BigDecimal java math BigDecimal sql type NUMERIC name max marker index 98 result set Type is POSITIONED RESULT result set name is com ibm itso sg246435 sql1j Hello EmployeeIterator contains 3 result columns 1 mode OUT java type java lang String java lang String sql type VARCHAR name getColl marker index 1 2 mode OUT java type java lang String java lang String sql type VARCHAR name getCol2 marker index 1 3 mode OUT java type java math BigDecimal java math BigDecimal sql type NUMERIC name getCol3 marker index 1 As you can see the profile contains zero customizations Compile the program javac As mentioned before we only need to do this step because we used the compile false option on the sqlj command line C TestSQLJ SG24 6435 gt javac com ibm itso sg246435 sq1j Hello java This
331. leted from the database driverManagerSpec Defines a connection using a the DriverManager E LEO Retreive a value from a result set P gt ma Figure 16 3 Select Tag Library The Tag Libraries box lists all the tag libraries that are available in the project Right now it only lists the jspsql tag library that we included in the project at the beginning of this section Check the library and enter sq in the Prefix text field This tells the JSP engine that all custom tags defined by the library will be referenced in the JSP with a prefix of sql we could have used any other prefix as long as it does not collide with another prefix already in use Click OK We can leave the next dialog window as it is and click Next to proceed to the Method Stubs panel Here make sure that Add to web xml is checked which causes a URL mapping to be set up for our JSP so it can later be invoked from a Web browser Now click Finish WSAD creates a skeleton JSP file that reflects our settings from the New JSP Page wizard Example 16 1 You may have to click the Source tab to see the actual JSP program skeleton Example 16 1 Skeleton JSP file generated by the New JSP Page wizard lt xml versionz 1 0 encoding ISO 8859 1 gt lt DOCTYPE html PUBLIC W3C DTD XHTML 1 1 EN http www w3 org TR xhtml 11 DTD xhtm111 dtd lt html xmins http www w3 org 1999 xhtml gt lt head gt lt taglib uri jspsql prefix sql gt lt
332. lient when a server side error occurs The proprietary method DB2Sglca getMessage can also be called to retrieve the formatted message text clientUser The clientUser property establishes the current client user name for this connection The current client user name is not the JDBC connection user name but is a string for accounting purposes Unlike the JDBC connection user name the current client user name and any associated client information may change during the life of this connection See Java API for Set Client Information SQLESETI on page 226 clientWorkstation Establishes the workstation name for the current client on this connection clientApplicationInformation Establishes generic application information for the current client on this connection 224 DB2 for z OS and OS 390 Ready for Java Property key clientAccountingInformation Establishes generic accounting information for the current client on this connection You can supply values for the individual keys by either using the DriverManager getConnection String url Properties properties method Example 12 1 or by encoding them in the URL for example in a DataSource definition or on the application s command line Example 12 1 Setting properties using DriverManager getConnection String Properties import java net InetAddress import java sql Connection import java sql DriverManager import java util Properties private static Connection o
333. lly was another result row or false if no more rows were found Thus the loop terminates after the last row in the result set has been processed We then print out the values in each column referring to the columns by index 104 DB2 for z OS and OS 390 Ready for Java 6 1 1 Loading the JDBC driver The first step before we can establish a connection is to load the JDBC driver To dynamically load a Java class into the JVM you use the Class forName method This method does not have anything to do with JDBC per se it can be used to load any class provided this class can be found on the application s runtime classpath After the class has been loaded the JVM initializes it a class can provide its own initialization code by implementing a piece of code called a static initializer In the case of a JDBC driver the static initializer registers that newly loaded driver with the JDBC runtime 6 1 2 Establishing the connection Next we can establish the connection to the database supplying a JDBC URL a user name and a password to the DriverManager getConnection method There are several other variations of the getConnection method that we do not discuss at this point Note that in order to keep the example reasonably small we have hardcoded the URL user and password in the program This is bad practice in a real world program because the application has to be changed and recompiled whenever one of these values changes To solve thi
334. loyee list lt title gt lt head gt lt body gt lt hl gt Employee listing as of lt java text DateFormat getDateInstance format new java util Date gt 2 lt h1 gt lt sql dataSourceSpec id MyConnection 3 dataSource jdbc empdb gt lt sql select id emplist connectionSpec MyConnection gt 4 lt sql sql gt SELECT EMPNO LASTNAME FIRSTNME MIDINIT FROM DSN8710 EMP ORDER BY EMPNO sgl sql lt sql select gt lt table gt lt col width 30 gt lt col width 70 gt lt thead gt lt tr align left gt lt th gt Serial lt th gt lt th gt Name lt th gt lt tr gt lt thead gt lt tbody gt lt sql repeat name emplist over rows gt 5 lt tr gt lt td gt a href EmployeeDetail jsp empno lt sql getColumn index 1 gt gt 6 sql getColumn index 1 gt lt a gt 284 DB2 for z OS and OS 390 Ready for Java lt td gt td sql getColumn index 2 gt lt td gt 7 lt tr gt lt sql repeat gt lt tbody gt lt table gt lt body gt lt html gt Notes on Example 16 2 on page 284 1 This directive makes the database access tag library known to the JSP compiler We use a prefix of sql to access the tags in the library 2 This is an example of a Java code snippet inside the HTML code It uses the java util TextFormat class to format the current date The resulting string is then included in the content 3 This is one of the custom t
335. lumnWidths computes the preferred display width of the columns in the result set Example 7 11 For each column it takes the maximum of the column width and the column name adding 1 for a blank character to separate the columns It uses two methods of the ResultSetMetaData API namely getColumnLabel and getColumnDisplaySize Example 7 11 Spufi getColumnWidths Determine column display widths param md Result set meta data return Array containing the column display widths Chapter 7 JDBC revisited 135 136 throws SQLException private int getColumnWidths ResultSetMetaData md throws SQLException final int columnCount md getColumnCount int columnWidths new int columnCount for int col 0 col lt columnCount col int colWidth 1 Math max md getColumnLabel col 1 length md getColumnDisplaySize col 1 if colWidth gt MAXCOLWIDTH colWidth MAXCOLWIDTH columnWidths col colWidth Last column does not need a blank appended columnWidths columnCount 1 return columnWidths Notes on Example 7 11 on page 135 Allocate an array of integers one for each column The column s display width is one plus either 1 2 3 the length of the column name 4 or the width of the column whichever is bigger 5 However we truncate at MAXCOLWIDTH characters The printRow method prints a single row from the result set calling the getColumnValue met
336. ly remove the data from the persistent store This method normally contains SQL DELETE statements gt ejbCreate is called when a new instance of an EJB class has to be created It usually executes SQL INSERT statements and returns the primary key values gt ejbLoad initializes the bean instance with the values from the database It normally contains SQL SELECT statements using the primary key value gt ejbStore updates the database with the current values from the in memory representation that is it writes back any changes to that in memory representation This is typically done with SQL UPDATE statements Some of the other methods allow processing to handle activation and deactivation of the bean as well as methods to reactivate the bean to allow reconnection to the database and a corresponding method to allow connections to be dropped Further setter and getter methods may be defined in the EJB to set column values in the beans data It is important to understand that although specific SQL is coded in the bean s methods it is the container that decides when the methods are executed and BMP beans must be coded to allow for this Finally when the container has finished with an instance of a bean it is returned to a pool for reuse Container managed persistence CMP BMP beans offer flexibility but can be difficult to maintain and repetitive to produce The alternative is to use container managed persistence CMP entity
337. lying database cursor to the next row It returns a boolean value that indicates whether or not the cursor is now positioned on a row This means that when calling it after the last row had been processed it will return false terminating the loop DB2 for z OS and OS 390 Ready for Java When the ResultSet object is positioned on a row that is when the next method has been called and returned true you use one of several getter methods to retrieve the individual columns of the current row There is two getter methods for each Java type that can possibly be returned as a column value One taking an int parameter and one taking a String parameter You can refer to a column either by its index in the list of selected columns beginning with 1 or by its name using the int or the String version of the getter method respectively In our JDBC program from Example 6 1 on page 103 we used the index String lastname rs getString 1 LASTNAME String firstname rs getString 2 FIRSTNME BigDecimal salary rs getBigDecimal 3 SALARY Important For maximum portability Sun recommends that you retrieve columns from left to right that is starting with the lowest index and that you retrieve each column only once per row Some JDBC drivers have been known to produce unexpected results otherwise Alternatively to refer to the columns by name we could have coded String lastname rs getString LASTNAME String firstname rs g
338. mation forUpdateCursorNames null DB2 ProfileData custom data 7 Package Name HELLO Package Version null Package Collection SG246435 Is Default Collection false Package Consistency Token hex format 4241635543474870 Package Consistency Token character format BAcUCGHp Notes on Example 9 7 on page 169 1 Now we see that there is one customization in the profile 2 We can immediately see that it is a DB2 customization 3 4 There is a description of all the input host variables Their data type nullability code page Everything beyond this point is part of the customization and length 5 There is information about the holdability of the cursor 6 There is a also a description of all the output columns in the SELECT list with similar information as for the input host variables such as their data type nullability code page and length The last section provides information about the DB2 packages that correspond with this serialized profile It shows the package and collection name and the consistency token Executing the sqlj application using statically bound statements Now we are ready to execute our program The command as well as the results are identical of course Example 9 8 on page 172 You can use the same means as described in Chapter 9 The SQLJ program preparation process 171 Verifying that your application uses static SQL on page 162 to check whether the program
339. mation supplied when creating the JDBC DB2 for z OS and OS 390 Ready for Java connection the extended information can be changed at any time for example when the application server processes a request from another user Also the extended client information can be used for accounting or workload management purposes The client information will be sent to the server at the next opportunity that is along with the next SQL call on that connection To use this interface cast the java sql Connection object to a com ibm db2 jcc DB2Connection In addition to the standard java sql Connection methods the DB2Connection class provides the following methods to set extended client information gt setDB2ClientUser public abstract void setDB2ClientUser String s throws SQLException Sets a user name for the connection Unlike the user ID supplied when creating the connection this can be a full user name gt setDB2ClientWorkstation public abstract void setDB2ClientWorkstation String s throws SQLException Sets a client workstation name for the connection for example the workstation s TCP IP host name gt setDB2ClientApplicationInformation public abstract void setDB2ClientApplicationInformation String s throws SQLException Sets an application name For example you can specify the main class name of the Java application working with the connection gt setDB2ClientAccountingInformation public abstract void se
340. md noargs Skip methods that returned an array if result instanceof Array continue Everything else should print out fine System out println m getName result catch Exception unexpected Should not happen public static void main String args try Class forName com ibm db2 jcc DB2Driver Connection conn DriverManager getConnection jdbc db2 DB7Y DatabaseMetaData md conn getMetaData printMetaData md catch Exception e System err printIn e 7 4 3 Information about database objects Suppose you want to find out the column names and types for a table If you are a seasoned DB2 developer you certainly know about the DB2 catalog table SYSIBM SYSCOLUMNS which contains information about each column in each table defined in the DB2 subsystem It is easy enough to query that table and find out the information you want However this approach obviously is everything but platform independent it ties your application to DB2 The DatabaseMetaData interface allows you to perform these catalog queries in a DBMS independent way Specifically you can gt Find all the tables whose names match certain criteria Chapter 7 JDBC revisited 125 126 gt For each table in turn Enumerate the columns in that table along with their type length and nullability Find the table s primary key columns Find the table s indices and get statistics on the table Find pr
341. ministrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help amp Bak gt O A Bsearch Favorites media lt 4 Eby 5 H Address amp http vwtsc63 itso ibm com 9080 admin secure logon do WebSphere Application Server Administrative Console rd Version 5 k2 Home Save Preferences Logout Help User ID bart JDBC Providers cel111 Servers JDBC providers are used by the installed applications to access data from databases i Applications E Resources Total 2 JDBC Providers E Scope Cell cel111 Node nd111sc63 Server ws111sc63 Generic JMS Providers O Cell cel111 Use scope settings to limit the availability of resources to a particular cell WebSphere JMS Provider node or server WebSphere MQ JMS Provider C Node ndiiiscB3 When new items are created in this view they will be created within the Mall Provid current scope lail Providers 9 server 51115c63 Resource Environment Providd URL Providers Apply Resource Adapters Security Fiter Environment Preferences System Administration y New Delete Troubleshooting Hame Description S Cloudscape JDBC Driver Cloudscape JDBC Provider bB2 JcC T2 zoS JDB Rrovider Custom JCC T2 configuration Figure 5 34 Display the JDBC provider Scroll to the bottom of the resulting window and click Data Sources Figure 5 35 on page 92
342. missing and of the correct format Finally for the finishing touch we modify EmployeePicServlet to display a default image when no picture of the employee was found in the database Modify the doGet method replacing copy pic out With if pic null getServletContext getRequestDispatcher images noimage gif forward req resp else copy pic out Chapter 15 Using Servlets to access DB2 267 Create a new folder called images under the Web Content folder and put the noimage gif file in there When you run the EmployeeList Servlet one more time the links should take you to the correct detail page for each employee 15 6 3 Using EmployeePicServlet from EmployeeDetailServlet 268 As mentioned before we want to show the employee s photo from the detail page if one is available To do so we simply add one row to the table containing the employee detail information Unlike the other table rows this one does not contain text but an image Modify the doGet method of EmployeeDetailServlet as shown in boldface in Figure 15 11 Example 15 11 EmployeeDetailServiet displaying a picture try Employee emp Employee findByPrimaryKey empno out println tr img src EmployeePicServlet empno _ empno gt lt tr gt printRow out Serial emp getEmpNo printRow out Name emp getLastName emp getFirstName printRow out Hired emp getHireDate printRow out
343. mming Guide and Reference for Java SC26 9932 for the full list of valid SQL statements Additionally there is a special statement to set the isolation level for the current transaction sql ctx SET TRANSACTION ISOLATION LEVEL level Where evel is one of the levels listed in Table 10 1 Table 10 1 SQLJ isolation levels and their corresponding DB2 isolation levels SQLJ isolation level DB2 isolation level READ UNCOMMITTED UR uncommitted read READ COMMITTED CS cursor stability REPEATABLE READ RS read stability SERIALIZABLE RR repeatable read 176 DB2 for z OS and OS 390 Ready for Java Note You can set the isolation level only at the beginning of a transaction that is before performing any other SQL operation The default isolation level for the DB2 JDBC drivers is READ COMMITTED CS 10 1 2 Iterator declarations You use an iterator declaration to declare an SQLJ iterator class To use an iterator you assign the result of a SELECT statement to an instance of an iterator class as discussed in Queries iterators and the assignment statement on page 182 SQLJ iterators come in two different flavors Positioned iterators and named iterators The two kinds of iterators are implemented as different Java types and cannot be used interchangeably In this section we only discuss iterator declarations Using positioned iterators on page 182 and Using named iterators on page 183 explain how to use pos
344. mprovement in the physical code to drive these calls the JDBC drivers 1 1 3 The rise of the Internet server As Java gained acceptance within the development community some work had to be done to move from a development platform for small consumer devices to one which would fit into and fulfill the needs of the large system development community This move was given further impetus through the rise of the Internet and the business community s acceptance of this new technology as a tool of its trade The business technology community was going through revolutionary changes with the move away from the single platform model which handled everything from the user interface processing of business logic to data access Replacing this was a whole new platform relationship where all these processes could be split off on to any number of platforms Where there was once a mainframe connected to dumb terminals all within the offices of the company the new technology saw end users running browsers which connected to business logic on middleware connected to databases running on other server platforms All this could be connected with a raft of new technologies such as message oriented middleware transaction monitoring and object request brokers While this was happening outside the Java platform itself was moving with the addition of new features such as Java Servlet support to provide a simple method of removing business logic from front end
345. must be added to the Java CLASSPATH If other JDBC classes are present in the classpath then this jar file must come first otherwise you get an error if your program contains references to new functions that are only available in this new driver For example if you use the Type 4 URL to connect and the old non JCC Type 2 driver class in your CLASSPATH the program will fail as Type 4 connections are not available from that driver Establishing a connection Once the driver is loaded a connection to the data source needs to be invoked This is done by using the DriverManager getConnection method which comes in three forms gt getConnection String url gt getConnection String url user password gt getConnection String url java util Properties info JDBC defines the format of the URL as jdbc driverselection database Where driverselection represents the database specific type designator such as db2 What the JDBC runtime does is to examine all drivers that have been loaded and therefore registered with the JDBC runtime asking each of them in turn whether they accept the driverselection part The first driver to answer true is used for the connection and is passed the database part which again has a driver specific syntax This way several JDBC drivers can simultaneously loaded into a Java Virtual Machine and the JDBC runtime can determine which one to use based on the connection URL Table 3 2 shows the different form
346. n 2 3 2 Primitive data types Java has a set of base or primitive data types These are boolean char byte short int long float and double These types are machine independent and have a specific size They are called primitive types because they do not contain any other types in contrast to class types for example 2 3 3 Garbage collection When an object is created in an object oriented language memory must be allocated in order to store its instance variables In C there are two types of memory allocation Stack based and heap based memory allocation An object allocated on the stack is automatically deleted when it goes out of scope that is at the end of the program section the block where it had been created Objects that are to remain in memory for a longer period must be allocated using heap storage With heap based storage allocation in C it is up to the programmer to keep a eye on the object and to explicitly delete the object when he is finished with it Throughout the life of an object the programmer needs to maintain a pointer to the object and use this to ultimately delete the object With a normal program this means the developer needs to maintain a list of active objects and explicitly remove objects when they are no longer used This leads to two problems The first one is that coders often do not delete objects when they are no longer needed meaning that programs grow unnecessarily This situation is call
347. n DB2 for z OS and OS 390 Ready for Java When both occur the SQLJ and JDBC drivers will be common with DB2 UDB V8 The implication for developers who wish to deploy SQLJ programs to the DB2 for OS 390 and z OS V7 platform is that there is a common SQLJ profile customizer db2sql jcustomize and the serialized profiles are portable across the DB2 family During the development of this publication we used early versions of both the T4 and T2 driver for z OS Actually since the T4 driver is a pure Java driver you can copy it together with the required license files from your DB2 for LUW installation into a USS directory on your z OS system which we actually did and it works fine at least for all our tests Developers can deploy on any server platform without running the profile customizer on the target system The ser file contains information needed for all BIND operations without having to recustomize on each BIND Note that DBRMs will no longer be produced DB2 UDB for z OS V8 will have the SQLJ and JDBC Universal Driver incorporated when it becomes generally available The current JCC T4 driver does not support 2 phase commit This will change in the near future Then the T4 driver will also be able to do 2 phase commit This functionality will be implemented in both the V7 and V8 of DB2 for z OS Chapter 4 Products and levels Now andsoon 55 56 DB2 for z OS and OS 390 Ready for Java Setup This chapter descr
348. n DOCTYPE HTML PUBLIC W3C DTD HTML 4 01 Transitional EN gt out printIn html out printIn lt head gt lt title gt Employee detail title head out printIn body out printIn lt table gt try ctx new Employee Ctx Employee emp Employee findByPrimaryKey ctx empno 2 printRow out Serial emp getEmpNo 3 printRow out Name emp getLastName emp getFirstName printRow out Hired emp getHireDate printRow out Salary emp getSalary catch Exception e throw new ServletException e out printIn table out printIn lt body gt out printIn lt htm1 gt Prints one row of employee information to the servlet output stream The left column shows a label the right column the actual data param out The servlet response output stream param label The label to be displayed in the left column param value The value to be displayed in the right column f private void printRow PrintWriter out String label Object value throws IOException out printIn lt tr gt out printIn lt td gt label lt td gt out printIn lt td gt value lt td gt out printIn tr DB2 for z OS and OS 390 Ready for Java Notes on Example 15 8 on page 263 1 Employee number hardcoded we fix this later 2 Retrieve employee data 3 Print one HTML table row
349. n context may or may not have been set up for you In fact the SQLJ runtime implementation tries to create a default context by doing a JNDI lookup for jdbc defaultDataSource so when you are in an environment that has access to a JNDI namespace and a DataSource under that name has been found the default context is set up automatically To set up a default connection context yourself use one of the following two alternatives gt Creating a Connection and a DefaultContext in one step DefaultContext ctx new DefaultContext url userid password autoCommit DefaultContext setDefaultContext ctx Because in most cases you want to be in control of the commit scope of your application make sure that autocommit is false for example 188 DB2 for z OS and OS 390 Ready for Java DefaultContext ctx new DefaultContext jdbc db20s390sq1j NEWYORK bart bingo false DefaultContext setDefaultContext ctx gt Creating a DefaultContext using an existing JDBC connection DefaultContext ctx new DefaultContext conn DefaultContext setDefaultContext ctx 10 6 2 Why the connection context is important As we said above an SQLJ statement that does not explicitly specify a connection context runs under a default context which you could have established yourself or which may have been set up by the environment in which your application is running This saves you some typing since you do not need to code the ctx clause on
350. n focusses on the new IBM Universal Driver for SQLJ and JDBC IBM s new JDBC driver implementation supporting both Type 2 and Type 4 driver connectivity to the members of the DB2 family including DB2 for z 0S and DB2 for Linux Unix and Windows This publication provides guidance on the different ways to set up your environment to hook a Java program up to a DB2 for z OS subsystem through JDBC or SQLJ using the Type 2 driver and the Type 4 driver We provide an SQLJ tutorial and demonstrate how to develop and deploy SQLJ programs using the new SQLJ support functions that became available with WebSphere Studio Application Developer V5 1 We demonstrate the use of Java and DB2 using native Java programs as well as through the use of Servlets and JSPs running on a WebSphere Application Server SG24 6435 00 ISBN 0738427853 j Redbooks INTERNATIONAL TECHNICAL SUPPORT ORGANIZATION BUILDING TECHNICAL INFORMATION BASED ON PRACTICAL EXPERIENCE IBM Redbooks are developed by the IBM International Technical Support Organization Experts from IBM Customers and Partners from around the world create timely technical information based on realistic scenarios Specific recommendations are provided to help you implement IT solutions more effectively in your environment For more information ibm com redbooks
351. n is not triggered so often thereby reducing the repeated scanning of long living objects 300 400MB heap sizes are not uncommon 13 4 2 Get the latest code and maintenance Keep current with JDK releases and with upgrades to the JDBC driver While the initial development emphasis was on delivering JDBC function in the meantime a lot of work has been done regarding improvements in CPU performance by reducing column processing overheads This has been delivered through improvements to the JDBC driver and the JDK JVM The JDBC 2 0 driver delivered with DB2 Version 7 has been the base for delivering these performance enhancements Chapter 13 Performance topics 237 13 4 3 Turn on DB2 dynamic statement caching Turn on DB2 dynamic SQL statement caching with CACHEDYN YES in your subsystem parameters DSNZPARM This option causes dynamically prepared SQL statements to be cached across transaction boundaries and can therefore dramatically improve the performance of JDBC applications especially when the same limited set of SQL statements is executed over and over again and parameter markers are used 238 DB2 for z OS and OS 390 Ready for Java 14 Error handling and diagnosis This chapter discusses error handling extended debugging and tracing facilities In particular we provide a description of the DB2 specific extensions to aid you with diagnosis tracing and accounting Copyright IBM Corp 2003 All rights reserved 239 14
352. n is shown in Figure 3 7 on page 34 Chapter 3 Accessing DB2 from Java 33 z OS image z OS image Figure 3 7 DB2 for z OS and OS 390 as DRDA AR In this configuration DB2A does not have any databases that are accessed by applications running on the WebSphere Application Server All SQL requests are routed through the local DB2A but are accessing data on the remote DB2B through DRDA In this configuration we need the DRDA Application Requester functionality of DB2A to access the data residing on DB2B This is because the current non Universal JDBC driver for z OS and OS 390 is a Type 2 driver and cannot directly access a remote DB2 system DB2B This also applies when using the Universal Driver with type 2 connectivity From the Java application s point of view the local DB2 is transparent The application will connect directly to the remote DB2 system However the local DB2 has to exist because its services as a DRDA Application Requester AR are needed to be able to connect to the remote DB2 system Note that from a performance standpoint this is not the ideal configuration as there is the additional overhead of having to route every SQL request over DRDA over most likely TCP IP to a remote DB2 system 3 2 6 IBM z OS Application Connectivity to DB2 for z OS and OS 390 34 z OS Application Connectivity to DB2 for z OS and OS 390 is a no charge optional feature of DB2 Universal Database Server for z OS
353. n rs getString TABLE NAME 5 rs close DB2 for z OS and OS 390 Ready for Java Notes on Example 7 3 on page 126 1 The first parameter to DatabaseMetaData getTables is a catalog name Since DB2 for z OS and OS 390 does not have the notion of a catalog we can set this parameter to null 2 The second parameter is the schema pattern 3 Next comes a pattern of table names to search for Since we want to retrieve all table names in the schema we use the wildcard which matches any string 4 The last parameter designates a list of table types to be returned for example this could include views aliases or other special table types supported by the database server The possible values are DBMS dependent the DatabaseMetaData getTableTypes method returns a list of all available table types 5 The getTables method returns a ResultSet with standardized column names including TABLE NAME For the list of column names refer to the JDBC API Also the JDBC API specifies the ordering of result set rows In the case of getTables the rows are ordered by table type table schema and table name Important The catalog metadata API requires the catalog stored procedures to be installed on the database server See Required DB2 for z OS changes to enable the Universal Driver on page 58 for more details 7 5 Positioned UPDATE and DELETE In INSERT UPDATE and DELETE statements on page 122 we only covered search
354. nce type argument with a null argument However the updateXxx methods do not update the database row immediately rather the changes are only recorded in the ResultSet object To actually change the database call the updateRow method Example 7 4 Using updatable cursors private static void increaseSalary BigDecimal amount throws SQLException PreparedStatement stmt conn prepareStatement SELECT SALARY FROM DSN8810 EMP ResultSet TYPE_SCROLL_SENSITIVE ResultSet CONCUR_UPDATABLE try ResultSet rs stmt executeQuery try while rs next BigDecimal oldSalary rs getBigDecimal SALARY rs updateBigDecimal SALARY oldSalary add amount rs updateRow finally rs close finally stmt close Restriction Positioned UPDATE is only supported with DB2 Version 8 and later We tested the example on a pre release version of DB2 Version 8 for z OS 7 5 2 Positioned DELETE Positioned DELETE is easy To delete the row the ResultSet is currently positioned on call ResultSet deleteRow The difference between positioned UPDATE and positioned DELETE is that deleteRow takes effect immediately while the updateXxx methods take effect only when updateRow is called 7 6 Large objects LOBs 128 LOBs allows applications to store large binary or text data such as images word processor documents or large XML files Basically there are two different ways to handle LOB columns fr
355. nded common storage area Environment descriptor management Enterprise resource management Enterprise Systems Architecture Enterprise Solution Package External throughput rate an elapsed time measure focuses on system capacity Formatted Data Object Content Functional track directory File Transfer Program Gigabyte 1 073 741 824 bytes Group buffer pool Global resource serialization Graphical user interface High performance Java Hypertext Markup Language Hypertext Transfer Protocol Input output International Business Machines Corporation Integrated catalog facility 315 ICMF IFI IPLA IRLM ISPF ISV ITR ITSO IVP J2EE J2ME J2SE JAAS JCE JCC JDBC JGSS JNDI JSP JVM KB LA LE LOB LPAR LPL LRECL LRSN LUW MB NPI ODBC OS 390 PAV PDS PIB PSID PSP 316 Internal coupling migration facility Instrumentation Facility Interface IBM Program Licence Agreement Internal resource lock manager Interactive system productivity facility Independent software vendor Information Technology Internal throughput rate a processor time measure focuses on processor capacity International Technical Support Organization Installation verification process Java 2 Platform Enterprise Edition Java 2 Platform Micro Edition Java 2 Platform Standard Edition Java Authentication and Authorization Service Java Cryptography Extension DB2 Universal Driver for Java Common Connectivi
356. net protocol based on TCP IP used to fetch hypertext objects from remote hosts IDE See ntegrated Development Environment Identity Column A column that provides a way for DB2 to automatically generate a numeric value for each row that is inserted into the table IIOP See Internet Inter ORB Protocol 320 DB2 for z OS and OS 390 Ready for Java incremental bind A process by which SQL statements are bound during the execution of an application process because they could not be bound during the bind process and VALIDATE RUN was specified Integrated Development Environment IDE A set of software development tools such as source editors compilers and debuggers that are accessible from a single user interface In WebSphere Studio the IDE is called the workbench Internet The vast collection of interconnected networks that use TCP IP and evolved from the ARPANET of the late 1960s and early 1970s The number of independent networks connected into this vast global net is growing daily Internet Inter ORB Protocol IIOP A protocol used for communication between Common Object Request Broker Architecture CORBA object request brokers Internet Protocol IP In the Internet suite of protocols a connectionless protocol that routes data through a network or interconnected networks IP acts as an intermediary between the higher protocol layers and the physical network However this protocol does not provide error recovery and
357. ng args a try iei load a driver Class forName com ibm db2 jcc DB2Drive Connection con DriverManager getConnec z bartr2 bartr2 Data Definition Navigator String s Statement stmt m ResultSet rs AA i stmt con createStatement i A DB7Y rs stmt executeQuery SELECT EMPNO fro while rs next s rs getString il System out println Employee number rs closetl stmt close catch Throwable e e printStackTracei gt 411 Servers Server Status 4 Tasks DB Output Servers Figure 5 19 The completed connection DB7Y Next we expand the DB Server DB7Y by clicking the sign Figure 5 20 on page 80 Chapter 5 Setup 79 Data IBM WebSphere Studio Application Developer File Edit Source Refactor Navigate Search Project Run Window Help w 5 jg ge s aw B 9 i 4 Sfo Iers j m 5624 6435 Data Definition x D Judyclass java x public class Judycl ss ra e i Gea judy public static void mainiString args 3 try i tevs load a driver Ji Class forName com ibm db2 jcc DB2Driver n Connection con DriverManager getConnection zs bartr2 bartr2 Data Definition Navigator y String s Statement stmt NS ResultSet rs AL IDB7Y1 stmt con createStatement i E A DB7Y rs stmt execu
358. ng a local RRS connection to the DB2 system Therefore you need to tell the JDBC driver gt What DB2 the driver initially has to connect to You may think that you already indicate this in the URL when calling the DriverManager or DataSource However this is different This parameter is to tell the JDBC driver to what DB2 the initial connection should be made irrespective of the name specified in the URL The db2 jcc ssid property is used for this purpose If this property is not specified the driver will look in the DSNHDECP module for a default DB2 subsystem name to connect to This may or may not be what you want gt What plan to execute Since this is a locally attached driver we need to tell the driver what plan name to use When using the T4 driver we use the DISTSERV plan that is built into DB2 When using the Universal Driver you can specify these properties in the db2jcct2 properties file You need to tell the system where this file is by using the export command like for any other USS environment variable see Example 5 6 on page 71 DB2 for z OS and OS 390 Ready for Java Example 5 2 db2jcct2 properties file db2 jcc ssid DB7Y db2 jcct2 planName BARTSQLJ Important At the time of writing of this publication db2jcct2 properties is the name of the properties file to specify the Type 2 Universal Driver for z OS properties This may have changed in the meantime as the tests were done with a pre GA version of the dr
359. ng application In the case of DB2 this task is performed by a program called the JDBC applet server DB2 for z OS and OS 390 does not supply a Type 3 driver DB2 for Linux Unix and Windows still has a so called network or net driver but if you are not using it already you are strongly encouraged to use the Type 4 driver instead Type4 A Type 4 driver is fully written in Java and accesses the target database directly using the protocol of the database itself In the case of DB2 this is DRDA As the driver is fully written in Java it can be ported to any platform that supports that DBMS protocol without change thus allowing applications to also use it across platforms without change This driver type has been implemented through the IBM DB2 Universal Driver for SQLJ and JDBC which is first provided with Version 8 of DB2 UDB for Linux Unix and Windows The driver will also be made available for DB2 for z OS and OS 390 Version 7 with the PTF for APAR PQ80841 This PTF was not available at the time this Redbook was published This driver is also known as the DB2 Universal Driver for Java Common Connectivity or JCC In addition the Universal Driver also provides Type 2 connectivity For more information about the Universal Driver see The IBM DB2 Universal Driver for SQLJ and JDBC on page 27 and Chapter 12 The DB2 Universal Driver on page 221 JDBC drivers and driver types for use with DB2 z OS and OS 390 There are a number of different
360. nix Windows db2jcc license cu jar is included with all of the DB2 LUW products including Personal Edition PE Websphere Edition WSE and DB2 Express gt db2jcc license cisuz jar Permits JDBC SQLJ connectivity to all DB2 servers including z OS iSeries VM VSE DB2 products UDB for Unix Windows and Cloudscape This license is provided to DB2 Connect licensees only db2jcc license cisuz jar is included with all DB2 Connect products including Personal Edition Enterprise Edition CUE CASE and DB2 ESE The meaning of the suffix letters in the license file names is as follows c cloudscape i iSeries 2 z OS s sqlds u unix windows Look in ane er E3 History amp Desktop za on es db2jcc license cisuz jar wm H db2jcc license cu jar A i sqlj zip NH My Computer File name db2jcc license cisuz jar db2jcc jar Files of type R jar zip Cancel ta My Network P Figure 5 16 Select jar files Chapter 5 Setup 77 gt The last entry was the connection URL It is specified as jdbc db2 server port Location name This form of invocation tells the JDBC driver that we want a Type 4 connection We coded our values If DDF in your installation is listening on well known port 446 you do not have to enter the port number When pressing the Finish button you are kindly reminded that a DB2 for z OS and OS 390 subsystem can contain a large number of objects and that it may
361. nly binds a package against the database but also updates the serialized profile We use the db2sql jprint utility again The result is shown in Example 9 7 Example 9 7 Customized serialized profile printing contents of profile com ibm itso sg246435 sqlj Hello SJProfileO created 1061630428017 2003 08 23 associated context is sqlj runtime ref DefaultContext profile loader is sqlj runtime profile DefaultLoader 2909cd92 contains 1 customizations 1 com ibm db2 jcc sqlj Customization 3de20d92 2 original source file com ibm itso sg246435 sqlj Hello sqlj contains 1 entries profile com ibm itso sg246435 sqlj Hello SJProfileO entry 0 Chapter 9 The SQLJ program preparation process 169 sql SELECT LASTNAME FIRSTNME SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN AND ORDER BY LASTNAME FIRSTNME E line number 49 PREPARED STATEMENT executed via EXECUTE QUERY role is QUERY descriptor is null contains 2 parameters 1 mode IN java type java math BigDecimal java math BigDecimal sql type NUMERIC name min marker index 90 2 mode IN java type java math BigDecimal java math BigDecimal sql type NUMERIC name max marker index 98 result set Type is POSITIONED RESULT result set name is com ibm itso sg246435 sq1j Hello EmployeeIterator contains 3 result columns 1 mode OUT java type java lang String java lang String sql type VARCHAR name getColl marker index 1 2 mode OUT java type java lang S
362. nnot assign values to a final field WSAD suggests not to create a setter method for the empNo field Figure 11 2 We reply Yes Generate Getter and Setter x p Field empNo is final b OK to skip creation of the setter method Yes To All No Cancel Figure 11 2 Skip setter method The following code has been added to the Employee sq j file Example 11 2 Example 11 2 Employee sqij with attributes and accessors return public Short getEducationLevel return educationLevel public String getEmpNo DB2 for z OS and OS 390 Ready for Java return empNo public String getFirstName return firstName param public void setEducationLevel Short short1 educationLevel short1 public void setFirstName String string firstName string Note that there is no setter method for empNo since we told WSAD not to generate one Now a red marker appears on the left hand side of the editor window indicating an error The error message says that the blank final variable empNo may not have been initialized This is because a variable that is declared final must be assigned an initial value somewhere either in the variable declaration itself or in the constructor 11 2 2 Implementing the constructor to create new employees Finally we have to create a constructor to create new employees The constructor has one argument the employee number Remember th
363. nstallation job h g NEW SDSNSAMP DSNTEJ1 Important If all you see are the schemas with no error message but no expansion you probably do not have DSNZPARM DESCSTAT set to YES see DSNTIJUZ on page 59 We found out the hard way YES is not the default in Version 7 Rerun the DSNTIJUZ job to update the value then issue SET SYSPARM LOAD DSNZPARM to load the changed DSNZPARMs 80 DB2 for z OS and OS 390 Ready for Java To give you a feel of what information is available in the WSAD data perspective we expand the Tables folder and the DB2 tables are displayed as Figure 5 21 illustrates 4 Data IBM WebSphere Studio Applicat File Edit Source Refactor Navigate Search Project Run Window Help ion Developer EFE E lE ES ue a lie 16 i lmstutst Judyclass java X amp gs amp amp a E Data Definition ES 29 Te 5624 6435 s tay judy Data Definition Navigator C3 Tables BA DSN8710 ACT ff DSN8710 DEPT ff DSN8710 EACT ff DSN8710 EDEPT ff DSN8710 EEMP ff DSN8710 EEPA ff DSN8710 EMP ff DSN8710 EMP PHOT ff DSN8710 EMPPROJA ff DSN8710 EPROJ ff DSN8710 EPROJACT ff DSN8710 MAP TBL ff DSN8710 PARTS BA DSN8710 PROJ f DSN8710 PROJACT B DSN8710 TCONA ff DSN8710 TDSPTXT FER nswazin Ios Ec Ec Ee Ee Ee Ee Ee Ee E E E E E E E
364. nts to the program 6 2 1 Creating the launch configuration To create a launch configuration select the Hello java file then click the drop down button beside the Running Man icon From the drop down menu select Run As Java Application WSAD will create a launch configuration for you and launch it immediately Chapter 6 Getting started with JDBC 109 Java IBM WebSphere Studio Application Developer File Edit Source Refactor Navigate Search Project Run Profile Window Help FHAA R F X RS E FE Outline 9 E o 4 2 Java Bean 5 uu 5624 6435 T Jo 3 JUnit Test Bl com ibm itso sg246435 jdbc impo 4 Run time Workbench impo m Gy JRE_LIB C Program Files IBM Web import java sql PreparedStatement 7 aro ih C Program Files IBM SQLLIB javald X build xml import java sql ResultSet jw H3 com ibm itso sg246435 jdbc import declarations Qo Hello BF ur i String PF user String BF password String e main String Sample test program to retrieve all employees in the EMP sample tables whose salary is in given range author Ulrich Gehlert tz public class Hello 37 Tasks 0 items 5 w x Description Resource In Folder Location Package Explorer Hierarchy com ibm itso sg246435 jdbc Hello java 5G24 6435 Figure 6 2 Running the application from WSAD The output from the
365. ny Judy Ruby Brown is a Consulting IT DB2 Specialist in the United States She has supported DB2 for OS 390 and z OS for fifteen years in the IBM Dallas Systems Center in the IBM Americas Technical Sales Support Organization Her areas of expertise include parallel sysplex and DB2 data sharing disaster recovery and high availability She has presented on these topics at the International DB2 Users Group IDUG in the US Europe and Asia Pacific as well as at the DB2 Technical Conferences and SHARE She holds a degree in Mathematics from the University of Oklahoma She previously co authored the SAP R 3 and DB2 for OS 390 Disaster Recovery publication Paul Tainsh is a DB2 Database Administrator with IBM Global Services Australia He has 14 years of experience in DB2 as a developer DBA and instructor He has been working with computers since 1980 and has worked with mainframes since 1982 in both Australia and England He previously wrote sections of DB2 UDB for z OS and OS 390 Version 7 Presentation Guide for the ITSO DB2 for z OS and OS 390 Ready for Java Figure 1 Paul Tainsh Bart Steegmans Judy Ruby Brown Ulrich Gehlert Thanks to the following people for their contributions to this project Julie Czubik The editor who edited this publication International Technical Support Organization Poughkeepsie Center Rich Conway Bob Haimowitz International Technical Support Organization Poughkeepsie Center Sigi Bigelis Bill Bi
366. o DB2 allowing database access across multiple platforms As well as changes to the drivers DB2 has been expanded to utilize the new technologies by providing services such as unicode support IEEE floating point data types and Java support in stored procedures and user defined functions Java application support on OS 390 and z OS In its brief life as a mainframe language Java has made huge inroads in that platform Java programs can be run in the Unix System Services environment of OS 390 and z OS in exactly the same way as they are run under any Unix system They can also be executed using JCL in the MVS batch environment using a Unix System Services utility program and they can be used to implement CICS and IMS transactions Perhaps the most significant area of support for Java programs on OS 390 and z OS is provided by WebSphere Application Server running on the mainframe platform which provides full J2EE support allowing the execution of Java elements such as JavaServer Pages and Enterprise Java Beans This publication covers the creation installation and actual execution of some of these elements and how they interact with DB2 for OS 390 or z OS DB2 for z OS and OS 390 Ready for Java An introduction to Java 2 Enterprise Edition The Java 2 Enterprise Edition J2EE is a specification that defines the set of technologies and their usage as building blocks to develop across enterprise applications This chapter discuss
367. o create IV public static void main Strina args I Constructors from superclass Inherited abstract methods ced Figure 6 1 New class wizard 102 DB2 for z OS and OS 390 Ready for Java 4 In the editor window that opens type the source code for our Hello application as shown in Example 6 1 changing the values for url user and password accordingly Example 6 1 Source code for Hello application pa im im im im im im pu ckage com ibm itso sg246435 jdbc port java math BigDecimal port java sql Connection port java sql DriverManager port java sql PreparedStatement port java sql ResultSet port java sql SQLException Sample test program to retrieve all employees in the EMP sample tables whose salary is in a given range author Ulrich Gehlert blic class Hello JDBC URL to the database private static final String url jdbc db2 wtsc63 itso ibm com 33756 DB7Y retrieveMessagesFromServerOnGetMessage TRUE User name to connect to the database private static final String user bartr1 Password for the database connection private static final String password bingo public static void main String args Connection conn null PreparedStatement stmt null ResultSet rs null try Load the JDBC driver Class forName com ibm db2 jcc DB2Driver Connect to the database server conn DriverMan
368. oGet method This is shown in Example 15 6 Example 15 6 Adapted doGet method public void doGet HttpServletRequest req HttpServletResponse resp throws ServletException IOException PrintWriter out resp getWriter out println DOCTYPE HTML PUBLIC W3C DTD HTML 4 01 Transitional EN gt out printIn HTML out printIn HEAD out printIn TITLE Employee listing lt TITLE gt out printIn HEAD out printIn BODY out printIn lt H1 gt Employee listing lt H1 gt try printTable out catch SQLException e throw new ServletException e getMessage e out printIn BODY out printIn HTML out close 15 3 Running the completed EmployeeList Servlet 256 By now we are ready to run the complete EmployeeListServlet Servlet again Right click our Servlet and select Run on Server WSAD will prompt you for the server on which you want to run the Servlet select the WebSphere Test Environment server created in Testing the Servlet on page 252 WSAD will display the embedded browser window again and invoke the Servlet Important When the browser window is already open you have to press the Refresh button SI on this window in order to force a refresh Normally the browser will now display a not very helpful error message namely Error 500 Failed to load target servlet EmployeeListServlet By examining the WebSphere log in the Console view we find t
369. ocol JDWP from a remote debugger connected to this port z OS system Debug JVM Remote Debugger Application c lt 9 Java Debug Interface JDWP E JDI over TCP IP i Istener thread Figure 6 11 Remote debugging architecture How exactly a JVM is enabled for remote debugging depends on the JVM implementation and on the environment that JVM is running in for example CICS or WebSphere Application Server Note For instructions on how to set up remote debugging in WebSphere Application Server refer to Assembling Java 2 Platform Enterprise Edition J2EE Applications SA22 7836 For CICS applications refer to Java applications in CICS SC34 6000 For stand alone applications you enable remote debugging via command line switches For the z OS JVM implementation the command line looks like this java Xdebug Xnoagent Xrunjdwp transport dt_socket server y suspend y address 8000 mainclass arguments Where suspend y instructs the JVM to suspend after loading the main class and wait for a debugger to attach Chapter 6 Getting started with JDBC 119 120 8000isthe TCP IP port number on which you want the debug listener thread to listen Instead of 8000 you can use any unused TCP IP port number greater than 1024 If you are working in a team it is probably a good idea to assign each team member a different port num
370. od liliis eee 210 11 12 EmployeeTest main after adding the call to delete 4 210 11 13 Employee update method 0 000 ee 211 11 14 Invoking the Employee update method 0 0 0 eee eee 211 11 15 Employee findAll method 0 0 liliis eee 212 11 16 Declaration of Employeelterator 0 ce ee 212 11 17 Employee fetch method 0 0 cee tae 213 11 18 testFindAll method i trenen ea EEEE eee 213 11 19 Invoking the testFindAll method 0 0 eee 214 11 20 Employee getPicture first version liliis 214 11 21 Employee getPicture second version s a suasa aeaaee 216 11 22 EmployeeTest testGetPicture onana anana 216 11 23 Employee createPicture 0 2c eres 217 11 24 Overloaded createPicture method liliis eese sess 218 11 25 EmployeeTest testCreatePicture llis 218 12 1 Setting properties using DriverManager getConnection String Properties 225 12 2 Creating and executing batched INSERT statements 0005 226 12 3 Using the extended client information API 00 0000 eee eee 227 12 4 Using the DB2 Universal Driver for SQLJ and JDBC monitoring API 229 12 5 Retrieving auto generated keys llle eese 231 13 1 Result of bind with online checking enabled vs disabled 236 14 1 Error handling in JDBC sssssssseeeese rn 240 1
371. ogram logic the problem that the two distinct technologies are mixed remains Although it made it quicker to develop a dynamic Web page the heavy mixing of HTML and programming logic created a maintenance nightmare similar to the days of pure Java Servlets As an alternative an architecture called Model 2 design was put forward This basically means that a JSP contains only the graphical Web elements within the page In this model the page is only responsible for the view which is how the data is presented to the browser Almost all programming logic is pushed back into pure Java programs which contain the business logic and interact with the JSPs to present the user interface 18 DB2 for z OS and OS 390 Ready for Java To extend the Model 2 design architecture the JSP specification now allows the creation of a Tag Library This library is used to store custom tags that are defined to the application and define a call to the Java code to perform the business logic All the Web developer needs to know is the tag and what function is to be run while the Java developer only codes the business logic for the function without writing presentation layer code Thus the separation of functions between Web developer and Java programmer are maintained and Web pages can be changed without pouring through Java code 2 4 5 JavaScript Despite the name JavaScript and Java have very little in common JavaScript is purely a scripting language for Web pa
372. om ibm itso sg246435 sqlj Hello 2 SET ARGS jdbc db2 DB7Y bart bingo 30000 50000 3 SET STDOUT tmp java stdout SET STDERR tmp java stderr JAVA EXEC PGM BPXBATCH REGION OK PARM SH amp JAVA amp CLASS amp ARGS 4 STDOUT DD PATH amp STDOUT amp SYSUID PATHOPTS OWRONLY OCREAT OTRUNC 5 STDERR DD PATH amp STDERR amp SYSUID PATHOPTS OWRONLY OCREAT OTRUNC Copy stderr and stdout to JES data set CPYOUT EXEC PGM IKJEFTO1 COND EVEN 6 HFSOUT DD PATH amp STDOUT amp SYSUID PATHDISP DELETE KEEP HFSERR DD PATH amp STDERR amp SYSUID PATHDISP DELETE KEEP STDOUT DD SYSOUT DCB RECFM VB LRECL 133 BLKSIZE 137 STDERR DD SYSOUT DCB RECFM VB LRECL 133 BLKSIZE 137 SYSTSPRT DD DUMMY SYSTSIN DD Chapter 8 Getting started with SQLJ 147 148 ocopy indd HFSOUT outdd STDOUT ocopy indd HFSERR outdd STDERR Notes on Example 8 2 on page 147 1 2 3 4 Invocation of the JVM executable program The main class of the Java program Command line arguments to the program Invokes BPXBATCH to run the JVM as a Shell script When running as a shell script at initialization time the profile of the user ID running the program is invoked Make sure the proper setup is done See Setting up the JDBC SQLJ environment variables on page 70 for details on setting up your profile for JDBC SQLJ usage 5 Creates HFS files
373. om a Java application The easy way is to read or write the LOB data in one large chunk using the getBytes or setBytes methods respectively for BLOB data or the get setString methods for CLOB data However this is not a good idea if the LOB data get very large which after all is the point of using LOBs since this uses a lot of memory Maybe you only want to figure out how long a LOB column s value is in this situation it is an obvious waste of resources to read the entire LOB data only in order to count the number of bytes or characters DB2 for z OS and OS 390 Ready for Java The alternative is to either use Java streams or the java sql Blob and java sql Clob interfaces Table 7 1summarizes the different JDBC methods to work with LOB data Note that you cannot construct a BLOB or CLOB object yourself they are Java interfaces not regular Java classes the JDBC runtime does that for you when retrieving LOB data Therefore the setBlob and setClob methods are useful only for copying the value of a LOB column from one row or column to another one Table 7 1 JDBC methods for use with LOB types Operation Using simple type Using LOB type BLOBs SELECT ResultSet getBytes ResultSet getBlob ResultSet getBinaryStream INSERT UPDATE PreparedStatement setBytes PreparedStatement setBinaryStream PreparedStatement setBlob CLOBs SELECT ResultSet getString ResultSet getClob ResultSet getCharacterStream INSE
374. ommand that we did when running it from the Windows command prompt Also note that we use the jdbc db2 wtsc63 itso ibm com 33756 DB7Y URL to address the DB2 for z OS subsystem However using this URL format means that we use the Type 4 driver using DRDA to talk to the local DB2 system Although it works fine performance wise this is not the best solution So instead of using the Type 4 driver we want to use the Type 2 driver Nothing is easier than that We just change the URL to jdbc db2 DB7Y and issue the following command java com ibm itso sg246435 sqlj Hello jdbc db2 DB7Y bart bingo 30000 50000 And the program still runs this time using the Type 2 driver using the RRS attachment to talk to the local DB2 system DB2 for z OS and OS 390 Ready for Java Note For things to work as they are described here you need set up the system properly This is described in more detail in Chapter 5 Setup on page 57 The other thing to note is that the URL has to be external to the program itself either through the use of a parameter as in our example or through an external property or an external DataSource definition 9 6 4 In summary As shown in the previous paragraphs development and deployment of sqlj programs is very easy and could be done in the following way 1 Users develop sqlj programs on a workstation platform using a nice integrated development environment like WSAD and do some initial testing of their database c
375. on process 155 Also enabling online checking is strongly recommended for performance reasons see the detailed discussion in Always customize with online checking enabled on page 236 If online checking is not enabled the DB2 optimizer may choose a poor access path due to lack of information resulting in badly performing queries 9 4 The DB2 profile binder As explained in The DB2 profile customizer on page 154 the DB2 profile customizer by default also binds the profile against the database producing or updating a database package for that profile If an existing application needs to be deployed against another database of the same family there is no need to recustomize the profile again You can just take the existing customized profile and bind it against the new database or DB2 for z OS subsystem To rebind an existing customized profile you can use the db2sql jbind tool Note Unfortunately at the time of writing rebinding without recustomizing is not supported by the SQLJ tooling in WSAD V5 1 9 5 The DB2 profile printer The DB2 profile printer db2sql jprint is a tool you can use to print the information contained in a serialized profile in human readable form Essentially what the profile printer does is to read a serialized profile de serialize it and print all profile entries including DB2 customization information if present Example 9 1 shows part of the db2sql jprint output for the customized p
376. on the mainframe In most DB2 installations different databases and subsystems are used to store the data in a development versus a production environment For example the test data is stored in a test subsystem called DB2T while the production database is run in the production subsystem DB2P In this case when an application is migrated from test to production the URL will have to be changed for all drivers when the DriverManager interface is used A way to avoid this problem is to externalize all references to the driver and connection and load it into the application at execution time You can do this by creating platform specific property files which contain entries for the driver and database and read these values in at runtime Java provides a class to read the properties file and access these entries the java util Properties class It can be used by all types of Java applications We show an example of this technique in Creating a test driver on page 204 For stand alone Java programs another way to externalize these values is to specify them as input arguments to the program in the command line For server Java programs this option is not available The driver and database details can be set in the initialization parameters for the application which can be accessed using the getlnitParameters method within the code The best way to maintain database connectivity is to avoid using the DriverManager altogether This is achie
377. on to Java servers An initial limitation of the stand alone Java application and the Web based Java applets was that they were both executed on the client machine and were limited to client based resources As you could never be sure what resources the client had the client based applications tended to go for the lowest common denominator solution which assumed minimal client access to resources This lead to static Web pages with limited appeal Java architecture was on the move towards server based solutions which as they were executed on the server opened up the browser to the power and resources that servers can provide Business logic was removed from the client side to the server The browser would handle the user interaction and pass any requests to the server application This application would contain all the business logic and database calls The application would then pass the results back to the browser in the form of HTML to be displayed One of the first solutions outside Java to achieve this was through the use of a Common Gateway Interface or CGI This solution always had performance issues Every time a CGI application is executed a new process is started on the host server The process of starting and stopping CGI applications is very expensive to the host machine If a lot of users are requesting the same process this can lead to large bottlenecks on the host machine Another Chapter 1 A brief history of Java 5 limitation
378. ons T Generate Default Bindings Install New Application o id ile ate default bindi f verride EE pee i Generate default bindings for o Resources Do not override existing bindings existing entries and over write them Security C Override existing bindings 9 Envi nt T 5 E Environme Virtual Host c 1 i The virtual host to be used for this System Administration Do not default virtual host name for web web module modules Troubleshooting Detault virtual host name for web modules default host Specific bindings file Browse li Optional location of pre defined bindings file Previous Cancel Figure 15 15 Install a new application 2 On the next window Figure 15 16 on page 273 all defaults are OK so just click Next In case you want to change the name of your application as it is known to WAS you can change the application name here The application name will show up later when we list the applications known to WAS to activate our newly installed application 272 DB2 for z OS and OS 390 Ready for Java WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Back search Favorites meda lt 4 B 5 SI E Address Address http http wtsc63 itso ibm com 9080 admin secure lagan do z Go Links WebSphere Application Server Administrative Console Version 5 Home Save Prefer
379. ontext getExecutionContext Returns the default execution context for this connection context See 10 7 Execution contexts on page 195 for a discussion of execution contexts and their relation to connection contexts 10 7 Execution contexts Each SQLJ statement is associated either implicitly or explicitly with an execution context This is an instance of the sqlj runtime ExecutionContext class which in turn is part of the SQLJ runtime The execution context allows you to gt Control the execution of statements running under that context For example you can set a query timeout for SQLJ statements If the timeout is exceeded an SQLException is thrown if supported by the SQLJ implementation gt Retrieve information about the status of the last SQLJ statement executed under that context For example you can retrieve the number of rows affected by the last SQL statement gt Cancel a statement currently executing under that context The cancel method allows you to cancel the operation currently running under a given execution context Obviously you can call this method only from a thread other than the one executing the statement Restriction At the time of writing query timeout and asynchronous cancel is supported on DB2 UDB Version 8 for Unix and Windows only It will also be supported for Type 4 connections accessing a DB2 for z OS Version 8 gt Execute statements in batch Using the setBatching setBatch
380. ood news is that WSAD generates this script for you A brief introduction to Ant As the Ant user manual puts it Apache Ant is a Java based build tool In theory it is kind of like make without make s wrinkles Why is it called Ant Because it is a little thing that can build big things If you are familiar with make you probably know how difficult it can be to write a makefile that works on each system you are targeting Also make is purely timestamp based it compares Chapter 9 The SQLJ program preparation process 157 158 the timestamps of targets against the timestamps of dependents to decide whether a target has to be rebuilt Ant on the other hand is platform independent and much more flexible and extensible It is centered around the concept of tasks which are Java classes that perform a certain job Ant comes with a number of useful predefined tasks for example to compile Java source code to do FTP transfers to perform unit tests and many more Tasks are executed under the control of targets To describe what tasks must be performed in order to build a given target you write a buildfile in XML Each buildfile must specify at least one target of course you can have many targets and those targets may have dependencies For example in order to export Java class files to another system you first have to compile the corresponding Java sources files so the export target will be dependent on the compile target WS
381. or Java dp web IBM WebSphere Studio Application Developer File Edit Navigate Search Project Run Window Help J ies a e amp 9 AAA E X x Sd Palette x x m palette is not available For ER Y his editor AE 15 DefaultEAR P Ya 3G24 6435 theme amp lib X ibm X ibm gs I 15G24 6435 Web wy Web Site Configuration 4j Web Deployment Descriptor 29 JavaSource E G webContent gt META INF Cs MaNrFEST MF 8 Master css WEB INF amp classes eh yml Project Navigator TGallery Struts Explorer web bnd xmi web ext xmi Attributes No attributes are available B Quick Edit Hi Attributes styles Thumbnails Quick Edit Tasks Colors Servers Palette Snippets 5G24 6435 Web Figure 15 2 The WSAD Web perspective 15 2 Creating the EmployeeList Servlet Now we are ready to create our first Servlet called EmployeeListServlet As the name suggests it will eventually display the contents of the employee table In the code for the Servlet we make use of the Employee class created in Chapter 11 SQLJ revisited on page 199 We create the Servlet using the New Servlet wizard Highlight the Java Source folder and select File gt New gt Servlet Enter com ibm itso sg246435 web for the Java package name and EmployeeListServlet in the Class name field Since our Ser
382. or class declaration There are three keywords predefined by the SQLJ standard gt sensitivity value must be one of sqlj runtime ResultSetIterator SENSITIVE sqlj runtime ResultSetIterator INSENSITIVE or sqlj runtime ResultSetIterator ASENSITIVE Controls whether the iterator is aware of changes to the underlying base table For details see the Application Programming and SQL Guide SC26 9933 gt holdability value must be true or false See Holdable iterators on page 186 for more details gt updateColumns value must be a comma separated list of column names See Positioned UPDATE and DELETE on page 186 for more details gt dynamic value must be true or false Controls whether a sensitive scrollable cursor is dynamic A true value only has an effect on a server that supports dynamic sensitive scrollable cursors like DB2 for z OS Version 8 In addition an SQLJ implementation may accept additional keywords allowing for vendor specific extensions 10 2 Host variables and expressions You use host variables and host expressions to pass arguments to SQL statements A host variable is simply a Java identifier which could refer to a method parameter a local variable or a field Host variables are prefixed with a colon Unlike other languages with embedded SQL support SQLJ also supports host expressions A host expression is any legal Java expression that yields a value Like host variables host expressions are pr
383. ositioned on is likely to be locked unless lock avoidance is used Other applications will not be able to update the row or even to read it if the iterator has positioned UPDATE enabled Example 14 2 Bad style Iterator not closed in case of exception NOT RECOMMENDED iterator not closed in case of exception void foo throws SQLException MyIterator iter sql iter SELECT FROM while true sql FETCH iter INTO if iter endFetch break pes iter close Chapter 14 Error handling and diagnosis 241 In Example 14 3 we use the try finally construct of Java to ensure that the iterator will always be closed even when an exception occurred The Java language specification guarantees that a finally block is always executed no matter whether the corresponding try block completes successfully or raises an exception Example 14 3 try finally construct to ensure proper closing of iterator void foo throws SQLException MyIterator iter null try fsql iter SELECT FROM while true sql FETCH iter INTO if iter endFetch break finally if iter null iter close Be especially careful when using holdable iterators see Holdable iterators on page 186 Holdable iterators remain open until you explicitly close them or a ROLLBACK occurs 14 4 DB2 specific error handling 242 Suppose you received an SQLException that says that you tried
384. oyee number B EMPNO CHAR B FIRSTNME VARCHAR B MIDINIT CHAR B LASTNAME VARCHAR Q3 WORKDEPT CHAR PHONENO CHAR rs closel HIREDATE DATE stmt closell JOB CHAR eatch Throwable e EDLEVEL SMALLINT e printStackTracei l SEX CHAR BIRTHDATE DATE SALARY DECIMAL BONUS DECIMAL COMM DECIMAL ra DSN8710 EMP PHOTO RESUME ra DSN8710 EMPPROJACT g ps EE n T 411 Servers Server Status 4 Tasks DB Output Servers Co oo oo oo CIN CIN CIN oo oo N8710 EPROJ SNR8710 FPRTACT Figure 5 22 The columns of the sample EMP table You can see the column names with their data types as well as primary and foreign key information These columns may be used directly by WSAD application developers to ensure that the right column name and type is used in a program You can also look at the sample contents of the table by right clicking the DSN8710 EMP table and selecting Sample Contents from the drop down box 5 6 WebSphere for z OS datasource setup 82 In this section we do not describe the entire setup of a WebSphere Application Server on z OS We assume that this has been taken care of by your systems programmer In this section we focus on how to set up a datasource in WebSphere so that it can be used by our Servlet application that we will develop in Chapter 15 Using Servlets to access DB2 on page 249 As mentioned before we use the brand new IBM
385. page Java applets on the other hand are programs written in the Java language and are called from within HTML pages or run as stand alone applications JDBC Java Database Connectivity In the JDK the specification that defines an API that enables programs to access databases that comply with this standard JIT See Just In Time Compiler JNDI See Java Naming and Directory Interface JNI See Java Native Interface JRE See Java Runtime Environment Just In Time compiler JIT A platform specific software compiler often contained within JVMs JITs compile Java bytecodes on the fly into native machine instructions thereby reducing the need for interpretation JVM See Java Virtual Machine Kerberos A network authentication protocol that is designed to provide strong authentication for client server applications by using secret key cryptography Large object LOB See LOB Link edit To create a loadable computer program using a linkage editor Glossary 321 Linker A computer program for creating load modules from one or more object modules or load modules by resolving cross references among the modules and if necessary adjusting addresses In Java the linker creates an executable from compiled classes Load module A program unit that is suitable for loading into main storage for execution The output of a linkage editor LOB A sequence of bytes representing bit data single byte characters double byte characters or a mix
386. penConnection Properties properties new Properties properties put user bartr1 properties put password secret properties put clientWorkstation InetAddress getLocalHost getHostName properties put clientUser System getProperty user name Connection conn DriverManager getConnection url properties return con 12 3 Functionality enhancements The DB2 Universal Driver for SQLJ and JDBC provides more functionality than the older drivers for example it supports scrollable cursors batch updates and savepoints as discussed in the following sections 12 3 1 Scrollable cursor support Scrollable cursor support refers to the possibility of moving a result set s cursor backward as well as forward This is especially useful for GUI applications when users want to browse a result set backward and forward Without scrollable cursors the application had to either cache the result set in memory or submit the query again when the user scrolled backward This feature introduced in JDBC 2 0 is now supported in the DB2 Universal Driver for SQLJ and JDBC 12 3 2 Batch updates A batch update is a set of multiple update statements that is submitted to the database for processing as a batch Sending multiple update statements to the database together as a unit can in some situations be much more efficient than sending each update statement separately This ability to send updates as a unit referred to as the b
387. pport this separation of concerns The next step in the development of Java was to split these areas of responsibility through the introduction of the JavaServer Pages JSP specification This model allows Web designers to control and concentrate on the Web design and the code developers to provide the designers the resources needed to create these pages while still using the performance and system management benefits of the Java Servlet Basically a JavaServer Page is an HTML page that contains Java code to execute program logic to generate dynamic Web pages This design allows Web designers to build the HTML to control layout while the developer adds the necessary logic needed to run the page logically splitting the page development tasks A key feature of the JavaServer Page is that it is an extension of the Java Servlet not a replacement In fact whenever a JavaServer Page is executed it is first dynamically compiled into a Java Servlet and it is this code that is run on the server 1 1 4 The Internet and the enterprise The needs of the large systems development community to manage the new server based application architecture and the vendor community to offer products to fulfill this architecture along with the development of new Java components culminated in the development of the Java 2 Technology Enterprise Edition J2EE standard The focus of this standard is to support a middle tier where the business logic runs It defines
388. program goes to the Console view which opens automatically whenever a Java program produces output by writing to System out or System err Our first attempt to run the sample program however produces an error message java lang ClassNotFoundException com ibm db2 jcc DB2Driver see Figure 6 3 This means that the Java Virtual Machine cannot find the bytecode for the indicated class in our case for the JDBC driver Sphere Studio eclipse jre bin javaw exe 11 13 02 11 18 En java lang ClassNotFoundException com ibm db2 jcc DB2Driver at java net URLClassLoader findClass lURLClassLoader java 240 at java lang ClassLoader loadClassiClassLoader java 514 at sun misc Launcherf AppClassLoader loadClass Launcher java 441 at java lang ClassLoader loadClass ClassLoader java 446 at java lang Class forNamel Native Method at java lang Class forName Class java 142 iu Tasks Console Figure 6 3 ClassNotFoundException due to missing JDBC driver The problem is that the program requires the JDBC driver at runtime but not at compile time In other words the JDBC driver implementation is not needed to compile the program because we only use the classes and interfaces in the java sql package which come with the Java standard libraries but it is certainly needed to run it To put additional libraries on the runtime classpath we have to modify the launch configuration created in the previous step 110 DB2 for z O
389. program preparation process 163 j DB7Y Thread Details Primary Authorization BART Thread Details View Tools Help amp e Gurrenttirme setting zoom I 8 23 03 12 14 20 AM amp z DBRMiPackage E Suspensions SQL Statement Times Location DB7Y Data base name NIP E Class 1 2 3 j Suspensions Collection ID 56246435 Data base ID NIP Other Program name HELLO2 Page Set name 16448 Locking Consistencytoken x 65424F68 Page Set ID NIP RI Version NIP Page number within pageset N P SQL Activity DML Stored procedure Elapsed Time NIP E DCL Statement type CLOSE CPU time NIP DDL Statementnumber 1 Total Number of Getpages NIP Miscellaneous Thread status In DB2 Total Synch Read I O NIP Buffer Manager Statement T SQL statement text Distributed Data Data Capt Loggir DECLARE DB2JCCCURSOR1 CURSOR FOR SELECT LASTNAME amp Query Parallelism FIRSTNME SALARY FROM D3N8710 EMP WHERE SALARY BETWEEN E Parallel Threads H H AND H H ORDER BY LASTNAME FIRSTNME Data Sharing Locl Group Buffer Pool Nested SQL Activi Figure 9 8 Currently execution SQL statement If that is not proof enough you can also look at the number and type of DML statements executed by the Hello program Figure 9 9 on page 165 As you can see no prepares were executed 164 DB2 for z OS and OS 390 Ready for Java j DB7Y Thread Details Primary Authorization BART Thread Details View Tools Help
390. programming language uses to access different forms of tabular data as well as some hierarchical systems The list includes full blown relational databases such as DB2 down to spreadsheets and flat files The JDBC specifications were developed by Sun Microsystems together with relational database providers such as IBM and Oracle to ensure the portability of Java applications across databases and platforms At this time there have been three major releases of the JDBC API specification the last one being JDBC 3 0 which was released in February 2002 The APIs are a set of classes and related methods that support full SQL functionality The classes fall into three categories gt Connecting to a database communicating the data layouts or metadata to the application and then processing the data within that database gt Exception processing or error handling gt Translating the native database data types to Java classes The API is defined in the classes and interfaces that make up the Java packages gt java sql gt javax sql An interface is a Java construct that looks like a class but does not provide an implementation of its methods In the case of JDBC the actual implementation of the JDBC interfaces is provided by the database vendor as a JDBC driver This may sound complex but in fact it provides portability in that all access using JDBC is through standard calls with standard parameters Thus an application can be coded
391. properties method serverName See Improved security for DB2 authentication on page 226 portNumber The TCP IP port number where the DRDA server listens for connection requests to this data source The default value is 446 databaseName gt For the Type 4 driver the actual database name and not the locally catalogued database name The Universal JDBC Type 4 driver does not rely on information cataloged in the DB2 database directory For the Type 2 driver this can be the actual database name if serverName and portNumber are specified or the locally catalogued database name The Type 2 driver can connect directly using TCP IP or rely on information cataloged in the DB2 database directory This applies to LUW logWriter See Tracing on page 244 traceFileAppend If set to true causes the trace to be appended to the trace file otherwise the file is overwritten See Tracing on page 244 fullyMaterializeLobData Determines whether the LOB data flowing from the server to the client is blasted true or streamed false resultSetHoldability For DB2 targets the default is HOLD CURSORS OVER COMMIT For Cloudscape Network Server the default is CLOSE CURSORS AT COMMIT currentPackageSet This property is used in conjunction with the db2jdbcbind collection option which is given when the JDBC CLI package is bound during DB2 installation Chapter 12 The DB2 Universal Driver 223 Property key securityMechanism See
392. provider 88 using JDBC or SQLJ 42 USS 30 58 68 profile 71 session 68 V VT 68 WwW WAS administrative console 270 creating a WAR file 269 Setting up system variables 83 WAS Administrative Console 83 WAS for z OS 278 wasNull 123 web archive file 269 WebSphere 40 WebSphere Application Server 8 54 119 WebSphere for z OS 269 WebSphere Studio Application Developer 32 48 54 WebSphere Test Environment 252 WLM 61 Application Environment 63 WSAD 32 48 54 102 145 152 157 249 278 285 code assist feature 252 create project 102 data perspective 74 debugger 118 setting up a data source 258 setup 73 WebSphere Test Environment 257 workbench 102 workspace 102 WSAD V5 1 73 X XHTML 282 XML 18 21 Index 335 336 DB2 for z OS and OS 390 Ready for Java DB2 for z OS and 08 390 Ready for Java Redbooks DB2 for z OS and 08 390 Ready for Java Setting up your Java DB2 environment Easy to use examples including using SQLJ with WSAD V5 1 Java DB2 usage hints and tips The earth is flat The earth is the center of the universe Men shall never fly Java will never work properly on the mainframe All four statements had a lot of advocates for a long time but all of them turned out to be wrong In this IBM Redbook we show how Java and DB2 for z OS and 05 390 can work together and form a strong combination that can run your mission critical enterprise applications This publicatio
393. pter In its most general form an iterator declaration looks like this sql modifiers iterator ClassName implements interface list with keyword with list column list Where gt interface listis a comma separated list of Java interface names just like in a Java class declaration gt keyword with listis a comma separated list of keyword value pairs keywordl valuel keyword2 value2 column listis either a list of column types for positioned iterators or column types and column names for named iterators as described above The implements interface list clauses cause the generated iterator class to implement the interfaces listed in the clause and may also cause the SQLJ translator to generate additional methods in the iterator class There are two interfaces predefined by SQLJ sqlj runtime ForUpdate which marks the iterator as updateable gt sqlj runtime Scrollable which marks the iterator as scrollable An implementation is free to support additional interfaces Important You must code the fully qualified interface name using an unqualified name does not work even when you add an import statement for the interface This is currently a limitation but it will be addressed in future versions DB2 for z OS and OS 390 Ready for Java The optional with clause causes a public static final variable in other words a constant of the appropriate type and the supplied value to be inserted into the generated iterat
394. r identity col INTO orderID FROM FINAL TABLE INSERT INTO my order table col list VALUES coTumn values h 232 DB2 for z OS and OS 390 Ready for Java 13 Performance topics In this chapter we discuss several hints and tips for optimizing the performance of JDBC and SQLJ applications We discuss the following gt General performance recommendations applying to both JDBC and SQLJ gt SQLJ performance considerations gt System level performance tuning Copyright IBM Corp 2003 All rights reserved 233 13 1 General performance recommendations Let us start by looking at over some general Java DB2 recommendations that will help you to obtain better performance 13 1 1 Use static SQL wherever possible Although dynamic statement caching see Turn on DB2 dynamic statement caching on page 238 relaxes the situation somewhat static SQL is generally much faster than dynamic SQL because the SQL parsing and access path calculation is done at compile time rather than at runtime In some situations however JDBC can be the better choice not from a performance viewpoint but from a functionality point of view For example a GUI application may allow the user to construct queries interactively with a large number of options that just do not allow every possible query to be coded statically 13 1 2 Turn auto commit off By default when you open a database connection via the DriverManager class that connection has the a
395. r2 gt JV SG24 6435 static war g adminconsole_ gt m ivtApe gt Figure 15 23 Start the application If everything is OK the application status turns into a green arrow like the other applications We are now ready to test 15 7 4 Test the application To test the application open another browser window Specify the following URL to invoke our Servlet http wtsc63 itso ibm com 9080 itso EmployeeListServlet The result should be something similar to Figure 15 24 on page 278 Note that the context root that we specified on the initial window when installing the new application is part of the URL that you specify to invoke the Servlet Chapter 15 Using Servlets to access DB2 277 Important This example clearly shows the true portability of Java applications including the new JCC driver We developed and tested our application in WSAD on a workstation We just exported the files and deployed them on a z OS platform into a WebSphere Application Server and we are up and running We did not have to touch the application at all The exact same code runs on WSAD as inside our WAS on z OS We even switched the driver type under the covers without any changes to the application Remember that when testing with WSAD we used a T4 driver to connect to the DB2 for z OS system DB7Y When running under the control of WAS for z OS we use a Type 2 connection to the same DB7Y system Switching is tran
396. racing can also be turned on from outside your program provided that the JDBC URL is not hard coded into the program it could be specified on the command line in a properties file or in a DataSource definition Use the syntax and property keys described in Setting connection properties in the URL on page 222 To find out the value for the property key traceLevel combine the trace levels you want enabled and convert the result to an integer For example to trace statement calls result set meta data and parameter meta data use TRACE STATEMENT CALLS TRACE RESULT SET META DATA TRACE PARAMETER META DATA 0x0002 0x0080 Ox0100 0x0182 386 So the URL looks like this jdbc db2 your server name port SSID traceFile jcctrace log traceLevel 386 Again the default for traceLevel is TRACE ALL if you specified a trace file name but no trace level Tip When the trace file name contains colons you have to enclose it in double quotes jdbc db2 your server name port SSID traceFile C temp jcctrace log traceLevel 386 246 DB2 for z OS and OS 390 Ready for Java Part 4 Accessing DB2 from Web applications In the previous chapters we showed you how to develop and run stand alone Java applications for DB2 In this part we show you how to access DB2 from a Web server environment using the Java Servlet and JavaServer pages technologies Copyright IBM Corp 2003 All rights reserved 247 248 DB2 for z OS and OS 390 Ready
397. ract String getSqlErrmcTokens Returns the error message tokens as described by the DB2 SQLCA sglerrmc field Each element in the returned String contains one of the sglerrmc tokens For example if you did not supply a value for a NOT NULL column in an INSERT statement the first and only element in the array will contain the column name A null value is returned if the value of the sglerrmc field is not relevant gt getSqlErrmc public abstract String getSqlErrmc Returns the error message tokens as a String delimited with semicolons A null value is returned if the value of the sqlerrmc field is not relevant gt getSqlErrp public abstract String getSqlErrp Returns the name of the DB2 module that issued the error or null gt getSqlErrd public abstract int getSqlErrd Chapter 14 Error handling and diagnosis 243 Returns error information as described by the DB2 SQLCA sglerrd field gt getSqlWarn public abstract char getSqlWarn Returns warning information as described by the DB2 SQLCA sqlwarn field as a fixed size char 11 array gt getSqiCode public abstract int getSqlCode Returns the SQLCODE same as SQLException getErrorCode gt getSglState public abstract String getSqlState Returns the SQL state same as SQLException getSQLState gt getMessage public abstract String getMessage Returns the server error message text Same as SQLException getMessage Remember also
398. rapbook page 2 Type the code above replace serfile with the name of a profile in your file system The class names must be fully qualified the easiest way to do this is to hit Ctrl Space after the class name 3 Select all press Ctrl A then select Run Inspect An Inspector window opens and allows you to look at the profile s contents Another way to look at the contents of a serialized profile is to use the db2sql jprint utility For more information see The DB2 profile printer on page 156 As mentioned before serialized profiles are platform independent in other words their format and contents are standardized precisely because they are created by converting Java objects into their serialized form Chapter 9 The SQLJ program preparation process 153 On the other hand there must be a way to place vendor specific information into the profiles Otherwise it would be impossible to support vendor specific extensions This is where profile customizations enter 9 3 The DB2 profile customizer 154 The profile customizer db2sqljcustomize adds customization information to each entry in the profile As mentioned before this customization information is DBMS specific and additional program preparation steps may be necessary before the customized program can successfully be run against the database In the case of DB2 during profile customization a DB2 package is created in the database catalog actually four package
399. re 15 11 Output from running the EmployeeList Servlet We did it 15 4 Creating the EmployeeDetail Servlet The EmployeeDetail Servlet displays detail information about an employee To create the EmployeeDetail Servlet follow the same steps as for the EmployeeList Servlet Example 15 8 shows the doGet method and a helper method that prints one row of employee data However we have one problem here How do we tell the Servlet which employee to display For the time being we hardcode an employee number see note 1 below Example 15 8 EmployeeDetailServlet doGet first version package com ibm itso sg246435 web import java io IOException import javax servlet Servlet import javax servlet ServletException import javax servlet http HttpServlet Chapter 15 Using Servlets to access DB2 263 264 import javax servlet http HttpServletRequest import javax servlet http HttpServletResponse import com ibm itso sg246435 sqlj Employee import java io PrintWriter version 1 0 author public class EmployeeDetailServlet extends HttpServlet implements Servlet private static Employee Ctx ctx see javax servlet http HttpServlet void javax servlet http HttpServletRequest javax servlet http HttpServletResponse i public void doGet HttpServletRequest req HttpServletResponse resp throws ServletException IOException PrintWriter out resp getWriter String empno 000130 1 out printl
400. re driver time monitor getCoreDriverTimeMicros ApplicationTimeMillis is blue line in figure above System out println Application time ms monitor getApplicationTimeMillis Chapter 12 The DB2 Universal Driver 229 You can choose to either reset or to accumulate times when starting the monitoring using DB2SystemMonitor RESET TIMES or DB2SystemMonitor ACCUMULATE TIMES on the monitor start method respectively Note that the various getTime methods may throw an SQLException if the driver cannot provide the information requested DB2SystemMonitor prerequisites Although already shipped in an earlier FixPak the DB2SystemMonitor is only fully operational with DB2 for LUW Version 8 FixPak 4 In addition to be able to get accurate times for the core driver and network I O times your JVM has to be at a certain code level as well Currently these JVM enhancements have only been implemented as a feature of IBM s JVMs more specifically in gt JDK 131 SR 5 available now for non z OS platforms gt JDK131 cm131s and JDK 141 SR1 cm141 for the z OS platform At the time of writing of this publication the Sun JVM 1 4 1 does not have this feature If the DB2SystemMoni tor does not find the accurate timer functionality in the JVM it throws an SQLException com ibm db2 jcc a SqlException Network IO time in microseconds is not available Or com ibm db2 jcc a SqlException Core driver time in microseconds is not
401. re you do this Attention As mentioned above we copied the class files shipped with DB2 for LUW because the official z OS Type 4 Universal Driver was not available when this book was written This technique although it worked fine for our examples is NOT officially supported You should NOT attempt to use the DB2 Universal JDBC Driver shipped with DB2 for Linux Unix and Windows on a z OS platform Although it may work as it did for us itis not supported and may lead to unpredictable results The new driver has been written from scratch and is not based on any of the existing JDBC CLI drivers The Universal Driver is architected as an abstract JDBC processor that is independent of driver type connectivity or target platform The IBM DB2 JDBC Universal Driver is an architecture neutral JDBC driver for distributed and local DB2 access Since the Universal Driver has a unique architecture as an abstract JDBC state machine it does not fall into the conventional driver type categories as defined by Sun Because the Universal Driver is an abstract machine driver types become connectivity types This abstract JDBC machine architecture see Figure 3 2 on page 29 is independent of any particular JDBC driver type connectivity or target platform allowing for both all Java connectivity Type 4 or JNI based connectivity Type 2 in a single driver A single Universal Driver instance is loaded by the driver manager for both Type 4 and Type 2 implementat
402. rectly to the remote database server DB2B through DRDA The Universal Type 4 driver implements DRDA Application Requester functionality You do not need to buy a DB2 license for DB2A when using the DB2 Universal Database Driver for z OS Java Edition It is sufficient to have a DB2 license for DB2B z OS image s z OS image Figure 3 8 z OS Application Connectivity to DB2 for z OS and OS 390 You are only authorized to use this feature when connecting an application running on z OS or OS 390 to DB2 UDB for z OS and OS 390 V7 or DB2 for z OS V8 running in a separate LPAR on the same server as the application or running on a different z OS or OS 390 server but only between z OS and or OS 390 environments The z OS Application Connectivity to DB2 for z OS and OS 390 feature provides connectivity from a z OS or OS 390 remote partition or system only For access from any other operating system or platform including z Linux to DB2 for z OS and OS 390 you must obtain a separate license of the edition of DB2 Connect that is appropriate for your environment This feature can only be ordered for DB2 UDB for z OS and OS 390 V7 or DB2 for z OS V8 It ships as a separate FMID HDDA210 DB2 UDB for OS 390 and z OS V7 servers do not have built in support for distributed transactions that implement the XA specification When accessing a DB2 V7 the DB2 Universal JDBC Driver supports distributed transactions two phase commit but has to em
403. red DB2 for z OS changes to enable the Universal Driver 58 5 2 Workload Manager WLM ssssseeee ete eee 61 5 9 Unix System Servi66s a2 2 ons ped ake Leiq dee twee a av dane van Goes Sed 68 5 3 1 Setting up a USS Session yerum pe tends Heed Lied a EENE d 3 ame uon wed 68 5 3 2 Setting up the JDBC SQLJ environment variables llis 70 5 4 DB2 Universal Driver Setup for a Windows environment 0 5 71 5 5 WSAD Setup ustedes ex aa eo d eR Edo vache dd Ru Reis 73 5 5 1 Using the data perspective 74 5 6 WebSphere for z OS datasource setup iiliielieseeeess 82 5 6 1 Log onto the WAS Administrative Console lllllllllllll less 83 5 6 2 Setting up system variables 0 ccc teen eee 83 Part 3 Putting it all together 0 0 ccc teas 99 Chapter 6 Getting started with JDBC 0 0 2 0 c eee 101 6 1 Creating the project 0 0 0 ccc tees 102 6 1 1 Loading the JDBC driver 105 6 1 2 Establishing the connection 0 0 00 eee 105 6 1 3 Preparing an SQL statement for execution 2 6 0 0 0 105 iv DB2 for z OS and OS 390 Ready for Java 6 1 4 Populating parameter markers l l 106 6 1 5 Executing the statement llle 106 6 1 6 Processing the result set llle 106 6 1 7 Cleaning up resources 0 00 cette eee 108 6 2 Running the Hello application from WSAD 06000 0 cee een 109 6 2 1
404. reley Delmar Blevins Curt Cotner Heather Lamb Peter Miller Becky Nin Paul Ostler Peggy Rader Tom Toomire John Vonau IBM Silicon Valley Laboratory San Jose Connie Tsui IBM Toronto Lab Canada Preface xxiii Ulrich Kawald Dresdner Bank Frankfurt am Main Germany Become a published author Join us for a two to six week residency program Help write an IBM Redbook dealing with specific products or solutions while getting hands on experience with leading edge technologies You ll team with IBM technical professionals Business Partners and or customers Your efforts will help increase product acceptance and customer satisfaction As a bonus you ll develop a network of contacts in IBM development labs and increase your productivity and marketability Find out more about the residency program browse the residency index and apply online at ibm com redbooks residencies html Comments welcome xxiv Your comments are important to us We want our Redbooks to be as helpful as possible Send us your comments about this or other Redbooks in one of the following ways gt Use the online Contact us review redbook form found at ibm com redbooks gt Send your comments in an Internet note to redbook8Gus ibm com Mail your comments to IBM Corporation International Technical Support Organization Dept QXXE Building 80 E2 650 Harry Road San Jose California 95120 6099 DB2 for z OS and OS 390 Ready for Java
405. resource provider Resource Adapters Security Environment System Administration Classpen DB2JCC_DRIVER_PATHyiclassesidb2jcc jar LA list of paths or JAR file names Troubleshooting DB2JCC_DRIVER_PATH classesidb2jcc_lic which together form the location lense_cisuz jar for the resource provider classes DB2JCC_DRIVER_PATH classes sqjj zip Classpath entries are separated by using the ENTER key and must not contain path separator characters such as or Classpaths may contain variable symbolic names which can be substituted using a k Figure 5 32 User defined JDBC Provider variables Once you have filled in this information click Apply and then Save to complete the creation of the new JDBC provider Bak gt O A Bsearch Favorites media 4 G gt 5 H Address ja http vwtsc63 itso ibm com 9080 admin secure logon do Go WebSphere Application Server Version 5 Home Save Preferences User ID bart cel111 Servers Applications E Resources JDBC Providers Generic JMS Providers WebSphere JMS Provider WebSphere MQ JMS Provider Mail Providers Resource Environment Provid URL Providers Resource Adapters Security Environment System Administration Troubleshooting Administrative Console e Logout Classpath S DB2JCC DRIVER PATHy classes db2jcc jar S DB2JCC DRIVER PATHj classes db2jcc lic ense cisuz jar DB2UCC_DRIVER_PATH icl
406. return public Short getEducationLevel return educationLevel return public String getEmpNo return empNo return Appendix B Source code of sample programs 297 public String getFirstName return firstName return public Date getHireDate return hireDate return public String getLastName return lastName return public boolean isMale return male return public String getMiddleInitial return middleInitial return public BigDecimal getSalary return salary param short1 public void setEducationLevel Short short1 educationLevel shortl param string public void setFirstName String string firstName string param date public void setHireDate Date date hireDate date 298 DB2 for z OS and OS 390 Ready for Java param string public void setLastName String string lastName string param b public void setMale boolean b male b param string public void setMiddleInitial String string middleInitial string param decimal public void setSalary BigDecimal decimal salary decimal param String public Employee String empNo this empNo empNo toStr
407. rofile of our simple Hello program Example 9 1 Output of ab2sqljprint printing contents of profile com ibm itso sg246435 sqlj Hello SJProfileO created 1059525326023 2003 07 29 associated context is sqlj runtime ref DefaultContext profile loader is sqlj runtime profile DefaultLoader 32ae0b42 contains 1 customizations 1 com ibm db2 jcc sqlj Customization 2eaa4b42 original source file Hello sqlj contains 1 entries profile com ibm itso sg246435 sqlj Hello SJProfileO entry 0 sql SELECT LASTNAME FIRSTNME gt SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN AND ORDER BY LASTNAME FIRSTNME E line number 45 PREPARED_STATEMENT executed via EXECUTE_QUERY role is QUERY descriptor is null contains 2 parameters 1 mode IN java type java math BigDecimal java math BigDecimal sql type NUMERIC name min marker index 152 2 mode IN java type java math BigDecimal java math BigDecimal sql type NUMERIC name max marker index 160 result set Type is POSITIONED RESULT 156 DB2 for z OS and OS 390 Ready for Java result set name is com ibm itso sg246435 sq1 j Hello EmployeeIterator contains 3 result columns 1 mode OUT java type java lang String java lang String sql type VARCHAR name getColl marker index 1 2 mode OUT java type java lang String java lang String sql type VARCHAR name getCol2 marker index 1 3 mode OUT java type java math BigDecimal java math BigDecimal sql type NU
408. roperly see DB2 Universal Driver Setup for a Windows environment on page 71 for details The command shown in Example 5 1 binds the JDBC packages into a collection called NULLID the default Example 5 1 Invoking the DB2Binder utility java com ibm db2 jcc DB2Binder url jdbc db2 wtsc63 itso ibm com 33756 DB7Y user bart password bingo Bind to jdbc db2 wtsc63 itso ibm com 33756 DB7Y under collection NULLID Package SYSSTAT Bind succeeded Package SYSLH100 Bind succeeded Package SYSLH200 Bind succeeded Package SYSLH300 Bind succeeded Package SYSLH400 Bind succeeded Package SYSLN100 Bind succeeded Package SYSLN200 Bind succeeded Package SYSLN300 Bind succeeded Package SYSLN400 Bind succeeded Package SYSLH101 Bind succeeded Package SYSLH201 Bind succeeded Package SYSLH301 Bind succeeded Package SYSLH401 Bind succeeded Package SYSLN101 Bind succeeded Package SYSLN201 Bind succeeded Package SYSLN301 Bind succeeded Package SYSLN401 Bind succeeded Package SYSLH102 Bind succeeded Package SYSLH202 Bind succeeded Package SYSLH302 Bind succeeded Package SYSLH402 Bind succeeded Package SYSLN102 Bind succeeded Package SYSLN202 Bind succeeded Package SYSLN302 Bind succeeded Package SYSLN402 Bind succeeded DB2Binder finished T2 Universal Driver setup In this section we discuss T2 Universal Driver setup Driver properties When using the T2 driver you are usi
409. roperties Properties properties new Properties properties load propsFile propsFile close Get driver property and load the driver String driver properties getProperty driver com ibm db2 jcc DB2Driver Class forName driver String url properties getProperty url Connection conn DriverManager getConnection url properties Set up the connection context with autocommit disabled ctx new Employee Ctx conn public static void testInsert throws SQLException Employee emp new Employee 000042 emp setFirstName Arthur emp setLastName Dent emp setMale true emp setSalary new BigDecimal 200000 Wish it were so emp insert ctx Chapter 11 SQLJ revisited 205 System out println Employee emp created successfully public static void main String args try initialize testInsert catch SQLException e System err println e SQLCODE e getErrorCode catch Exception e System err printIn e Notes on Example 11 5 on page 205 1 Declare an instance of the connection context class For simplicity we decided to store that instance in a class variable so it need not be passed to each test method as a parameter as we do in the Employee class 2 This method initializes the SQLJ connection context Other than in previous examples the connection properties are not hardcoded rather they are read in from a file Note
410. rovider x Create a JDBC Provider Select the type of JDBC provider to create Name BM T4 JDBC provider Description User defined JDBC Provider Implementation class name sx A aAA aeaa aala BEL eT Class path CiSQLLIBljavaldb2jcc jar Add External JARs e CASQLLIBijavaldb2jcc license cisuz jar O csQLLIBljavatsgli zip Add Path Remove Native path Add External JARs cme Figure 15 7 Create a JDBC Provider 2 Now we are back to the window shown in Figure 15 5 on page 258 with your newly defined JDBC provider in the list Now it is time to define the data source using the newly define JDBC provider Make sure the newly created JDBC provider is highlighted which it normally is after you have created it 5 Click Add beside the currently empty data source list 6 On the next dialog select DB2 Universal JDBC Driver Provider and Version 5 0 data source Press Next 7 The next dialog prompts you to configure properties for the new data source We only need to provide values for the required fields Name and JNDI name In the Name field enter a descriptive name such as Data source T4 The most important property is the JNDI name enter jdbc empdb Figure 15 8 on page 261 This is the data source name when setting up the connection context in the Employee class Also specify com ibm websphere rsadapter DB2DataStoreHelper as the DataSource helper class name Click Next
411. row data and counting the number of rows retrieved Example 7 10 Example 7 10 Spufi executeQuery 134 DB2 for z OS and OS 390 Ready for Java Execute a SELECT statement printing the result set param stmt SELECT statement to execute return Number of rows selected throws SQLException private int executeQuery PreparedStatement stmt throws SQLException ResultSet rs null try rs stmt executeQuery 1 int columnWidths getColumnWidths rs getMetaData 2 printHeader rs getMetaData columnWidths 3 int rowcount 0 while rs next 4 printRow rs columnWidths 5 rowcount 6 return rowcount 7 finally if rs null 8 rs close Notes on Example 7 10 on page 134 Execute the query Determine preferred column widths to be used for the header and data rows Print the header row showing the column names While there are more rows print each row in turn see Example 7 12 on page 136 and count the number of rows retrieved Return the number of rows retrieved Qo mb d Or Re dct Clean up Note that here we use a try finally block to clean up before the method terminates no matter if it terminates normally returning the row count or abnormally throwing an exception Although the finally block syntactically comes after the return statement it is executed before the method actually returns to the caller A helper method getCo
412. rowable e e printStackTrace finally try if iter null iter close if ctx null ctx close catch SQLException ignored 296 DB2 for z OS and OS 390 Ready for Java Initial SQLJ employee programs Following are the two programs that we use to do the initial work for the employee class Employee sqlj Example B 3 is the employee sqlj program Note that the version that is presented here is what was used in the last chapters of the publication when deploying programs on a Web Application Server This version varies a lilttle bit from the initial one However when following the steps outlined in the publication it should be easy to determine the differences Example B 8 Employee sqlj package com ibm itso sg246435 sq1j import java sql import sqlj runtime ref import java math BigDecimal import java sql Date import java sql SQLException next one is needed to retrieve pictures import java io InputStream next ones are needed to store pictures import sqlj runtime BinaryStream import java io IOException import java io File import java io FileInputStream public class Employee sql public static context Ctx with dataSource jdbc empdb private final String empNo private String firstName private String middleInitial private String lastName private Date hireDate private Short educationLevel private boolean male private BigDecimal salary
413. rt of the DB2 for z OS V8 base code As this functionality is critical for DB2 people deploying Java applications on z OS the Universal Driver for SQLJ and JDBC will also be made available for DB2 for z OS and OS 390 Version 7 not Version 6 through the maintenance stream However we did some testing using the Type 4 JCC driver on DB2 for z OS and OS 390 As the Type 4 driver is a pure Java driver it is sufficient to copy the required jar files into your OS 390 HFS and set up the classpath properly We FTPed db2jcc jar from the directory on the workstation where DB2 for LUW had installed it SQLLIB java to the classes subdirectory of the DB2 installation directory on USS pp db2javadriver classes 5 1 3 Required DB2 for z OS changes to enable the Universal Driver 58 To enable certain database meta data catalog queries and message formatting methods when connected to DB2 UDB OS 390 and z OS the Universal Driver for SQLJ and JDBC requires certain stored procedures on the target DB2 subsystem Required service The following service is required to enable DB2 V6 and V7 for OS 390 and z OS for the JCC driver DB2 V8 when it becomes available will include this maintenance in the base code gt PQ62695 Adds stored procedures as well as tables and views used by these stored procedures that allow the JDBC and ODBC drivers to retrieve schema based catalog metadata As part of installing this maintenance you need to run the DSNTIJMS
414. run your first Java application to access DB2 data We do this for both JDBC and SQLJ applications Also we provide a reference on SQLJ syntax and usage and SQLJ tools We explain how to access DB2 from Web applications and address performance topics Copyright IBM Corp 2003 All rights reserved 99 100 DB2 for z OS and OS 390 Ready for Java Getting started with JDBC Traditionally when you learn a new programming language or environment you start with a Hello World kind of program to get familiar and we hope a feeling of success In this chapter we do just that writing a very simple JDBC program and running it from both the workstation and the z OS USS environment Copyright IBM Corp 2003 All rights reserved 101 6 1 Creating the project In this section we write and prepare the code for our simple test program The program reads the DSN8710 EMP DB2 sample table retrieving all employee records where the salary is in a given range and prints each row retrieved To develop our sample program we use WebSphere Studio Application Developer the IBM Java development platform In WSAD any code you write must reside in a project A project is the top level of organization of your resources in the WSAD workbench A project contains files and folders Projects are used for building version management sharing and organizing resources 1 Start WSAD and create a new project called SG24 6435 Select File gt New gt
415. s problem instead of using the DriverManager interface you can use the DataSource interface For a more detailed discussion see Connecting using the DataSource API on page 39 What the getConnection method does is to ask each registered JDBC driver in turn whether it accepts the supplied URL The first driver to announce that it accepts that URL will be used For example if you had loaded both the DB2 JDBC driver and the Oracle driver like this Class forName com ibm db2 jcc DB2Driver Class forName oracle jdbc driver OracleDriver A URL of the form jdbc db2 database will be passed to the DB2 driver whereas a URL of the form jdbc oracle database will be passed to the Oracle driver When a connection can be established the getConnection method returns a Connection object otherwise an exception will be thrown Using this object you can prepare SQL statements for execution control transactions and query the database for metadata information describing its tables SQL grammar and so on 6 1 3 Preparing an SQL statement for execution Once the connection has been established we send an SQL statement to the database to prepare it for execution using the prepareStatement method of the Connection object The database checks the statement s syntax and prepares it for execution by determining the optimal access path If nothing went wrong the prepareStatement method in turn returns a PreparedStatement object
416. s F3 http wtsc63 itso ibm com 9080 admin secure logon do F WebSphere Application Server Administrative Console vd Version 5 yi j Home Save Preferences Logout Help User ID bart JDBC Providers celt11 New Servers Choose a type of JDBC Provider to create i Applications E Resources Configuration JDBC Providers Generic JMS Providers General Properties WebSphere JMS Provider JDBC Providers User detined JDBC Provider 9 irine list of supported WebSphere MQ JMS Provider JDBC Provider types does not include the JDBC Mail Providers h A Provider that you wish to Resource Environment Providi use select the User URL Providers Defined JDBC Provider ou will need to consult the Resource Adapters documentation for the JDBC Security Provider for more Environment information on specific properties that may be System Administration required by that provider E Troubleshooting Apply OK Reset Cancel WebSphere Status i Previous Next October 28 2003 7 26 12 PM GMT Figure 5 31 Select user defined JDBC Provider Click Apply to create a user defined JDBC Provider A JDBC Provider screen frame with empty fields is displayed Fill in the fields in as indicated below Name DB2 JCC T2 zOS JDBC Provider Description Custom JCC T2 configuration Classpath DB2JCC DRIVER PATHyclasses db2jcc jar DB2JCC DRIVER PATHycla
417. s and launch Name 5624 6435 sqlj customize xml a Main e Refresh g Targets Mh Classpath 3 Properties 3s Common Properties 7 db password secret Add db user bartri ew Remove Property files Add R Figure 9 6 Setting the user name and password properties Now click the Run button at the bottom WSAD executes the build script and displays the messages in the Console view Example 9 3 on page 162 Chapter 9 The SQLJ program preparation process 161 162 Example 9 3 Output from running the customization script Buildfile C wsad51 workspace SG24 6435 SQLJAntScripts sqlj customize xm com ibm itso sg246435 sqlj Hello java java java java java java java java java java java customizeAl1 rA oe ji r r3 m3 oc LEJN BUILD SUCCESSFUL Total time 6 seconds i i i ibm db2 jcc sq j bm db2 jcc sq 3 bm db2 jcc sq bm db2 jcc sq ibm db2 jcc sq j ibm db2 jcc sq j ibm db2 jcc sa13 ibm db2 jcc sa13 ibm db2 jcc sa13 ibm db2 jcc sa13 ibm db2 jcc sa13 Begin Customization Loading profile com ibm itso sg246435 sqlj Hello_SJProfiled Customization complete for profile WcomVibmVitsoNsg246435NVsq1jMlello SJProfileO0 ser Begin Bind Loading profile com ibm itso sg246435 sqlj Hello_SJProfiled Binding package HELLO1 at isolation le
418. s by default one for each isolation level The serialized profile is also updated by the customization process The DB2 customization information includes the package name supplied when invoking the profile customizer command a timestamp indicating when the customization took place and a consistency token used to ensure that the database package matches the profile When an SQLJ implementation reads a serialized profile at runtime it checks for available customizations that are compatible with that particular implementation When no such customization is found the SQLJ runtime falls back to using dynamic SQL that is it reads the actual SQL text of the statements and prepares and executes these statements dynamically using JDBC calls This is in fact one of the beauties of SQLJ as opposed to other languages with embedded SQL support Whereas in COBOL for example you are required to bind your program against the database after translation otherwise you get a runtime error when executing it this is not the case with SQLJ Rather you simply translate the program and the SQLJ runtime will figure out whether or not the program has been customized This is very convenient during program development since you can omit the customization and bind step until your program is ready for deployment This is in fact what we did with the Hello sample in the previous chapter When we executed it it ran as dynamic SQL because we had not customize
419. s not affect a pending batch that is calling this method does not cause an existing batch to be cancelled or executed setBatchLimit public void setBatchLimit int limit Causes the batch to automatically execute when imit number of statements have been added to the batch where imit is a positive integer or one of ConnectionContext UNLIMITED BATCH or ConnectionContext AUTO BATCH The latter leaves the actual batch size at the discretion of the SQLJ runtime This method does not affect an existing batch executeBatch public int executeBatch throws java sql SQLException Executes the pending statement batch contained in this execution context and returns the result as an array of update counts If no pending statement batch exists for this execution context null is returned setMaxFieldSize public void setMaxFieldSize int max Specifies the maximum number of bytes that are returned for any character column in queries that use the given execution context The default is 0 which means that the maximum number of bytes is unlimited Chapter 10 SQLJ tutorial and reference 197 gt setMaxRows public void setMaxRows int max Specifies the maximum number of rows that are returned for any query that uses the given execution context The default is 0 which means that the maximum number of rows returned is unlimited gt setQueryTimeout public void setQueryTimeout int timeout Sets the maximum number of seconds that subsequent S
420. same as the iterator class name or you have to declare it as an inner class see Example 10 1 Example 10 1 Iterator declaration as an inner class public class MyClass fsql public static iterator MyPositionedIterator Chapter 10 SQLJ tutorial and reference 177 178 Named iterators A named iterator declaration looks very similar to a positioned iterator declaration except it declares not only type names but also column names sql modifiers iterator ClassName name list Where gt modifiers s an optional list of Java class declaration modifiers such as public static final and synchronized gt ClassName Is a legal Java class name gt name listls a comma separated list of a Java type name followed by a Java identifier For example fsql iterator MyNamedIterator String name int edLevel Timestamp dateHired The names in the name list must match ignoring case the names of the columns you want to retrieve in a SELECT statement using the iterator see Using named iterators on page 183 for more details Modifying iterator behavior using the implements and with clauses In the previous sections we only showed the basic form of iterator declarations There are two optional clauses in the iterator declaration statement that are used for advanced features Feel free to skip this section on first reading and refer back in the discussion of positioned UPDATE and DELETE and holdable iterators later in this cha
421. se to setthe current SQLID on any connections to this resource If this value is not set the current SQLID will default to the Primary Authorization ID on the connection databaseName This String propert true r specifies the location name of the database that should be used when establishing eo Figure 5 40 Custom Properties window Fill in the values for each of the fields as indicated below databaseName Description loginTimeout planName DB7Y which is the DB2 location name New JDBC Datasource 0 This field is initially set to DSNJDBC Change it to BARTSQLJ our SQLJ plan You do so by clicking the name of the variable We show how to specify the databaseName in Figure 5 41 on page 97 DB2 for z OS and OS 390 Ready for Java E WebSphere Administrative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Bak gt Q A Bsearch Favorites Meda lt 4 F gt 5 ig Administrative Console B LO M Home Save Preferences Logout Help User ID bart WebSphere Application Server Version 5 Message s cel111 Servers amp y Changes have been made to your local configuration Click Save to apply changes to the master configuration E The server may need to be restarted for these changes to take effect Applications E Resources JDBC Providers Generic JMS Providers
422. sparant to the application as it is done through the specification of the URL name that you use to set up the connection to your database The specification of that URL is done in the datasource definition and is outside the application The applation only uses a logical name jdbc empab to indicate the DB2 system it wants to access E Employee listing Microsoft Internet Explorer File Edit View Favorites Tools Help Back gt G A A Gsearch Favorites Meda lt 4 B 5 E vlet Address ttp iwtsc63 itso ibm com 9080 itso Employee M BluePages amp IBM Standard Software Installer Customize Links amp IBM Internal Help Free Hotmail Employee listing 000010 HAAS CHRISTINE I 000020 THOMPSON MICHAEL L 000030 KWAN SALLY A 000050 GEYER JOHN B 000060 STIEN IRVING F 000070 PULASKI EVAD 000090 HENDERSON EILEEN W 000100 SPENSER THEODORE Q 000110 LUCCHESI VINCENZO G 000120 O CONNELL SEAN 000130 QUINTANA DOLORES M Figure 15 24 EmployeeListServlet result 278 DB2 for z OS and OS 390 Ready for Java 16 JavaServer Pages As a simpler alternative to Servlets this chapter shows how to present DB2 data using JavaServer pages JSPs While the JSP language per se does not have a built in mechanism to access relational data it can easily be extended by custom tag libraries We demonstrate how to use a custom tag library for database access Copyright IBM Corp 2003 All rights reserved 279
423. sql getColumn name empdetail index 1 gt lt td gt lt tr gt lt tr gt lt td gt Name lt td gt td sql getColumn name empdetail index 2 gt lt td gt lt tr gt lt tr gt lt td gt Hired lt td gt td sql getColumn name empdetail index 3 gt lt td gt lt tr gt lt tr gt lt td gt Salary lt td gt td sql getColumn name empdetail index 4 gt lt td gt lt tr gt lt table gt lt body gt lt html gt Notes on Example 16 3 on page 285 We indicate that this is a single row query using the maxRows attribute H The JSP engine replaces the lt gt construct by the value of the enclosed expression in this case the value of the parameter empno in the HTTP request The sql parameter tag supplies a value for the parameter marker in the SQL statement Note that rather than using sql parameter we could have embedded the expression directly in the statement WHERE EMPNO lt request getParameter empno gt 286 DB2 for z OS and OS 390 Ready for Java However using a parameter marker is more efficient since it allows DB2 to cache and reuse the prepared statement To display the employee s image we still use EmployeePicServlet from Creating the EmployeePic Servlet on page 265 You can test a JSP exactly the same as the Servlets from Chapter 15 Using Servlets to access DB2 on page 249 by selecting Run on server Remember that eventually a JSP is automa
424. ss Enter to extract the definition from the existing and active WLM policy Always do this to avoid regressing the existing policy Expect this to take a few minutes The next panel is Figure 5 2 on page 63 62 DB2 for z OS and OS 390 Ready for Java File Utilities Notes Options Help Functionality LEVELOOA Definition Menu WLM Appl LEVELO13 Command gt Definition data set none Definition name Sampdef Required Description Sample WLM Service Definition Select one of the following options 9 1 Policies 2 Workloads 3 Resource Groups 4 Service Classes 5 Classification Groups 6 Classification Rules 7 Report Classes 8 Service Coefficients Options 9 Application Environments 10 Scheduling Environments P Figure 5 2 WLM Choose options from the extracted policy We select option 9 and press Enter to look at the existing application environments Figure 5 3 lists all existing WLM Application Environments S Application Environment Notes Options Help Application Environment Selection List Row 1 to 12 of 12 Command gt Action Codes 1 Create 2 Copy 3 Modify 4 Browse 5 Print 6 Delete Menu Bar Action Application Environment Name Description m BARTSRV J2EE Application Server for Bart BBOASR1 WAS IVP Server BBOASR2 J2EE Application Server CBINTFRP WAS Interface Repository Server ine CBNAMING WAS Naming Server s CBSYSMGT WAS System Management Server Z DB7YJSPP
425. sses db2jcc license cisuz jar DB2JCC DRIVER PATHyclasses sqlj zip Native Library Path DB2JCC DRIVER PATHy lib Implementation Classname com ibm db2 jcc DB2ConnectionPoolDataSource This is also shown in Figure 5 32 on page 90 and Figure 5 33 on page 90 Chapter 5 Setup 89 90 Microsoft Internet Explorer File Edit View Favorites Tools Help Bak 9 A Qusearh Favorites media lt 4 Shy ig Address http Iwtsc63 itso ibm com 9080 admin secure logon do WebSphere Application Server Administrative Console Version 5 Home Save Preferences Logout Help User defined JDBC Provider a User ID bart cel111 JDBC providers are used by the installed applications to access data from databases Servers Configuration Applications E Resources General Properties JDBC Providers APER Le EU LU eee ee eee Scope cells cel111 nodes nd111sc63 servers ws111sc63 The scope of the configured H i i senene Peces resource This value indicates the WebSphere JMS Provider configuration location for the WebSphere MQ JMS Provider configuration file Mail Providers Name pez JCC T2 zOS JDBC Provider i The name of the resource provider Resource Environment Providi URL Providers Description Custom JCC T2 configuration i 4 text description for the Resource Adapters
426. st language compiler Output from this precompilation includes source code that can be submitted to the compiler and the database request module DBRM that is input to the bind process Prepare The first phase of a two phase commit process in which all participants are requested to prepare for commit Prepared SQL statement A named object that is the executable form of an SQL statement that has been processed by the PREPARE statement Primary key A unique non null key that is part of the definition of a table A table cannot be defined as a parent unless it has a unique key or primary key Process A program executing in its own address space containing one or more threads Property An initial setting or characteristic of a bean for example a name font text or positional characteristic RDBMS See Relational database management system Read stability RS An isolation level that locks only the rows that an application retrieves within a transaction Read stability ensures that any qualifying row that is read during a transaction is not changed by other application processes until the transaction is completed and that any row changed by another application process is not read until the change is committed by that process Read stability allows more concurrency than repeatable read and less than cursor stability Reference An object s address In Java objects are passed by reference rather than by value or by pointers Re
427. standard assignment of 7 bit numeric codes to characters See also Unicode 2 An encoding scheme used to represent strings in many environments typically on PCs and workstations Contrast with EBCDIC Attachment facility An interface between DB2 UDB for OS 390 and TSO IMS CICS or batch address spaces An attachment facility allows application programs to access DB2 UDB for OS 390 Authorization ID A string that can be verified for connection to DB2 and to which a set of privileges are allowed It can represent an individual an organizational group or a function but DB2 does not determine this representation Copyright IBM Corp 2003 All rights reserved Autocommit To automatically commit the current unit of work after each SQL statement Automatic bind More correctly automatic rebind A process by which SQL statements are bound automatically without a user issuing a BIND command when an application process begins execution and the bound application plan or package it requires is not valid Base table 1 A table created by the SQL CREATE TABLE statement that is used to hold persistent data Contrast with result table and temporary table 2 A table containing a LOB column definition The actual LOB column data is not stored along with the base table The base table contains a row identifier for each row and an indicator column for each of its LOB columns Contrast with auxiliary table Bean A definition or instan
428. statements from one or more input streams such as files and displays the results author Ulrich Gehlert public class Spufi Maximum display width of a column public static final int MAXCOLWIDTH 256 Default JDBC driver name public static final String DEFAULT DRIVER com ibm db2 jcc DB2Driver JDBC connection private final Connection conn 1 Output stream DB2 for z OS and OS 390 Ready for Java private final PrintWriter out 2 Error stream private final PrintWriter err 3 Automatically commit after each statement private boolean autoCommit 4 Buffer for reading SQL statements and printing rows allocated as an instance variable to reduce garbage collection private StringBuffer buf new StringBuffer 1000 Contains Readers from which to read SQL statements f private List streams new ArrayList 5 Construct a new Spufi which logs to System out and System err param conn JDBC connection to use public Spufi Connection conn 6 this conn new PrintWriter System out new PrintWriter System err Construct a new Spufi which logs to the specified output and error streams param conn JDBC connection to use param out Output stream param err Error stream public Spufi Connection conn PrintWriter out PrintWriter err 7 this conn conn this o
429. t whenever you develop a program in Java you are actually creating a class that contains instance variables and methods Instance variables define the state of an object and methods define the behavior of that object The benefit of truly object oriented design is that classes can be developed once and then be shared and reused across the applications that need the same functionality In addition to this is the concept of class inheritance where a base class is used to define the base states and behaviors which are common across all implementations of the class Another class can be developed that extends the base class adding new behaviors or states to the base class for specific implementations of the class In addition to this inheritance allows for the base classes characteristics and states to be overridden to allow the inheriting class full flexibility in its implementation In implementing the object oriented model Java is based heavily on C However it removed some of the overly complex elements that were contained in C For example Java does not allow multiple inheritances of classes Also whereas in C a programmer has to keep track of all allocated memory taken by objects and has to explicitly free this memory DB2 for z OS and OS 390 Ready for Java when the object is no longer used Java itself manages the object creation and the actual removal when they are no longer referenced This process is called garbage collectio
430. t Source Refactor Navigate Search Project Run Window Help IS B28 4 g es e amp lis B 9 LAOSE ET Data Definition x W Judyclass java X m 7p 5624 6435 public class Judyclass i H E judy Oh public static void main String args try 1 ag load a driver y Class forName com ibm db2 jcc DB2Driver Connection con DriverManager getConnection j bartra2 bartr2 gt EA String s Stat Resu stmt rs whil Data Definition Navigator rs c stmt catch e pr New Connection ement stmt ltSet rs con createStatement i stmt executeQuery SELECT EMPNO from dsn amp 71 e irs nexti s rs getString 1 System out println i Employee number s loseil closell Throwable e intStackTrace 411 Servers Server Figure 5 14 WSAD The data perspective The DB Servers pane is located on the 4 Tasks DB Output Servers Status Server Writable ower left hand side of the perspective If you have never created a connection to a DB Server this pane is blank As we want to create a connection to allow us to communicate with our DB2 for z OS and OS 390 V7 subsystem click anywhere in the white part of the D appear Click the New Connection box B Servers pane and the words New Connection and Figure 5 15 on page 76 appears Chapter 5 Setup 75 76 Databas
431. t is known to be text such as Java source code Make sure that it does not perform that conversion for class files that are binary 6 3 2 Exporting via FTP If your installation does not offer SMB access to the HFS file system you can use FTP to export your code Fortunately you do not have to do this manually WSAD comes with built in FTP support To export the code follow these steps 1 Select the project folder SG24 6435 then select File Export 2 Select FTP from the list of export destinations and click Next 3 In the FTP host field type the TCP IP host name of your S 390 system 4 In the FTP folder field type the name of the target HFS directory in our example u rc63 SG24 6435 see Figure 6 8 on page 116 Click Next 5 Enter your user name and password Click Finish Chapter 6 Getting started with JDBC 115 Export Resources to an FTP Server Identify the FTP Server and Resources to Export Export resources to an FTP server FTP FTP host e g ftp ibm com or ftp ibm com 21 wtsc630e FTP folder e g ftp myfolder or root ftp myfolder ulrc63 S5G24 6435 Resource Selection 5G24 6435 Browse FTP Options M Use PASY Mode Passive Mode to go through the firewall Use Firewall settings Connection timeout in milliseconds 10000 ced Figure 6 8 Export to FTP Server wizard Note Unfortunately the FTP export does not seem to allow you to do ASCII to EBCDI
432. t side gt Java 2 Platform Enterprise Edition J2EE Focuses on the development and execution of server based applications through the specification of technologies such as Enterprise Java Beans EJBs Servlets JavaServer Pages and JDBC It is important to note that J2EE includes the J2SE specification Due to the nature of development of applications using DB2 for OS 390 and z OS we focus on this edition within this publication 2 2 Java 2 Platform Enterprise Edition 10 Java 2 Platform Enterprise Edition J2EE is a specification of the different technologies that are available to developers of enterprise Java applications Typically these applications are server based and may even include multiple servers of different types This server focus makes it the architecture of interest for DB2 for OS 390 and z OS developers It is important to note that the specifications that are contained in this edition are not solutions in themselves but are just definitions of how the solution will be interfaced with and how it will behave Thus a solution to be judged as compliant must fulfill the functionality specified in the architecture using the objects and methods defined within it The J2EE technologies Figure 2 1 on page 11 gives an overview of the J2EE application model We will now briefly discuss some of the technologies that are part of J2EE DB2 for z OS and OS 390 Ready for Java WebSphere
433. t the server node or cell level When variables in different scopes have the same name the order of resolution is server variables then node variables then cell variables Update Web Server Plugin Virtual Hosts Manage WebSphere Variable Shared Libraries Configuration Naming System Administration Troubleshooting Name ppgucc DRVER PATH Specifies the symbolic name i representing a physical path or URL oot Specifies the absolute path that the symbolic name represents Value pp db2javadriver Description Driver path for the new JCC T2 z OS driver i Provides an optional description for your administrative records Figure 5 27 Save your changes Save into the master configuration as well Click Save again as indicated in Figure 5 28 File Edit View Favorites Tools Help Bak gt A Bsearch Favorites media 4 gt 5 ig Address ja http wtsc63 itso ibm com 9080 admin secure logon do WebSphere Application Server Administrative Console hie V ms Save Preferences Logout User ID bart Message s cel111 Servers amp changes have been made to your local configuration Click Save to apply changes to the master configuration E The server may need to be restarted for these changes to take effect Applications Resources WebSphere Variables gt DB2JCC D
434. tDB2ClientAccountingInformation String s throws SQLException Sets accounting information Example 12 3 demonstrates the use of the extended client information API Example 12 3 Using the extended client information API public class ClientInfoTest public static void main String args String url jdbc db2 wtsc63 itso ibm com 33756 DB7Y try Class forName com ibm db2 jcc DB2Driver Connection conn DriverManager getConnection url bartrl secret if conn instanceof DB2Connection DB2Connection db2conn DB2Connection conn db2conn setDB2ClientUser Ulrich Gehlert db2conn setDB2ClientWorkstation java net InetAdress getLocalhost getHostName db2conn setDB2ClientApplicationInformation ClientInfoTest Dummy call to force extended client information to be sent conn prepareStatement SELECT FROM SYSIBM SYSDUMMY 1 WHERE 0 1 executeQuery Chapter 12 The DB2 Universal Driver 227 Sleep for a while 30 secs so we can display DB2 thread information Thread sleep 30000 catch Throwable e e printStackTrace Without setting client information the DB2 DISPLAY THREAD command only shows the authorization ID and location name for the thread associated with the JDBC connection see Figure 12 1 DISPLAY THREAD SERVER DETAIL DSNV401I DB7Y DISPLAY THREAD REPORT FOLLOWS DSNV402I DB7Y ACTIVE THREADS NAME STA REQ ID AUTHID PLAN ASID TOKEN SERVER RA 0 d
435. tOnCtx2 public String toString return Thread 3 ctx2 start catch Exception e e printStackTrace private static void message String message System out println Thread currentThread message private static void updateOnCtx1 try BigDecimal salary null message SELECT 7 sql ctx1 SELECT SALARY INTO salary FROM DSN8710 EMP WHERE EMPNO empno h message Salary salary Chapter 10 SQLJ tutorial and reference 191 192 message Updating row sql ctx1 UPDATE DSN8710 EMP SET SALARY SALARY 300 WHERE EMPNO empno h synchronized semaphore updated true semaphore notifyAll message Sleeping for 10 seconds Thread sleep 10000 message Awake message Rollback sql ctx1 ROLLBACK catch Exception e message e toString private static void selectOnCtx1 try BigDecimal salary null synchronized semaphore while updated semaphore wait message SELECT sql ctx1 SELECT SALARY INTO salary FROM DSN8710 EMP WHERE EMPNO empno Ks message Salary salary catch Exception e System out println Thread currentThread e private static void selectOnCtx2 try BigDecimal salary null synchronized semaphore while updated semaphore wait message SELECT sql ctx2 SELECT SALARY INTO salary FROM DSN8710 EMP WHER
436. teQuery SELECT EMPNO from dsn amp 71 while rs next s re getString 1 El J DB Gdbe db2 J wtsc63 itso ibm co H B BARTRI je 9 BBO System out println Employee number s H B BBOLDAP H B CBASRUZ rs closetl B S9 psNACC stmt close x S DSNRGCOL catch Throwable e 32 DSN8710 e printStackTrace H 6 Tables C3 views Ali ed 41 Servers C3 Triggers Server Status Server G Structured Types C3 Stored Procedures HAMO 4 Tasks DB Output Servers writable Figure 5 20 The databases from DB7Y You see the actual URL that we just entered when creating the connection Expand the URL by clicking its sign and the schemas are shown Important WSAD utilizes the database metadata catalog queries and message formatting methods when connected to DB2 UDB V7 for OS 390 and z OS that we described in Required DB2 for z OS changes to enable the Universal Driver on page 58 If you do not retrieve any schemas there will be no error message when you expand the URL it is an indication that the PTF named in Required DB2 for z OS changes to enable the Universal Driver has either not been applied or that the customization job described in that section has not been run As we continue to expand the DSN8710 schema we see the tables views and other objects displayed The DB2 for OS 390 and z OS V7 sample database is created by running the DB2 i
437. ter configuration Environment System Administration Save to Master Configuration Troubleshooting Click the Save button to update the master repository with your changes Click the Discard button to discard your changes and begin work again using the master repository configuration Click the Cancel button to continue working with your changes Total changed documents 23 View items with changes Uk Discard Cancel Figure 15 21 Save your changes to the master repository After saving your changes the admin console application takes you back to the main menu Figure 15 22 We are now ready to activate the application trative Console Microsoft Internet Explorer File Edit View Favorites Tools Help Bak gt Q A Qusearch Favorites Gmeda lt 4 D 4 m4 5 Address r3 http Jwtsc63 itso ibm com 9080 admin secure logon do WebSphere Application Server Administrative Console 29 Version 5 Home Save Preferences Logout Help User ID ceit11 WebSphere Application About your Webs Servers Server on IBM com Application S E Applications IBM WebSphere Application Server for Enterprise Applications z OS 5 0 0 immi Install New Applid tion Build Number V501000 Resources nd other Build Date 915 03 seful information pna RE MP did Security Licensed Material Property of IBM X Environment System Administration Troubleshooting 9
438. terators and the assignment statement Connection declarations Execution contexts Interoperability between JDBC and SQLJ vvvvvvvy Copyright IBM Corp 2003 All rights reserved 175 10 1 The basic syntax of SQLJ statements There are four different forms of SQLJ statements Executable statements Iterator declarations Assignment statements Connection contexts vvvy We discuss each of these in the following sections 10 1 1 Executable statements This is the simplest SQLJ construct You use an executable statement to perform database operations such as gt Inserting modifying and deleting data Data Manipulation Language DML statements Creating database objects Data Definition Language DDL statements Control transactions COMMIT and ROLLBACK statements Calling stored procedures vvv An execute statement can appear anywhere a Java statement can appear It begins with the token sql as does any other SQLJ statement followed by an optional connection context specification and the SQL statement text enclosed in curly braces and is terminated by a semicolon sql connCtx SQL statement When you omit the connCtx clause sql SQL statement The statement is executed under the current default connection context See Connection contexts on page 188 for why this is not good practice SQL statement can be almost any DB2 statement that can be statically prepared Refer to Application Progra
439. terminates when the end of the stream has been reached Executing a single SQL statement The execute String sql method takes one SQL statement and prepares and executes it It has to determine whether the statement is a query or not in the case of a query it prints the result set and the number of rows found otherwise it prints the number of rows affected by the statement for example the number of rows deleted if it was a DELETE statement Example 7 8 Spufi execute Execute a single SQL statement The statement and results are logged to the output streams as are any exceptions resulting from execution param sql SQL statement to execute private void execute String sql sql sql trim 1 if sql length 0 returns PreparedStatement stmt null try stmt conn prepareStatement sql 2 int numRows if isSelectStatement sql 3 numRows executeQuery stmt 4 log numRows row s selected else numRows executeUpdate stmt 5 Chapter 7 JDBC revisited 133 log numRows row s affected if isAutoCommit 6 conn commit catch SQLException e logError e 7 finally close stmt 8 Notes on Example 7 8 on page 133 1 Remove leading and trailing blanks return to the caller if the statement was empty 2 Prepare the statement If the statement had a syntax or authorization error or if any other database problem was encountered the catch block will be exe
440. than between parameter markers and the setBigDecimal methods in JDBC Also JDBC uses statement handles that must be explicitly closed when you are done with the statement In SQLJ the translator automatically generates the cleanup code for you Iterators must still be closed explicitly of course DB2 for z OS and OS 390 Ready for Java Example 3 3 JDBC vs SQLJ Multi row query PreparedStatement stmt conn prepareStatement SELECT LASTNAME d FIRSTNME 5 gt SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN AND t tt stmt setBigDecimal 1 min stmt setBigDecimal 2 max ResultSet rs stmt executeQuery while rs next lastname rs getString 1 firstname rs getString 2 salary rs getBigDecimal 3 Print row rs close stmt close EmployeeIterator iter sql ctx iter SELECT LASTNAME FIRSTNME SALARY FROM DSN8710 EMP WHERE SALARY BETWEEN min AND max h while true sql FETCH iter INTO lastname firstname salary h if iter endFetch break Print row iter close In Example 3 4 we compare how to code a single row query that is a query returning exactly one row of data In JDBC we have to open a result set advance it to the next and only row and retrieve the values using getXxx methods Also we have to check if exactly one row has been found In SQLJ on the other hand we can use the SELECT INTO syntax an SQLE
441. that DB2 Connect Server provides functionality that is not provided by the Type 4 driver These functions include sysplex awareness and connection concentration These functions can be very valuable in large installations For a more detailed description of these features please see Distributed Functions of DB2 for z OS and OS 390 SG24 6952 Note DB2 Connect Server is a component of multiple DB2 Connect products namely DB2 Connect Enterprise Edition EE DB2 Connect Application Server Edition ASE gt DB2 Connect Unlimited Edition CUE 3 2 4 Type 2 connectivity from a non z OS platform 32 As a Type 2 driver is normally a local driver and in this configuration the DB2 for z OS and OS 390 system is not local to the JDBC driver you need something in the middle This thing in the middle is of course DB2 Connect The configurations in Figure 3 6 on page 33 are often used when the WAS system is running on a different machine and or platform than the DBMS DB2 This is often done for security reasons People want to make it as hard as possible for hackers to get to the corporate data Therefore in many installations it is not allowed to have the data on the same physical machine as the Web server DB2 for z OS and OS 390 Ready for Java z OS image z OS image Figure 3 6 Type 2 connectivity from a non z OS platform But as you can see in the figure above this is not a problem You run your Java applications
442. that make up the Java technology and how Java can be used to access a DB2 for z Os and OS 390 system Copyright IBM Corp 2003 All rights reserved 1 2 DB2 for z OS and OS 390 Ready for Java A brief history of Java In its brief lifetime Java has become a significant force in computer application development moving quickly from its initial function as a controller of small consumer devices to a language that runs on mainframes and is a cornerstone of Internet application development With this growth Java has expanded from being just a computer programming language to a complete specification of technologies to provide complete enterprise solutions This chapter describes gt The development of Java as a programming language and its evolution into a platform for providing enterprise solutions culminating in the J2EE specification gt The creation of the JDBC and SQLJ specification for accessing relational databases with an emphasis on DB2 for OS 390 and z OS gt The implementation of Java on IBM s OS 390 and z OS platforms and the effect this has had on the architecture of the mainframe gt A few of the products developed to host and manage applications on this platform specifically with their support of DB2 in mind Copyright IBM Corp 2003 All rights reserved 3 1 1 From toaster to the enterprise I dunno the Internet Homer J Simpson 1 1 1 Java and the consumer electronics revolution 4 Java
443. that you can get a nicely formatted error message using the SQLException getMessage method similar to what you have today in DB2 for z OS when you call DSNTIAR to format you SQL error information It does require that you have the retrieveMessagesFromServerOnGetMessage property set on your connection An example is shown in Example 6 1 on page 103 14 5 Tracing A very useful feature of the DB2 Universal Driver for SQLJ and JDBC is its tracing support which can be turned on either from your program or even externally by setting up properties on a DataSource definition or in the JDBC connection URL This way you can turn on tracing even when running an application for which you do not have the source code provided that the connection URL is specified externaly to the program 14 5 1 Turning on tracing in the program To turn on tracing programatically use the setJccLogWriter method of class DB2Connection The first argument to this method is a PrintWriter to which the output is sent The optional second argument specifies the trace level Table 14 1 lists the available trace levels The constants representing these levels are declared in class com ibm db2 jcc DB2BaseDataSource You can combine individual levels using bitwise OR If you use the one argument setJccLogWriter method TRACE ALL is assumed Table 14 1 Trace level options for the DB2 Universal Driver for SQLJ and JDBC TRACE NONE 0x0000 TRACE_CONNECTION_CALLS 0x0001 TR
444. the EmployeeDetail JSP 00 002 ee 285 16 4 Deploying to WebSphere Application Server 0 00 eee 287 Part 5 Appendixes 2 2505 ocak ied eed eral Aha eva bes Red vtta dure ara 289 Appendix A SQLSTATE categories isliisiil elles eese 291 Appendix B Source code of sample programs 0000 0 ee eae 293 Hlello J8V8 urs S rh e e oet t AR e IR Rt CR HT UNUS ac p ctim ti e dade 294 Hello sqlj ue Ebr Rode Du ae ia pedir o peace Salidas 295 Initial SQLJ employee programs ssssseeeee e m 297 Employee glj aU EF EE eae EN EE d eR RU e chis aids 297 Employee lest avaiy o s sabi tegen Rye whe bck aided ahaa eee eRe a paca 303 Servlets ood sea etes us Ced reds eed PA ee eine a dice cohen o eiua iuis 305 EmployeeListServlet lll lsllseeeeleeeee teen eens 305 EmployeeDetailServiet annuun 0 0 cece eee 307 EmployeePicServlet 0 000 cc ene 308 JavaServer Pages lisi hmm e rer 310 Employeebist JPS uie e xm eie aa o kr totg Se e lO aa x 310 EmployeeDetail JSP 1 0 0 nes 311 Appendix C Additional material 0 000 cee 313 Locating the Web material llle re 313 Using the Web material llli RI III n 313 How to use the Web material lille 313 Abbreviations and acronyms sslssseee ern 315 Glossary uis en xc pex e Been ere Me e RR STD RE e hades 317 Related publications 0 0 02 cece eens 327 IBM RedbOOK
445. the bytecode instruction by instruction translating it into machine code If a piece of bytecode was executed a number of times the translation would occur every time With the introduction of the JIT compiler the JVM monitors how often a certain piece of code is executed when that count exceeds a threshold that code is translated into efficient machine code which from now on replaces the bytecode execution of the code On a single user platform this machine code is discarded when the application stops and the JVM terminates On server platforms however the JVM can be reused so when common classes are reused often and compiled the resulting machine code can be reused by multiple users across different applications and performance increases significantly The efficiency of the JVM was further improved by optimizing the bytecode and by improving memory management and garbage collection processing With these improvements Java on the single user platforms is getting close to the performance of platform specific programs 2 4 Java application environments Java has evolved from a simple single device programming language to a fully blown enterprise development system This evolution has lead to a number of distinct programming solutions from a simple stand alone program through a server based Web page generation component and ending up with a business process object that encapsulates and manages business logic across the enterprise
446. the view s title bar Double clicking the title bar again restores the previous layout 6 2 3 Troubleshooting 112 If the program did not run correctly check the stack trace that appears in the Console window The most common errors are described in this section Invalid database URL syntax Make sure the JDBC URL has the following format jdbc db2 your server name portnumber locn Where gt your server name is your server s hos tname or IP address portnumber is the TCP port number on which your DB2 subsystem receives incoming SQL requests locnis your DB2 location name not the DB2 subsystem name in our case both are the same but that is usually not the case Tip If you do not know the port number either ask you system administrator issue the DIS DDF command or look for message DSNL519I in your DB2 system s master address space job log IO Exception opening socket to server The driver could not establish a TCP IP connection to the target database server due to one of the following reasons The port number in the JDBC URL is incorrect gt The database server is not running gt DDF is not active on the database server or is not set up for TCP IP Connection authorization failure occurred The user name or password is incorrect An attempt was made to access a database which was not found Make sure you specified the DB2 location name in uppercase DB2 for z OS and OS 390 Ready for Java FI
447. tically compiled into a Servlet by the Web application server 16 4 Deploying to WebSphere Application Server This is similar to deploying a Servlet but you need to use a different URL from the Web browser Chapter 16 JavaServer Pages 287 288 DB2 for z OS and OS 390 Ready for Java Appendixes Part 5 290 DB2 for z OS and OS 390 Ready for Java SQLSTATE categories Table 16 1 below provides an overview of selected SQLSTATE categories to help you categorize the cause of an SQLExcept ion For full details and the individual SQLSTATE values refer to DB2 Universal Database for OS 390 and z OS Messages and Codes GC26 9940 Table 16 1 SQLSTATE categories owe Meaning Emme context w emen w wmeewwnhepm mw mwemaRped mmm 70 7 Dmssmeemeesa C Der Testen massam Copyright IBM Corp 2003 All rights reserved 291 Constraint Violation INSERT or UPDATE a NULL value into a NOT NULL column Invalid foreign key value emey weaning Emme ENNEELLICLIONEN NN NN o 5 CNN UN E Violation No priviledge to perform operation E NM Invalid Operand or Inconsistent tua i Miscellaneous SQL or Product Error Resource Not Available or me Intervention ECL NEN NN 292 DB2 for z OS and OS 390 Ready for Java Source code of sample programs This appendix provides the source code for the examples presented in
448. tication and Authorization Service JAAS gt IBM Java Cryptography Extension JCE The authentication technique can be specified by either setting a Java property in the application or by recording the required technique in the DB2 DataSource definition 12 3 4 Improved Java SQL error information The standard JDBC SQLException class does not provide a way to retrieve vendor specific error information other than the getErrorCode method which for DB2 returns the SQLCODE reported by the DB2 server For example if an INSERT into a table fails because you try to insert a NULL value into a NOT NULL column there is no standard way to find out which column DB2 is complaining about other than to parse the error message Again the DB2 Universal Driver for SQLJ and JDBC provides a proprietary API to retrieve detailed error information in DB2 terms to access the SQLCA This feature is discussed in more detail in DB2 specific error handling on page 242 12 3 5 Java API for Set Client Information SQLESETI 226 As a DB2 specific extension the JCC driver provides an interface to supply extended client information to the DB2 server This is especially useful in a client server or application server environment Using this interface the client program or the application server can supply information that identifies the specific user issuing a request Otherwise only the application servers information is passed Unlike the user infor
449. tion S Connection Properties KA WebSphere v4 Applicatic Host wtsc63 itso ibm com E WebSphere v5 Applicatic AI websp m Port 6000 Allow termination of remote VM Apply Revert Close Figure 6 12 Launch configuration for remote debugging 4 Click Debug WSAD switches to the Debug Perspective and attaches to the remote JVM Now you can debug the remote program exactly as if it was running inside WSAD on your local machine The only thing you have to be careful about is to keep the byte code and the Source code in sync that is to make sure that you re export the program to the remote machine after changing the source code DB2 for z OS and OS 390 Ready for Java JDBC revisited This chapter describes the following INSERT UPDATE and DELETE statements NULL handling Database metadata Positioned UPDATE and DELETE Large objects LOBs vvvvy It also demonstrates many of the APIs and techniques with a complete application a Java version of the well known SQL Processing Using File Input SPUFI tool Copyright IBM Corp 2003 All rights reserved 121 7 1 INSERT UPDATE and DELETE statements In Chapter 6 Getting started with JDBC on page 101 we only covered queries that are executed by the PreparedStatement executeQuery method returning a ResultSet To execute an SQL statement that is not a query you use the executeUpdate method of PreparedStatement Note that the method name may be slig
450. tions Help F Policy Selection List Row 1 to 1 of 1 C Command gt D The following is the current Service Definition installed on the WLM couple data set D D Name Sampdef S f Installed on 2002 10 16 at 20 33 49 Select the policy to be activated with Sel Name Description WLMPOL Sample WLM Service Policy kkkkkkkkkkkkkkkkkkkkkkkkkk Bottom of data kkkkkkkkkkkkkkkkkkkkkkkkkkk Installed by BARTR2 from system SC63 Figure 5 9 Activate the modified WLM policy Select WLMPOL by specifying a slash beside the service policy and press Enter When the screen is redisplayed you should see the words Service policy WLMPOL was activated IWMAM060 atthe bottom Verify that the application environment DB7YJCC is available by issuing the following z OS console command D_WLM APPLENV If you specified a new procedure make sure to add it to a member of a data set in the PROCLIB concatenation of your system See Example 5 4 below Example 5 4 Started task JCL for DB7YWLM in SYS1 PROCLIB DB7YWLM PROC RGN OK APPLENV XXXXXX DB2SSN DB7Y NUMTCB 8 l1EFPROC EXEC PGM DSNX9WLM REGION 8RGN TIME NOLIMIT PARM amp DB2SSN amp NUMTCB amp APPLENV STEPLIB DD DISP SHR DSN DB7YU RUNLIB LOAD DD DISP SHR DSN CEE SCEERUN DD DISP SHR DSN DB7Y7 SDSNLOAD DSSPRINT DD SYSOUT SYSIN DD UNIT
451. tions and click Next 3 In the Directory field type the drive letter to which you mapped the HFS file system followed by a colon and a backslash Select Create directory structure for files see Figure 6 7 on page 115 4 Click Finish 5 To verify that the files have been exported correctly use Windows Explorer to view the directory structure on the HFS file system Note The Create directory structure for files option tells WSAD to create the same directory structure on the export directory as it exists in the workspace That is in our example the wizard will create a directory called SG24 6435 and create the directories corresponding to the Java package names below that directory When you work with several projects at a time this makes it easier to tell which file on the destination file system belongs to which project DB2 for z OS and OS 390 Ready for Java File system Export resources to the local file system c x Select the resources to export amp v G 5624 6435 E classpath 3 project Select Types Select All Deselect All Select the export destination Directory uit v Browse Options Overwrite existing files without warning lt Back Next Cancel Figure 6 7 Export to file system wizard Important Since the HFS file system on Unix System Services uses EBCDIC the SMB server software usually performs an ASCII to EBCDIC conversion for files whose conten
452. to DB2 data types boolean SMALLINT DB2 has no exact equivalent but Boolean SMALLINT is the best match A zero value denotes false any non zero value denotes true byte SMALLINT DB2 has no exact equivalent but Byte SMALLINT is the best match short SMALLINT Short int INTEGER Integer long DECIMAL 19 0 DB2 has no 64 bit integer type However Long a DECIMAL column with precision 19 can hold all long values float REAL Float double DOUBLE or FLOAT Somewhat confusingly FLOAT is a synonym for DOUBLE in DB2 java math BigDecimal DECIMAL p s p precision s scale For example a DECIMAL 5 2 column has a range of 000 00 to 999 99 java lang String CHAR n Declares a fixed width column of length n where 1 n 255 The value will be padded with trailing blanks if necessary VARCHAR n Declares a variable width column of maximum length n No padding occurs Certain restrictions apply if n gt 255 see DB2 Universal Database for OS 390 and Z OS SQL Reference SC26 9944 GRAPHIC n Declares a fixed width DBCS column of length n where 1 n 255 The value will be padded with trailing blanks if necessary VARGRAPHIC n Declares a variable width DBCS column of length n where 1 n 255 The value will be padded with trailing blanks if necessary EN 2 NN Deme me 00 LLL meme me LLL ees meme Chapter 10 SQLJ tutorial and reference 181 10 5 Queries iterators and the assignment statement
453. to calls to the SQLJ runtime library They appear as comments in the translated Java file The output of the SQLJ translator is platform independent that is you do not need to re translate your SQLJ source files when targeting a database from a different vendor This is true for both the generated Java source file and the profiles 9 2 2 More about profiles The generated profiles contain information about all the SQL statements in the program A profile consists of one or more profile entries each of which describes one SQL statement Each profile entry in turn contains the type of statement the number and type of parameters and results the actual SQL text and zero or more profile customizations explained below In this respect a serialized profile is much like a DBRM A profile is actually an instance of the SQLJ runtime class sq1j runtime profile Profile and the ser file produced by the SQLJ translator is the serialized form of that object produced by the standard Java serialization mechanism Therefore you can reconstitute the profile object by deserializing it import sqlj runtime profile Profile ObjectInputStream ois new ObjectInputStream new FileInputStream serfile Profile p ois readObject This is exactly what the SQLJ runtime library does when the program is executed Tip A convenient way to try this from within WSAD is to use a scrapbook page as follows 1 Open a scrapbook page select File New Sc
454. to insert a null value into a NOT NULL column and you want to figure out which column it was that DB2 complained about The standard SQLException class does not provide an interface to retrieve vendor specific information other than getErrorCode which for DB2 returns the SQLCODE However the DB2 JCC driver provides a proprietary method to examine the SQLCA which is a DB2 data structure providing information about the execution of SQL statements To access the SQLCA you cast the SQLException object to com ibm db2 jcc DB2Diagnosable and call the getSqlca method to retrieve an instance of class com ibm db2 jcc DB2Sql1ca This class in turn provides methods to access the individual fields of the SQLCA The meaning of each field depends on the specific kind of error The most interesting part of the SQLCA is a string called SQLERRM which contains several error tokens separated by the character OxFF You do not need to tokenize this string yourself the DB2Sq1ca getSqlErrmcTokens method does this for you To find out what the individual error tokens mean for a given SQLCODE refer to DB2 Universal Database for OS 390 and z OS Messages and Codes GC26 9940 Look up the error text in the description of the SQLCODE The tokens appear sequentially in the SQLERRM in the order they appear in the message text For example this is the error text for SQLCODE 805 805 DBRM OR PACKAGE NAME location name collection id dbrm name consistency
455. toring auditing performance accounting statistics and serviceability global data Transmission Control Protocol based on IP 1 A network communication protocol used by computer systems to exchange information across telecommunication links 2 An Internet protocol that provides for the reliable delivery of streams of data from one host to another Type n WSAD a generic term for a class or interface UDF User defined function UDT User defined data type Uncommitted read UR An isolation level that allows an application to access uncommitted changes of other transactions The application does not lock other applications out of the row it is reading unless the other application attempts to drop or alter the table Unicode A 16 bit international character set defined by ISO 10646 See also ASCII Uniform Resource Locator URL Theunique address that tells a browser how to find a specific Web page or file URL See Uniform Resource Locator User defined data Type UDT See distinct type User defined function UDF A function defined to DB2 using the CREATE FUNCTION statement that can be referenced thereafter in SQL statements A user defined function can be either an external function or a sourced function Contrast with built in function Variable 1 An identifier that represents a data item whose value can be changed while the program is running The values of a variable are restricted to a certain data type 2 A dat
456. tring java lang String sql type VARCHAR name getCol2 marker index 1 3 mode OUT java type java math BigDecimal java math BigDecimal sql type NUMERIC name getCol3 marker index 1 EntryInfo custom data 3 DB2 Statement Type 231 Needs runtime describe false Section com ibm db2 jcc sqlj StaticSection 13b08d92 Section number 1 DB2 parameterMetaData com ibm db2 jcc SQLJColumnMetaDataQb124d92 Parameter 1 4 name nul1 label null nullable true sqlType 485 precision 9 scale 2 ccsid 0 columnLength 0 tableName Parameter 2 name nul 1 label null nullable true sql Type 485 precision 9 scale 2 ccsid 0 columnLength 0 tableName Query Related Information 5 cursorName DB2JCCCURSORI holdability false cursorType 1003 resultSetConcurrency 1007 dynamicUpdate section null DB2 resultSetMetaData com ibm db2 jcc SQLJColumnMetaData 110c8d92 Parameter 1 6 name LASTNAME label null nullable false sqlType 448 precision 0 170 DB2 for z OS and OS 390 Ready for Java scale 0 ccsid 37 columnLength 15 tableName lt not described gt Parameter 2 name FIRSTNME label null nullable false sql Type 448 precision 0 scale 0 ccsid 37 columnLength 12 tableName not described Parameter 3 name SALARY label null nullable true sql Type 485 precision 9 scale 2 ccsid 0 columnLength 0 tableName lt not described gt Positioned Update Delete specific Infor
457. tructure issues such as instance swapping resource pooling and activation The first EJB specification 1 1 appeared in J2EE 1 2 while J2EE 1 3 extended this specification with the introduction of EJB 2 0 introducing performance enhancements and the message driven EJB The most recent release EJB 2 1 was released with J2EE 1 4 which is the most current specification at the time this publication was written Why use EJBs One reason for the development of EJBs was to separate middleware oriented code responsible for persistence security transaction control and so on from the business logic Beans encapsulate the business logic mainly within the session bean with data access controlled using the entity beans Issues such as security communication and actual bean location are managed by the container in which the beans are run and once set are no concern of the client programs that are ultimately going to call these EJBs This also increases the ability to scale the applications as beans can be spread across servers Chapter 2 An introduction to Java 2 Enterprise Edition 19 20 The three flavors of EJBs There are three distinct types of EJBs Session beans There are two types of session beans Stateless session beans SLSBs Stateful session beans SFSBs gt Entity beans There are two types of these beans Bean managed persistence BMP Container managed persistence CMP gt Message driven beans
458. try System out println Connecting to DB2 for 0S 390 database con DriverManager getConnection jdbc db2 wtsc63 itso ibm com 33756 DB7Y sflynn girlieO1 System out println Connection made stmt con createStatement System out println Statement created rs stmt executeQuery SELECT STRIP CREATOR AS CREATOR NAME RECLENGTH FROM SYSIBM SYSTABLES WHERE DBNAME DSNDBO6 AND TYPE T ORDER BY 2 1 System out println Statement executed Print all warnings for SQLWarning warn stmt getWarnings warn null warn warn getNextWarning System out println Statement warning code warn getErrorCode rs close stmt close con close catch SQLException e System out println SQLCODE e getErrorCode System out println SQLMESSAGE e getMessage Ww DB2 for z OS and OS 390 Ready for Java e printStackTrace catch Exception e e printStackTrace 14 2 SQLCODE and SQLSTATE The SQLException class has two methods to retrieve an error code associated with an exception gt The getErrorCode method which returns a vendor specific error code as an integer In DB2 this vendor specific error code is the SQLCODE gt The getSQLState method which returns a five character string the SQLSTATE If you are interested in maximum portability we recommend that you use the getS
459. ts across the network As a transport protocol RMI can use the Java Remote Method Protocol JRMP or the Internet Inter Orb Protocol IIOP which also supports CORBA distributed method calls gt Java Authorization and Authentication Service JAAS provides for enterprise wide security Java Transaction API JTA is the interface for local and distributed transactions gt Java Messaging Services JMS provides an interface to Message Oriented Middleware servers such as IBM WebSphere MQ for providing asynchronous messaging JavaMail and JavaBeans Activation Framework JAF is an API through which Java applications can send e mail gt JavalDL is an API to allow J2EE components to use external CORBA objects using the IIOP protocol J2EE Connector Architecture J2EECA is an architecture for connecting transactional J2EE to existing enterprise systems 2 3 Java 2 features The foundation for all Java technologies is the Java 2 specification This specification defines the language elements of Java 2 along with how the language is processed and executed It covers everything from language syntax through to how the JVM is to operate The following topics cover the significant features of this specification 2 3 1 Object oriented programming 12 Java was built from the ground up as a fully object oriented language This means that it is based around the concept of classes which when implemented define objects In fac
460. ture method to the EmployeeTest class Example 11 25 The only difficult thing is to find a picture of Arthur Dent Example 11 25 EmployeeTest testCreatePicture Employee emp Employee findByPrimaryKey ctx 000042 emp createPicture ctx C temp arthur dent bmp System out printIn Successfully stored picture of emp 218 DB2 for z OS and OS 390 Ready for Java Important Up to this point we have not customized the profile and run it as static SQL against the database To get the most out of SQLJ it is very important to do this When using WSAD you simply have to generate and run your SQLJ Ant script again as described in Preparing SQLJ programs to use static SQL through WSAD on page 157 Chapter 11 SQLJ revisited 219 220 DB2 for z OS and OS 390 Ready for Java 12 The DB2 Universal Driver This chapter discusses some of the enhancements of the DB2 Universal Driver for SQLJ and JDBC which is a significant reengineering of Java support for DB2 and will eventually replace the existing platform specific drivers In particular we discuss the following Whatthe DB2 Universal Driver for SQLJ and JDBC is Setting connection properties in the URL gt Functionality enhancements Copyright IBM Corp 2003 All rights reserved 221 12 1 What the DB2 Universal Driver for SQLJ and JDBC is The DB2 Universal Driver for SQLJ and JDBC is a significant reengineering of Java support for DB2 The DB2 Universal Dr
461. ture of single and double byte characters A LOB can be up to 2GB 1 byte in length See also BLOB CLOB and DBCLOB Method A fragment of Java code within a class that can be invoked and passed a set of parameters to perform a specific task Middle tier The hardware and software that resides between the client and the enterprise server resources and data The software includes a Web server that receives requests from the client and invokes Java Servlets to process these requests The client communicates with the Web server via industry standard protocols such as HTTP and IIOP Middleware A layer of software that sits between a database client and a database server making it easier for clients to connect to heterogeneous databases Multithreading Multiple TCBs executing one copy of DB2 ODBC code concurrently sharing a processor or in parallel on separate central processors MVS ESA M Multiple Virtual Storage Enterprise Systems Architecture Native code Machine dependent C code that can be invoked from Java For multi platform work the native routines for each platform need to be implemented Null A special value that indicates the absence of information Object The principal building block of object oriented programs Objects are software programming modules Each object is a programming unit consisting of related data and methods ODBC See Open Database Connectivity ODBC driver A dynamically linked library DLL that
462. ty Java DataBase Connectivity Java Generic Security Service Java Naming and Directory Interface JavaServer Page Java Virtual Machine Kilobyte 1 024 bytes Logical Agent Language Environment Large object Logical partition Logical page list Logical record length Log record sequence number Logical unit of work Megabyte 1 048 576 bytes Non partitioning index Open Data Base Connectivity Operating System 390 Parallel access volume Partitioned data set Parallel index build Pageset identifier Preventive service planning DB2 for z OS and OS 390 Ready for Java PTF PUNC QA QMF RACF RBA RDBMS RECFM RID RR RRS RRSAF RS RUW SAA SDK SPUFI SQL SRB SU TCP IP UOW URL USS WAS WSAD XML Program temporary fix Possibly uncommitted Quality Assurance Query Management Facility Resource Access Control Facility Relative byte address Relational Database Management System Record format Record identifier Repeatable read Resource recovery services Resource recovery services attach facility Read stability Remote Unit of Work System Application Architecture Software development kit SQL Processing Using File Input Structured Query Language Service Request Block Service Unit Transmission Control Protocol Internet Protocol Unit Of Work Uniform Resource Locator UNIX System Service WebSphere Application Server WebSphere Studio Application Developer Extensible Markup L
463. ty to access the sample tables If this is not the case please ask your DBA to create a copy of the sample tables and grant you the required authority For your convenience Table 11 1 summarizes the columns in the EMP sample table However for the sake of brevity we will not read or write all of the columns but only the required non null columns and a few other interesting ones Table 11 1 Employee sample table counnnene sorsmsugs sus memue weownns e 11 2 Creating the Employee class 200 In this section we develop a Java class that represents the data in the EMP sample table The class will have attributes that map to the individual columns in the table and methods to insert delete and modify employee data To create the SQLJ source file in WSAD right click the package then select New Other Data gt SQLJ Sgqlj File Click Next and enter the file name Employee and make sure it is created in the correct package com ibm itso sg246435 sqlj This is similar to what we did in Creating the source file on page 142 for our initial SQLJ Hello program In the editor window we create the skeleton of our Employee class Example 11 1 on page 201 We add the import statement for java lang BigDecimal and the instance variables also called fields or attributes that will hold the individual values from the table To keep the example reasonably small we only create fields for a few of the columns D
464. u should specify this clause since this normally leads to a more efficient program Let us look at Example 10 8 below Example 10 8 Positioned UPDATE package com ibm itso sg246435 import java math BigDecimal import java sql SQLException public class UpdateBonus sql public static iterator EmployeeIterator 1 implements sqlj runtime ForUpdate with holdability true updateColumns BONUS 2 String s public static void updateBonus String dept BigDecimal bonus throws SQLException EmployeeIterator empiter sql ctx empiter SELECT LASTNAME FIRSTNME MIDINIT FROM DSN8710 EMP WHERE WORKDEPT dept h try String name null int numrows 0 3 while true sql FETCH empiter INTO name if empiter endFetch break sql ctx UPDATE DSN8710 EMP 4 SET BONUS bonus WHERE CURRENT OF empiter h System out print n name if numrows amp 100 0 5 sql ctx COMMIT sql ctx COMMIT 6 finally empiter close 7 Chapter 10 SQLJ tutorial and reference 187 Notes on Example 10 8 on page 187 1 Declares the iterator class The iterator must implement sq1j runtime ForUpdate in order to use it in a positioned UPDATE statement 2 In this example we combine the holdability and updateColumns attributes 3 Counts the number of rows changed 4 This is the positioned UPDATE statement It modifies the row the iterator is currently positione
465. ulate that support You have to run an additional setup step to create the SYSIBM INDOUBT table and its package via the DB2TAXAlndoubtUtil utility program DB2 UDB for z OS Version 8 has native XA two phase commit support in DRDA For additional information see the applicable announcement letter For the United States see IBM Software announcement letter 203 351 dated December 16 2003 For more information see DB2 for Z OS Application Programming Guide and Reference FOR JAVA Version 8 SC18 7414 01 and the Redbook DB2 for z OS and WebSphere The Perfect Couple SG24 6319 Chapter 3 Accessing DB2 from Java 35 3 3 Developing a Java application using JDBC Let us now look at how to develop a Java application that wants to talk to a database through JDBC In this section we focus on the connection part of the process How to connect to the DB2 system from a Java application More details on how to actually retrieve and update data is provided throughout the rest of this publication This section also applies to SQLJ programs as SQLJ uses JDBC to establish a connection to a database 3 3 1 Connecting to a database 36 Connecting to the database means you make the connection between the application and the driver that you will use in the program There are two methods that are used to set the driver and database for the connection gt The DriverManager interface gt The DataSource interface When using the DriverMana
466. ures as well but since we want to show you how to set up a WLM application environment we chose to create our own just for the stored procedures used by the DB2 Universal Driver for Java Common Connectivity In Figure 5 5 we make the necessary changes to the environment 1 Specify the name of the application environment DB7YJCC S Application Environment Notes Options Help Copy an Application Environment FORWARD is not active Command gt Application Environment DB7YJCC Requi red Description DB7Y stored procs used by JCC Subsystem Type DB2 Procedure Name DB7YWLM Start Parameters DB2SSN DB7Y NUMTCB 8 APPLENV DB7YJCC Limit on starting server address spaces for a subsystem instance 1 1 No limit 2 Single address space per system 3 Single address space per sysplex Ne m Figure 5 5 WLM Create application environment WLM100 2 Provide a meaningful description of the purpose of the environment 64 DB2 for z OS and OS 390 Ready for Java 3 Specify APPLENV DB7YJCC as the same name on the application environment and DB2SSN DB7Y as the DB2 subsystem this application environment address space will connect to in the start parameters 4 We indicate that there is no limit on the number of address spaces that we allow WLM to start 5 As we reused the existing DB7YPROC there is no need to create a new started task by that name in SYS1 PROCLIB If you want to use a differ
467. uses dynamic SQL under the cover This allows you to start prototyping your SQL statements very early on during the development process running uncustomized Once you are moving on to more detailed testing you can customize and run as static SQL Thus SQLJ combines the best of both worlds Easy development and good performance 3 5 4 SQLJ provides better authorization control 46 Perhaps even more important than the performance aspect of the static SQL model is the security aspect If you use dynamic SQL the user ID under which you run your program must have all privileges required by the code For example if the program wants to update rows in the DSN8710 EMP table and is running under your user ID your user ID must have UPDATE authority on that table or belong to a group that has If any other user wants to run your program he or she must also be given that authority There are two problems associated with this Managing these authorizations when a large number of people need to access the data as they all need to be granted access is a cumbersome and error prone task This can be partially solved by grouping users together in groups and granting access to the group instead of each individual user gt The second problem with this is that once a user has the authority to update a table he can do so not only using the application he wanted to use in the first place but also for example with interactive tools Obviously th
468. ut out this err err Notes on Example 7 5 on page 130 The JDBC connection to be used Any normal output such as result sets goes to this stream 2 3 while any error output when an exception was thrown goes to this one 4 If this flag is true commit automatically after each statement 5 The list of input streams from which SQL statement are read in turn Chapter 7 JDBC revisited 131 132 6 This constructor uses System out and System err respectively as output and error streams 7 This constructor allows output and error messages to go to an arbitrary stream Adding input streams Next we implement methods to add an input source for SQL statements Example 7 6 In the most general form this can be a Java text input stream a Reader for convenience there are two additional methods which take the name of a file and a single SQL statement string respectively Each method adds the input source to the list of streams to be processed Example 7 6 Spufi addStream addFile and addSql Add a stream to read SQL statements from param reader Stream to add public void addStream Reader reader streams add reader Add a file to read SQL statements from param filename Name of the file throws FileNotFoundException File could not be found public void addFile String filename throws FileNotFoundException addStream new FileReader filename
469. ution is to use SQLJ for all stable SQL while from within the same program coding all changing SQL in JDBC methods 3 5 9 SQLJ JDBC interoperability JDBC and SQLJ are complementary technologies and can be mixed and matched within an application for example SQLJand JDBC can share the same connections SQLJiterators and JDBC result sets can be converted to each other This is explained in more detail in Interoperability between JDBC and SQLJ on page 198 In fact there is no SQLJ equivalent to the PREPARE EXECUTE and DESCRIBE statements used in other programming languages with embedded SQL support since this functionality is already covered with JDBC 3 6 Summary Table 3 3 summarizes the differences between JDBC and SQLJ that we discussed in the previous sections Table 3 3 Differences between JDBC and SQLJ summarized JDBC SQLJ Dynamic SQL Static SQL Call level interface API Embedded SQL language extension No precompile or binding necessary Precompile necessary bind optional but strongly recommended Chapter 3 Accessing DB2 from Java 49 Standard part of the Java platform Not a standard part of Java but an ISO standard ISO IEC 9075 10 2000 50 DB2 for z OS and OS 390 Ready for Java Part 2 Prerequisites and setup In this part we discuss how to set up your Java and DB2 environment properly to be able to fully enjoy this brave new world As we do most of our development through WebSphere Stu
470. utoCommit property set to true which forces a commit after every single SQL statement You should turn auto commit off and commit only when appropriate 13 1 3 Only retrieve update columns as needed Column processing is one of the major factors in CPU resource consumption The two primary reasons for this are gt Strings for character string columns must be converted between Unicode Java and EBCDIC ASCII DB2 engine gt A Java object is created per column per row for those data types that are not primitive types in Java such as character string columns So in order to reduce this overhead you should only retrieve or update the columns you need 13 1 4 Store numbers as numeric data types This may be obvious but is still sometimes done wrong Use the DB2 numeric data types not CHARACTER for numeric data For example consider storing ZIP codes in an INTEGER column rather than in a CHARACTER 5 column This saves the overhead of creating an object if the column is declared NOT NULL and of EBCDIC Unicode conversion 13 1 5 Use DB2 built in functions 234 DB2 comes with many useful built in functions that are more efficient than their Java counterparts For example when retrieving a fixed width character data column you may want to get rid of the trailing blanks that DB2 appends whenever the value is shorter than the column s length You can use the Java String trim method like this sql SELECT JOB INTO job FR
471. ve compatible types In the case of named iterators the column names of resultset and iter must match as well 10 8 2 Converting an SQLJ iterator into a JDBC result set 198 You convert an SQLJ iterator into a JDBC result set via the getResultSet method of the iterator class ResultSet rs iter getResultSet Note that after converting the iterator to a ResultSet object you may subsequently fetch the rows only by using the ResultSet Do not use FETCH INTO in the case of positioned iterators or the column accessor methods in the case of named iterators The results are implementation defined which is another way of saying that it may or may not work as you expect It does work with the DB2 Universal Driver for Java Common Connectivity but again it is not guaranteed to work with other SQLJ implementations and should be avoided DB2 for z OS and OS 390 Ready for Java 11 SQLJ revisited In this chapter we develop a more realistic SQLJ application demonstrating various techniques such as Inserting updating and deleting rows Single row select SELECT INTO Iterating through result sets FETCH Positioned UPDATE and DELETE Inserting and retrieving LOB data YYYY Y Copyright IBM Corp 2003 All rights reserved 199 11 1 Introduction In the following sections we create a simple SQLJ application that reads and updates one of the sample tables that come with DB2 We assume that you have the necessary authori
472. ved by using the DataSource interface which allows system administered and secure definitions of the data connections between an application and its data sources If you have the technology to support this method JDBC 2 0 compliant drivers then DataSource connections should be the method of choice 3 3 3 Connecting using the DataSource API To avoid the problems that the DriverManager has with portability issues another method of storing data source information is defined in JDBC 2 0 This method uses a new class called Chapter 3 Accessing DB2 from Java 39 40 the DataSource With this object all data pertaining to the connection is stored in a system managed area that is accessed through a logical name The thing to remember is that this DataSource object is persistent and once it has been created it is maintained as a system object Once created the DataSource object is brought into the application referring to its logical name The beauty of this method is that you can have DataSource objects with the same logical names defined on a different platform that contains platform specific information and the program will automatically plug into it when it is moved to that platform Setting up a DataSource The DataSource object is defined and managed within the application server in a normal development platform where development work is done on a client platform such as Windows with the code ultimately being deployed on the z OS or O
473. vel UR Binding package HELLO2 at isolation level CS Binding package HELLO3 at isolation level RS Binding package HELLO4 at isolation level RR Bind complete for com ibm itso sg246435 sqlj Hello_SJProfiled Tip The Ant script uses the standard Windows CLASSPATH to search for the SQLJ customizer Therefore make sure that db2jcc jar and db2jcc_license_cisuz jar appear in the standard windows CLASSPATH Verifying that the packages have been created Next we verify that the SQLJ profile customizer indeed created the packages in the database for our two SQLJ source files Start your favorite interactive query tool for example QMF or the DB2 Command Center and issue the following query SELECT COLLID NAME CREATOR TIMESTAMP FROM SYSIBM SYSPACKAGE WHERE CREATOR YOUR USERID Assuming that you never created any other packages in the database your output will look similar to Example 9 4 Example 9 4 Excerpt from SYSIBM SYSPACKAGE catalog table after profile customization COLLID NAME CREATOR TIMESTAMP lzlzrzLi SG246435 HELLO1 BART 2003 08 08 19 35 20 341008 SG246435 HELLO2 BART 2003 08 08 19 35 20 481462 56246435 HELLO3 BART 2003 08 08 19 35 20 613669 56246435 HELLO4 BART 2003 08 08 19 35 20 745443 DSNE6101 NUMBER OF ROWS DISPLAYED IS 4 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL SQLCODE IS 100 Verifying that your application uses static SQL The fact that a package e
474. ver the error will be reported against the FETCH statement not the SELECT statement or the iterator Also the data types in the iterator declaration must be compatible with the column types The SQLJ profile customizer will check and report any data type mismatches if online checking is enabled You then retrieve the rows in the result set by executing FETCH statements usually in a loop The column values will be assigned to the corresponding host variables in the INTO list To find out if all the rows have been retrieved you call the endFetch method of the iterator This technique is demonstrated in Example 10 3 on page 183 182 DB2 for z OS and OS 390 Ready for Java Example 10 3 Fetching result set rows into host variables String name null int edLevel Timestamp dateHired null try while true sql FETCH iter INTO name edLevel dateHired h if iter endFetch break System out println name t edLevel t dateHired finally iter close Note that endFetch reports the result of the last attempt to retrieve a row returning false when a row had been fetched successfully and true if no more rows could be retrieved This implies that it returns a meaningful result only after a FETCH has been performed The following snippet Example 10 4 is not guaranteed to work as expected Example 10 4 Wrong usage of endFetch Do not try this at home WRONG will not work
475. viders R useful information i WebSphere JMS Provider Licensed Material Property of IBM WebSphere MQ JMS Provider Mail Providers Resource Environment Providd URL Providers 9 WebSphere Developer 9 InfoCenter Resource Adapters Domain R P The complete source for product Security Getthe latest technical articles best documentaton including ta eference Environment practices tutorials and much more in the ation on product System Administration WebSphere Application Server Zone features and functions Influence th ution of WebSphere Applicatio ver and request new product features Troubleshooting Figure 5 29 JDBC providers Chapter 5 Setup 87 88 A screen frame similar to Figure 5 30 will be displayed To specify the scope of the provider select Server and then Apply Once the scope is defined to the server select New to create a new JDBC provider d WebSphere Administrative Console Microsoft Internet Explorer GE x File Edit View Favorites Tools Help Ea Bak gt amp A Bsearch Favorites media lt 4 B 5 E Address http Jwtsc63 itso ibm com 9080 admin secure logon do eal Go WebSphere Application Server Administrative Console mA E e E Version 5 E S e Home Save Preferences Logout Help User ID bart JDBC Providers cel111 Servers JDBC providers are used by the installed applications to access data from databases i
476. vlet will generate an HTML page the default superclass javax servlet http HttpServlet is fine Click Next and unselect the doPost check box as we do not need a doPost method in our application Click Finish to create the Servlet In the editor window that opens you will see the stub source code which contains only one method called doGet This method is invoked by the Web server when a client makes a request to that Servlet 15 2 1 Implementing the doGet method The implementation of the doGet method as created by the New Servlet wizard does nothing except call the doGet method provided by its superclass HttpServlet which in turn sends an error code back to the client So we have to replace that default implementation Chapter 15 Using Servlets to access DB2 251 We start with a skeleton Servlet that for the time being just displays a heading see Example 15 1 You will have to add an import statement for class java io PrintWriter not shown in the example code Tip A convenient way to have WSAD add the import statement for you is to use the Code Assist feature Position your text cursor after the class name PrintWriter and press CtrltSpace WSAD automatically adds an import statement Example 15 1 The doGet method skeleton public void doGet HttpServletRequest req HttpServletResponse resp throws ServletException IOException PrintWriter out resp getWriter out println DOCTYPE HTML PUBLIC W3C
477. want to retrieve the value that DB2 generated for that column you may need it for example as a foreign key value in a dependant table You can use the DB2 specific function IDENTITY VAL LOCAL to retrieve the last value generated Beginning with JDBC 3 0 however there is a mechanism that allows an application to retrieve the value without using vendor specific extensions and this API is supported by the DB2 Universal Driver for SQLJ and JDBC Example 12 5 shows how to use this API For further details refer to Section 13 6 of the JDBC 3 0 API specification The JDBC 3 0 specification can be found on the Web at http java sun com products jdbc download html Example 12 5 Retrieving auto generated keys Statement stmt conn createStatement indicate that the key generated is going to be returned int rows stmt executeUpdate INSERT INTO ORDERS ISBN CUSTOMERID VALUES 195123018 BILLG Statement RETURN GENERATED KEYS ResultSet rs stmt getGeneratedKeys if rs next retrieve the new key value int orderID rs getInt 1 Chapter 12 The DB2 Universal Driver 231 Note Unfortunately there is no equivalent construct for SQLJ you have to use the IDENTITY VAL LOCAL function sql SELECT IDENTITY VAL LOCAL INTO orderID FROM SYSIBM SYSDUMMY1 In DB2 for z OS Version 8 you will be able to use the SELECT FROM INSERT construct to retrieve generated values of a table sql SELECT orde
478. when the applet is visible such as graphic processes Executed whenever the applet goes out of view This method will be overridden to stop processes that are not required while the applet is hidden 16 DB2 for z OS and OS 390 Ready for Java Loic RAE RNEEENENIKONENE destroy Called just before the applet is unloaded and allows final release of the applet resources Applets are downloaded from the Web server onto the clients each time they are executed A further download is started for each class the applet requests This means there can be a performance hit if the applet is large or requests to many classes Therefore applets tend to be typically small single function processes 2 4 3 Java Servlets A Java Servlet is a piece of Java code that is written to operate within the confines of a Java enabled server They extend the functionality of Web application servers and provide a component based platform independent method for building Web applications Although Java allows for other protocols to access a Servlet most of the requests emanate from a Web browser and use the HTTP protocol The Servlet is mapped to a URL On receiving a request for that URL the server executes the Servlet by invoking the service method of the Servlet The Servlet then processes the request and responds to it Normally the response is in the form of an HTML document to produce dynamic Web content Servlets are run in a section of the Web server c
479. ws an application to issue a new operation to make a new copy of the static cursor that can be used with different host variable input at DB2 for z OS and OS 390 Ready for Java OPEN time Since a cursor currently has to be unique based on the fully qualified package name consistency token and section number it is not possible for an SQLJ application to have more than one instance of an open cursor A second instance or OPEN of the same cursor would not be allowed in DB2 and results in an SQLCODE 502 DB2 for z OS Version 8 allows multiple opens for the same cursor used by an SQLJ iterator The DB2 server now provides a unique identifier to the requester for each open cursor or result set The request can then manage the multiple instances using the unique cursor identifier Embedded SQL applications non SQLJ applications will not be allowed to take advantage of this new processing 12 3 9 SAVEPOINT support The DB2 Universal Driver for SQLJ and JDBC supports the SAVEPOINT mechanism specified in the JDBC 3 0 specification For further details about savepoints refer to the JDBC 3 0 specification 12 3 10 Auto generated keys Like many other database servers DB2 has a mechanism that allows you to automatically generate a new unique key value whenever a row is inserted In the case of DB2 you declare a column to be an IDENTITY column Of course after inserting a new row into a table containing an IDENTITY column you probably
480. ws communications between a browser and a server to be encrypted and secure SSL prevents eavesdropping tampering or message forgery on your Internet or intranet network Security Features in Java that prevent applets downloaded off the Web from deliberately or inadvertently doing damage One such feature is the digital signature which ensures that an applet came unmodified from a reputable source Serialization Turning an object into a stream and back again Server The computer that hosts the Web page that contains an applet The class files that make up the applet and the HTML files that reference the applet reside on the server When someone on the Internet connects to a Web page that contains an applet the server delivers the class files over the Internet to the client that made the request The server is also known as the originating host Server bean The bean that is distributed using RMI services and is deployed on a server Servlet See Java Servlet Single precision A floating point number that contains 32 bits See also double precision Glossary 323 Sourced function A function that is implemented by another built in or user defined function already known to the database manager This function can be a scalar function or a column aggregating function it returns a single value from a set of values for example MAX or AVG Contrast with external function and built in function SQL Structured Query Language A
481. x FROM PROD EMP x Table TEST EMP Table PROD EMP Table TEST EMP_ rabie PROD EMP Table TEST TEAM Table PROD TEAM Table TEST TEAM Table PROD TEAM Table TEST LOC Table PROD LOC Table TEST LOC Table PROD LOC Database DBAPP1 Database DBAPP1 Database DBAPP1 Database DBAPP1 Table Table Table able TEST ACCOUNT PROD ACCOUNT TEST ACCOUNT PROD ACCOUNT Table Table Table able TEST BRANCH PROD BRANCH TEST BRANCH PROD BRANCH Table TEST TRANS Table PROD TRANS Table able TEST TRANS PROD TRANS Database DBAPP2 Subsystem DB2T Database DBAPP2 Database DBAPP2 Database DBAPP2 Subsystem DB2P Instance DB2T Instance DB2P DB2 for z OS and OS 390 DB2 UDB for Windows Figure 3 9 Scope of connection 3 3 2 Using the DriverManager interface This is the older method of connecting to a data source and is available in all flavors of JDBC When using the DriverManager interface the application must define specific JDBC driver classes and database names This limits portability If an application is in development and requires a local database for testing purposes when the application is migrated to the production environment the code has to be changed to reflect the change of drivers and the database conn
482. xception will be thrown if no row or more than one row was found By the way the SQL J version is more efficient as well JDBC has to make four calls to DB2 prepare statement fetch row fetch row close statement whereas the SQLJ version only has to do one single SELECT INTO call Note The SQLJ version will only be more efficient when the program has been customized and bound If it is running uncustomized it will emulate SELECT INTO by using result sets under the cover just like the JDBC version Example 3 4 JDBC vs SQLJ Single row query PreparedStatement stmt conn prepareStatement SELECT MAX SALARY AVG SALARY FROM DSN8710 EMP rs statement executeQuery if Irs next Error no rows found maxSalary rs getBigDecimal 1 avgSalary rs getBigDecimal 2 if rs next Error more than one row found rs close stmt close sql ctx SELECT MAX SALARY AVG SALARY INTO maxSalary avgSalary FROM DSN8710 EMP h Chapter 3 Accessing DB2 from Java 43 As a last example consider the INSERT statement in Example 3 5 Again the SQLJ code is easier to read and to maintain since the boilerplate code for supplying parameters to the statement and for resource cleanup is generated for you by the SQLJ translator Example 3 5 JDBC vs SQLJ INSERT statement stmt conn prepareStatement sql ctx INSERT INTO DSN8710 EMP INSERT INTO DSN8710 EMP EMPNO FIRS
483. xists is of course no guarantee that the package is actually being used In order to verify that our SQLJ Hello application is really using static SQL you can use your DB2 monitor After the customization run the program again from WSAD using the same run configuration that we set up in the previous chapter for the Hello SQLJ program DB2 for z OS and OS 390 Ready for Java Following are a number of screen captures from DB2 Performance Expert for z OS to demonstrate that we are actually using static SQL Figure 9 7 shows that the DISTSERV plan is used as usual since we come in through DRDA and that we are executing the program HELLO2 remember 2 is the CS version of the package from collection SG246435 amp DB7Y Thread Summary Thread Summary Selected View Tools Help amp e 7 Curentime setting I 8 23 03 12 07 51 AM Primary Authorization Plan Collection ID Program Name Elapsed Class 1 Elapsed Class 2 Ti DISTSERV 50246435 IHELLO2 00015 0 00084 FPEPX110 DGO SDOB 0 07 28 0 01144 N NIP NIP 0 07 28 0 00186 0 NIP NIP 0 07 28 0 00336 D NIP NIP 0 05 41 0 00734 0 N N N P N P NIP NIP NIP NIP NIP NIP n Figure 9 7 DB2 PE thread summary In case you want to see more details you can zoom into the thread and look at the SQL statement that is being executed Figure 9 8 on page 164 We see our cursor declaration from the SQLJ program This is true static SQL indeed Chapter 9 The SQLJ
484. y are used whenever a double byte character string might exceed the limits of the VARGRAPHIC type DBMS Database management system DBRM See Database Request Module DDF See Distributed Data Facility DDL See Data Definition Language Distributed Data Facility DDF A set of DB2 UDB for OS 390 components through which DB2 UDB for OS 390 communicates with another RDBMs Distributed relational database architecture DRDA A connection protocol for distributed relational database processing that is used by IBM s relational database products DRDA includes protocols for communication between an application and a remote relational database management system and for communication between relational database management systems DLL dynamic linklibrary A file containing executable code and data bound to a program at load time or run time rather than during linking The code and data in a dynamic link library can be shared by several applications simultaneously The DLLs Enterprise Access Builders also generate platform specific DLLs for the workstation and OS 390 platforms DML See Data Manipulation Language Double precision A floating point number that contains 64 bits See also single precision Double byte character large object DBCLOB See DBCLOB DRDA Distributed relational database architecture Dynamic bind A process by which SQL statements are bound as they are entered Dynamic SQL SQL statements that are prepared
485. ys customize with online checking enabled on page 236 below a non matching data type may result in a poor access path if that host variable is part of a predicate 13 3 2 Use positioned iterators not named iterators Named iterators are actually implemented as a wrapper around positioned iterators with an associated hash table that maps column names to column numbers For best performance we recommend that you use positioned iterators that do not have this overhead 13 3 3 Always customize with online checking enabled 236 Online checking is especially important when predicates using host variables are involved As explained in Use matching data types on page 235 above host variables should match their corresponding columns in data type and size In order for a predicate to use a matching index scan the definition in the Java package must match the definition in the DB2 catalog both in terms of data type and length Because Java String objects do not have a concept of length that information can only be obtained from the catalog This is part of online checking process Suppose you have the following SQL statement sql SELECT NAME FROM SYSIBM SYSTABLES WHERE CREATOR creator h Example 13 1 shows an excerpt of the Explain table after two binds of the generated serialized profile one with online checking enabled the other one with online checking disabled While the first one uses an index the second does a table space sc
Download Pdf Manuals
Related Search
Related Contents
User`s Manual - Nanov Display, Inc. 添付文書 Manuale di istruzioni - psn serpillieres jetable Audiovox MMD92 User's Manual USO DE ADOBE CONNECT Requisitos de uso Entrar en la sala Olympus 10 42 EXWP I User's Manual EXSYS USB to 2S Serial RS-232 port (Prolific Chip-Set) Adaptador USB Ryś - Retro 7-bit - 7 Copyright © All rights reserved.
Failed to retrieve file