You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Anderson, James H [IT]" <ja...@citigroup.com> on 2004/06/22 16:31:39 UTC

[DbUtils] ClassCastException

(This is a resend of a msg I sent yesterday which I did not see appear on the list. In fact, I haven't received any mail from the list for a day or two...)

I'm getting a ClassCastException that I don't understand. I hope someone can help with this, because I'd very much like to be able to use DbUtil on my project!

The stack trace:

org.apache.commons.dbutils.BasicRowProcessor$CaseInsensitiveHashMap
java.lang.ClassCastException: org.apache.commons.dbutils.BasicRowProcessor$CaseInsensitiveHashMap
	at com.ssmb.recom.aft.access.DbUtil.select(DbUtil.java:143)
	at com.ssmb.recom.aft.access.DbUtil.selectOneMapTrimmed(DbUtil.java:66)
	at com.ssmb.recom.aft.datamanager.AftRequests.nextBusinessDate(AftRequests.java:3454)
	at com.ssmb.recom.aft.datamanager.AftRequests.nextBusinessDateAfter(AftRequests.java:3489)
	at com.ssmb.recom.aft.datamanager.AftRequests.main(AftRequests.java:2920)

The invoking code:

        java.sql.Date sqlDate = new java.sql.Date( date.getTime() );

        String selectTradingDate = "select trading_date from trade_days where trading_date >= ? and banking_day = 'B'";

        Object[] params = { sqlDate };
        boolean  commit = true;

        Map h = null;
        try {
            h = (Map) DbUtil.selectOneMapTrimmed(selectTradingDate, params, commit);
        } catch (Exception e) {
            logger.error("No trade date >= " + date, e);
            throw new RuntimeException(e.getMessage());
        }
        java.util.Date tradeDate = (java.util.Date) h.get("trading_date");


My wrapper code as invoked in the code, above:

package com.ssmb.recom.aft.access;

import java.sql.*;
import java.util.*;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;
import org.apache.commons.dbutils.wrappers.*;
import org.apache.commons.beanutils.BeanUtils;

import com.ssmb.recom.datamgr.RecomDataSource;

public class DbUtil {

[...]

	/**
	   select one row, set param=null to avoid parameter substitution
	 */
	public static Object selectOneMapTrimmed(String query, Object[] param, boolean commit)
	throws Exception {

		MapHandler h = new MapHandler() {
				public Object handle (ResultSet rs) throws SQLException {
					StringTrimmedResultSet wrapped = new StringTrimmedResultSet(rs);
					rs = ProxyFactory.instance().createResultSet(wrapped);
					Object returnVal = null;
					try {
						returnVal = super.handle(rs);
					} catch (SQLException e) {
						System.out.println(e.getMessage());
						throw e;
					}
					return returnVal;
				}
			};

		if (param == null) {
			return select(query, h, commit);
		} else {
			return select(query, param, h, commit);
		}
	}

[...]

}

The table being queried:

table              database   creator    created   
------------------ ---------- ---------- ----------
trade_days         db2tst08   vimdbat    06/20/2000

column             type     length scale nulls
------------------ -------- ------ ----- -----
trading_date       date     4      0     y    
trading_day        char     1      0     y    
trading_dow        smallint 2      0     y    
banking_day        char     1      0     y    
--------------------


Note:

When I execute this query using vanilla JDBC, everything works fine.

Any help appreciated,

jim

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DbUtils] ClassCastException

Posted by David Graham <gr...@yahoo.com>.
You originally posted this to commons-dev and I replied :-).  Here it goes
again...

What does your DbUtil line 143 look like?  When you pause at that line in
your debugger, what class are you getting back and what class are you
trying to cast to?

David

--- "Anderson, James H [IT]" <ja...@citigroup.com> wrote:
> (This is a resend of a msg I sent yesterday which I did not see appear
> on the list. In fact, I haven't received any mail from the list for a
> day or two...)
> 
> I'm getting a ClassCastException that I don't understand. I hope someone
> can help with this, because I'd very much like to be able to use DbUtil
> on my project!
> 
> The stack trace:
> 
> org.apache.commons.dbutils.BasicRowProcessor$CaseInsensitiveHashMap
> java.lang.ClassCastException:
> org.apache.commons.dbutils.BasicRowProcessor$CaseInsensitiveHashMap
> 	at com.ssmb.recom.aft.access.DbUtil.select(DbUtil.java:143)
> 	at com.ssmb.recom.aft.access.DbUtil.selectOneMapTrimmed(DbUtil.java:66)
> 	at
>
com.ssmb.recom.aft.datamanager.AftRequests.nextBusinessDate(AftRequests.java:3454)
> 	at
>
com.ssmb.recom.aft.datamanager.AftRequests.nextBusinessDateAfter(AftRequests.java:3489)
> 	at
> com.ssmb.recom.aft.datamanager.AftRequests.main(AftRequests.java:2920)
> 
> The invoking code:
> 
>         java.sql.Date sqlDate = new java.sql.Date( date.getTime() );
> 
>         String selectTradingDate = "select trading_date from trade_days
> where trading_date >= ? and banking_day = 'B'";
> 
>         Object[] params = { sqlDate };
>         boolean  commit = true;
> 
>         Map h = null;
>         try {
>             h = (Map) DbUtil.selectOneMapTrimmed(selectTradingDate,
> params, commit);
>         } catch (Exception e) {
>             logger.error("No trade date >= " + date, e);
>             throw new RuntimeException(e.getMessage());
>         }
>         java.util.Date tradeDate = (java.util.Date)
> h.get("trading_date");
> 
> 
> My wrapper code as invoked in the code, above:
> 
> package com.ssmb.recom.aft.access;
> 
> import java.sql.*;
> import java.util.*;
> import org.apache.commons.dbutils.*;
> import org.apache.commons.dbutils.handlers.*;
> import org.apache.commons.dbutils.wrappers.*;
> import org.apache.commons.beanutils.BeanUtils;
> 
> import com.ssmb.recom.datamgr.RecomDataSource;
> 
> public class DbUtil {
> 
> [...]
> 
> 	/**
> 	   select one row, set param=null to avoid parameter substitution
> 	 */
> 	public static Object selectOneMapTrimmed(String query, Object[] param,
> boolean commit)
> 	throws Exception {
> 
> 		MapHandler h = new MapHandler() {
> 				public Object handle (ResultSet rs) throws SQLException {
> 					StringTrimmedResultSet wrapped = new StringTrimmedResultSet(rs);
> 					rs = ProxyFactory.instance().createResultSet(wrapped);
> 					Object returnVal = null;
> 					try {
> 						returnVal = super.handle(rs);
> 					} catch (SQLException e) {
> 						System.out.println(e.getMessage());
> 						throw e;
> 					}
> 					return returnVal;
> 				}
> 			};
> 
> 		if (param == null) {
> 			return select(query, h, commit);
> 		} else {
> 			return select(query, param, h, commit);
> 		}
> 	}
> 
> [...]
> 
> }
> 
> The table being queried:
> 
> table              database   creator    created   
> ------------------ ---------- ---------- ----------
> trade_days         db2tst08   vimdbat    06/20/2000
> 
> column             type     length scale nulls
> ------------------ -------- ------ ----- -----
> trading_date       date     4      0     y    
> trading_day        char     1      0     y    
> trading_dow        smallint 2      0     y    
> banking_day        char     1      0     y    
> --------------------
> 
> 
> Note:
> 
> When I execute this query using vanilla JDBC, everything works fine.
> 
> Any help appreciated,
> 
> jim
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org