Home > AS/400 Tips > WebSphere Strategies for iSeries professionals > Building dynamic JDBC components and utilities
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

WEBSPHERE STRATEGIES FOR ISERIES PROFESSIONALS

Building dynamic JDBC components and utilities


Paul Holm and Colin Slade
07.15.2004
Rating: -4.83- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Powerful generic utilities and products can be built that allow you to query,
display, update and delete records. These utilities may be similar in function to
QUERY/400 and DFU for the Web. The key to doing this is the usage of JDBC
metadata to discover at runtime what libraries, tables and fields are being
operated on.

To do that, we must be able to feed a JDBC connection a SQL string and
then dynamically list the records returned, along with the library, table and
field names that data came from. For example, select * from invoice should
produce a list of invoices, while select * from orders will display a list of
orders. Prior to V5R2, the Java toolbox for the iSeries did not indicate the
library (schema) where the data field was returned from, which made it
difficult to determine when writing generic update code. In V5R2, the
connection can be configured as to allow extended metadata to be returned.

In the following example, we will create a generic utility illustrated by code
snipits that allows any JDBC/SQL Select statement to be run. The result will be
a list of the records returned with a column header that shows the library,
table and column name for each field returned. For real production usage,
simply format the column headers to your liking.

Step1:

First we must create a connection that specifies "extended metadata" =
true property of the connection. Your code would look something like this:

Connection con = 
DriverManager.getConnection("jdbc:as400://server_ip;extended
metadata=true;trace=false;" , username, password);

The server_ip is the address of your iSeries, and the username and password
are used for authentication to the server. To give you a better idea about what
the extended metadata property does, below is IBM's description and warnings.
NOTE: This example uses IBM's ToolBox for Java (JT400.JAR).

Information from manual
"Extended metadata" -- Specifies whether the driver requests extended
metadata from the server. Setting this property to true increases the accuracy
of the information returned from the following ResultSetMetaData methods:
getColumnlabel(int), isReadOnly(int), isSearchable(int)

Other information:
Additionally, setting this property to true enables support for the
ResultSetMetaData.getSchemaName(int) method. Setting this property to true
may degrade performance because it requires retrieving more information from
the server. Leave the property as the default (false) unless you need more
specific information from the listed methods. For example, when this property
is off (false), ResultSetMetaData.isSearchable(int) always returns "true"
because the driver does not have enough information from the server to make
a judgment. Turning on this property (true) forces the driver to get the correct
data from the server. You can use extended metadata only when connecting to
a server running OS/400 V5R2 or later.

Step 2:

Using the connection created above, create an SQL statement from a string.
The string may be dynamically composed.

Example: Statement smt = conn.createStatement();

String anySQLSelect = 'select * from qiws.qcustcdt';

ResultSet rs = smt.executeQuery(anySQLSelect);

Step 3:

Loop through the resultSet, getting the column, table and library from the
metadata.

ResultSetMetaData rsmd = null;
while (rs.next == true)
{  //get resultSetMetaData about columns.
  rsmd = rs.getMetaData();
   //number of columns is set and used to dynamically know how many flds
  int numOfColumns = rsmd.getColumnCount();
   for(int columnCount = 1; columnCount <= numOfColumns; ++columnCount)
   { //loop thru rsmd to get column, table, and library/schema information 
     //to output
   System.out.println("For Column " + rsmd.getColumnName(columnCount));
   System.out.println("the table name is"+rsmd.getTableName(columnCount));
   System.out.println("and the Schema or Library name is" + 
     rsmd.getSchemaName(columnCount));                                                 
    
// now dynamically output the data returned from the rs. 
  rs.getObject(columnCount)
 }
}

Summary
The key additional feature available with "extended metadata" is the ability to
dynamically get the schema name, which in the iSeries means the library
name. This is very powerful and also works for SQL Joins. The only thing you
would need to change is the SQL statement to include a join or multiple joins.

What this all means is that IBM has provided Web developers with a new
capability to develop generic components and utilities without requiring them
to know the library name. Among other things it allows allow for the usage of
library lists, which can be specified on the connection.

---------------------------
About the authors: Paul Holm is a former IBM-Rochester WebSphere,
Java, and DB2 developer/consultant. He specializes in helping iSeries RPG
shops develop Web-based applications. Paul is currently a lead architect for the
"WOW" product, which is Query/400 and DFU for WebSphere (aka WebSphere on steriods).
Paul can be reached at pholm@planetjavainc.com.

Colin Slade is a Web developer at PlanetJ Corp.


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Web Development
Application modernization strategies for System i
RPG application modernization for i5
Web skills crucial to iSeries programmer professional development
System i Web interface could boost the platform
Free System i development tools rebuttal
COMMON product round-up: Modernizing the IBM System i
Top 10 System i white papers
Enterprise open source basics
Make WebSphere work for you
WebSphere for System i tutorial

Web Tools
Search400.com Products of the Year 2008
Application modernization strategies for System i
Natively supported Web applications for Power running i
System i PHP authoring tool tries to fill void in Web-coding know-how
Lazy coder: What does PHP on the i5 mean?
IBM WebSphere Portal Primer, Second Edition -- Chapter 2
Make WebSphere work for you
WebSphere for System i tutorial
Users gain from Red Hat's JBoss buy
Choices for running Web workloads on iSeries

Web Servers
System i no longer the stepchild of IBM's world
Connecting WebSphere to AS/400 for image retrieval
Weaving in WebSphere
SOA enhancements drive IBM WebSphere feature packs
SOA means money for IT workers
Enable J2EE app on WAS to access DB2
Securing Apache: Keeping patches current
IBM runs USOpen.org on Power boxes
MoMA's IT makeover a mix of old and new
How does RPG talk to a browser?

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
WebSphere Development Studio Client (WDSC)  (Search400.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts