Archive for the 'Programming' Category

Quick Java Tips - List conversions

Here are some quick Java tips. This is based on my recent code reviews.

1. How to convert HashMap keys to an ArrayList?
new ArrayList(map.keySet());

2. How to convert an Array to an ArrayList?
new java.util.ArrayList(java.util.Arrays.asList(array));

3. How to create and initialize a List in one step?
java.util.List = java.util.Arrays.asList(new String[] {”1″, “2″});

Limiting query results in DB2. Replacing ROWID in Oracle

While working with DB2 tables, many times we may need to limit query results. This is especially true when we just want to test out our queries. If the data volume is huge, the queries can take hell lot of time.

In oracle, the solution is to use ROWID in WHERE clause. In DB2, you can use the construct FETCH FIRST 10 ROWS ONLY. For example,

SELECT * FROM CUSTOMER FETCH FIRST 10 ROWS ONLY;

Error in RAD 6.0: Resolve against non-hierarchical or relative base error

RAD error due datasource configurationWhile changing the datasource configuration in Rational Application Developer 6.0, I across the following error.

java.lang.IllegalArgumentException: resolve against non-hierarchical or relative base

This turned out to be a very nasty problem. I first tried removing the datasource configuration and reconfiguring it. When that didn’t work tried deleting the server configuration. No luck!

On googling, I came across this solution.
http://www-1.ibm.com/support/docview.wss?uid=swg21218587

Solution
Open the resources.xml and find:
<cmpDatasource href=”tempworkarea.xmi#DataSource_1125683336050″ mce_href=”tempworkarea.xmi#DataSource_1125683336050″/>

The only way to fix this error is to manually edit the resources.xml. On searching under runtimes folder, I came across multiple resources.xml files. Edited all of them and removed cmpDataSource references completely.

The steps are,

1. Shutdown RAD.
2. Remove cmpDataSource entries completely from resource.xml files under “runtimes” folder.
3. Start RAD and reconfigure datasources.

It worked!

Configuring DB2 connectivity in Rational Application Developer (RAD)

Rational Application Developer (RAD 6)When you start Web application in RAD 6.0 (Rational Application Developer 6.0), the first thing you need to configure is the database access. In this article, I will show you how to configure DB2 database access in RAD. I assume that DB2 is hosted on a Windows machine.

First thing you need to configure DB2 is the DB2 universal JDBC driver. These can be taken from DB2 server installation or from DB2 connect installation. The files you need are,

db2jcc.jar
db2jcc_license_cu.jar
db2jcc_license_cisuz.jar

Copy these files to a folder. This can be anywhere(I would suggest that you keep this under Rational root folder).

I assume you have already created a server configuration in RAD. Now right click on the server (from server window) and click on “Run administrative console”. This will open up the administrative interface. Typically it runs at “http://localhost:9060/ibm/console/“.

Login by giving any userId. Click on “JDBC providers” under “Resources”. On the right side you will see JDBC providers listed. Click on “New”. Now select values as shown below.

RAD JDBC Provider Settings

Click on Next. Now fill the driver paths in the Class path as shown below. You need to change this to point to the folder where JDBC jars are stored.

 RAD JDBC settings

Click on Apply button. Once you have pressed apply button, you will see a Save link above. Click on Save and then click on “Data sources” link on the right side. Fill in as shown below. Please substitute your DB2 server configuration (IP address, DB name and port etc.). Also note the JNDI name, which you will use in your application.

 RAD Datasource configuration

RAD Datasource settings
Click on “J2C authentication entries” on the right and fill in as shown below. This should be the DB2 userid/password. After completing this, press apply and return to Datasource screen.

RAD Datasource settings
Now you need to select the userid alias you have created from “J2C authentication entries” under component managed authentication alias as shown below. Please note that this is in Datasource screen.

RAD Datasource settings

Now you can apply and save the settings. Click on “Test Connection” to verify the settings. If you see a successful message, you are all set for DB2 access from your Web application!

Any problems? Mail me!

How do I find Struts version?

Recently while working on a J2EE project, I had to know the Struts version being used. The IDE used was Rational Application Developer. I couldn’t find an option in IDE which tells me the Struts version it is using!

The only way I could see was to open the struts.jar in Winzip and inspect the Manifest.mf. It showed Implementation-Version : 1.1. This means that RAD by default uses Struts 1.1.

I wanted to use the latest stable version of Struts (which is 1.3.5). Hence I deleted all the jars from WEB-INF/lib and then copied new jars from struts-1.3.5-lib.zip. Thats it!