commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Graham <grahamdavid1...@yahoo.com>
Subject RE: [DbUtils] ClassCastException
Date Tue, 22 Jun 2004 19:35:49 GMT
It looks like you're trying to cast a Map returned from run.query() to a
List.  As another person said, Object.getClass() will tell you what class
any object reference is.  However, you don't really need that, you just
need to cast the result to a Map :-).

David

--- "Anderson, James H [IT]" <james.h.anderson@citigroup.com> wrote:
> Hi David,
> 
> Sorry about the mixup. Here's the routine with line 143 marked:
> 
>     /**
>        select with multiple params
>      */
>     public static List select(String query, Object[] param,
> ResultSetHandler h, boolean commit)
>     throws Exception {
> 
>         Connection conn   = null;
>         List       result = null;
> 
>         try {
>             conn = RecomDataSource.getConnection();
>             QueryRunner run = new QueryRunner();
>             result = (List) run.query(conn, query, param, h);		// Line
> 143
>         } catch (Exception e) {
>             System.out.println(e.getMessage());
>             e.printStackTrace();
>             throw e;
>         } finally {
>             if (commit) {
>                
> org.apache.commons.dbutils.DbUtils.commitAndCloseQuietly(conn);
>             } else {
>                 org.apache.commons.dbutils.DbUtils.closeQuietly(conn);
>             }
>         }
> 
>         return result;
>     }
> 
> I can see where the problem is likely to be on my side. Unfortunately
> I'm having some problems with connectivity to the database at the moment
> and probably won't be able to try your suggestion with the debugger
> until tomorrow. I'm still a little new to java, so let me ask you this.
> Clearly the result being returned by query() is not a List and hence the
> ClassCastException, but how can I determine at runtime what type of
> object it is? Must I do a bunch of instanceof tests, or is there a
> cleaner way?
> 
> Thanks,
> 
> jim
> 
> 
> -----Original Message-----
> From: David Graham [mailto:grahamdavid1980@yahoo.com]
> Sent: Tuesday, June 22, 2004 12:49 PM
> To: Jakarta Commons Users List
> Subject: Re: [DbUtils] ClassCastException
> 
> 
> 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]" <james.h.anderson@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
> > 
> > 
> 
> 
> 
> 		
> 
=== message truncated ===



	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
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


Mime
View raw message