Back Forum Reply New

StoredProcedure and column names

Hi -  

I've written a GenericStoredProcedure class which extends the Spring StoredProcedure class, as part of a DAO framework where I pass in the StoredProc name amp; parameters and have it create my domain objects which implement a domain interface.

Instances of the domain object are created and populated in the mapRow method my GenericRowMapper (which implements RowMapper), and I am iterating through the fields of my domain object and populating them by getting the contents of the column from the Result Set with the same name.  All good thus far.  

I want to use my domain objects with various different Stored Procs, each of which may returns Result Sets with any subset of the fields of my domain objects as their columns, however when I call getObject(colName) on a col that doesn't exist in the RS I get a SQLException.  

I need to find a way to determine the column names on my result set, which I can use to see if the current col of my domain object is in there.  If I was just using plain JDBC I would do something like the below code.

Should I use RowCountCallbackHandler somehow?  

Any help appreciated

Andrewpublic static void getColumnNames(ResultSet rs) throws SQLException {   if (rs == null) {     return;   }   ResultSetMetaData rsMetaData = rs.getMetaData();   int numberOfColumns = rsMetaData.getColumnCount();
   // get the column names; column indexes start from 1   for (int i = 1; i lt; numberOfColumns + 1; i++) {     String columnName = rsMetaData.getColumnName(i);     // Get the name of the column's table name     String tableName = rsMetaData.getTableName(i);     System.out.println(quot;column name=quot; + columnName + quot; table=quot; + tableName + quot;quot;);   } }

You can use ResultSetExtractor interface instead of RowMapper, retrieve jdbc metadata and parse returned result set in accordance with it.

Thanks for that - I've got a pretty nice solution going now using both RowMapper and ResultSetExtractor - similar to the one they have here :

2007/09/...ing-templates/
¥
Back Forum Reply New