CachedRowSet and ResultSetMetaData.getXXX() methods

I have created a reporting application in J2EE environment, which uses CachedRowSet and was working just fine. Then I decided to improve this application, in part by using ResultSetMetaData to get things like number of columns, column names, column types, columnlabels, using the getXXX() methods of ResultSetMetaData. Turns out, none of the getXXX() methods seem to work, and others cause very confusing things to happen, ultimately crashing my program. For example:

1.int numberOfColumns = crs.getMetaData().getColumnCount();

2. System.err.println("Number of Columns = "+numberOfColumns);

3.for (int i = 0; i < numberOfColumns; i++){

4.String colClassName = crs.getMetaData().getColumnClassName(i);

5.System.err.println("Column Class Name is "+colClassName);

6.String colName = crs.getMetaData().getColumnName(i);

7.if (colName !=null)

8. System.err.println("Column Name is"+colName);

9.else

10.System.err.println("Column Name is null.");

Line 2 does not even print out, but program execution continues. The program then enters thefor loop, indicating to me thatnumberOfColumns is, in fact, initialized with an int > 0. The program then crashes after line 6. I have never seen such behavior!

What am I doing wrong?

Thanks,

Pat Garner

[1708 byte] By [pgarner] at [2007-9-26 2:20:19]
# 1
you should try creating a ResultSetMetaData Object using the original ResultSet. If you want information on the RowSet then use the RowSetMetaData Interface.Jamie
jlrober at 2007-6-29 9:23:57 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
What I meant to say was that you are using a ResultSetMetaData interface on a RowSet object. Use the RowSetMetaData interface instead.Jamie
jlrober at 2007-6-29 9:23:57 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Heads UP!!

In Java, _almost_ everything starts @ 0. But not ResultSetMetaData!! It starts @ 1.

If you try to get a column[name][class], etc @ position 0, your program will throw a NullPointerException

Your code should read:

ResultSetMetaData rsmd = crs.getMetaData();

for(int i=1;i<=rsmd.getColumnCount();i++)

{

//do something

}

BTW - since the CachedRowSet is backed by a RowSet object, getting the ResultSetMetaData from the original RowSet will not be any different that getting it from the CRS.

Enjoy

- Justoon

justoon at 2007-6-29 9:23:57 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...