You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nathan Maves <Na...@Sun.COM> on 2004/04/22 01:15:46 UTC

what could cause this Class Cast exception

I know this is not truly struts related but...

I had a servlet that loaded blobs from oracle (pdf files).  This worked  
perfect.  I copied the code into my action and I know receive this  
error:

java.lang.ClassCastException
         at  
reporting.viewer.presentation.actions.ViewReport.execute(ViewReport.java 
:86)
         at  
org.apache.struts.action.RequestProcessor.processActionPerform(RequestPr 
ocessor.java:484)
         at  
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java: 
274)

here is the code....


	// Obtain our environment naming context
         Context initCtx = new InitialContext();
         Context envCtx = (Context) initCtx.lookup("java:comp/env");

         // Look up our data source
         DataSource ds = (DataSource)  
envCtx.lookup("jdbc/report-viewer");

         // Allocate and use a connection from the pool
         Connection conn = ds.getConnection();
         PreparedStatement ps = null;
         ResultSet rs = null;

	.....

	if (rs.next()) {
                 OracleResultSet ors = (OracleResultSet)rs;     
----------------(THIS IS LINE 86 of ViewReport.java)
                 BLOB blob = ors.getBLOB(2);

                 if (blob != null) {
                     response.setContentType(rs.getString(1));

                     InputStream is = blob.getBinaryStream();
                     int size = blob.getBufferSize();
                     byte[] buffer = new byte[size];
                     int length = -1;

                     while ((length = is.read(buffer)) != -1) {
                         out.write(buffer, 0, length);
                     }
                 }
             }


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


Re: what could cause this Class Cast exception

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Well you need to get the original ResultSet out of the wrapper - I don't use
dbcp, but looking at the source code it looks like getInnermostDelegate()
does that.

http://cvs.apache.org/viewcvs.cgi/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/

The thing is, do you need to cast it to an OracleResultSet at all, can't you
use the standard ResultSet.getBlob(int) method?

Niall

----- Original Message ----- 
From: "Nathan Maves" <Na...@Sun.COM>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Thursday, April 22, 2004 1:54 AM
Subject: Re: what could cause this Class Cast exception


> So what would be the best way of converting that wrapper class into an
> OracleResultSet?
>
> Is the problem that an OracleResultSet extends java.sql.ResultSet and
> the Wrapper implements it?
>
> Nathan
> On Apr 21, 2004, at 5:29 PM, Niall Pemberton wrote:
>
> > So if its not an OracleResultSet, then what kind is it?
> >
> > I believe if you use something like Commons dbcp it uses wrappers for
> > everything - DelegatingResultSet
> >
> > Niall
> >
> > ----- Original Message -----
> > From: "Nathan Maves" <Na...@Sun.COM>
> > To: "Struts Users Mailing List" <us...@struts.apache.org>
> > Sent: Thursday, April 22, 2004 12:15 AM
> > Subject: what could cause this Class Cast exception
> >
> >
> >> I know this is not truly struts related but...
> >>
> >> I had a servlet that loaded blobs from oracle (pdf files).  This
> >> worked
> >> perfect.  I copied the code into my action and I know receive this
> >> error:
> >>
> >> java.lang.ClassCastException
> >>          at
> >> reporting.viewer.presentation.actions.ViewReport.execute(ViewReport.ja
> >> va
> >> :86)
> >>          at
> >> org.apache.struts.action.RequestProcessor.processActionPerform(Request
> >> Pr
> >> ocessor.java:484)
> >>          at
> >> org.apache.struts.action.RequestProcessor.process(RequestProcessor.jav
> >> a:
> >> 274)
> >>
> >> here is the code....
> >>
> >>
> >> // Obtain our environment naming context
> >>          Context initCtx = new InitialContext();
> >>          Context envCtx = (Context) initCtx.lookup("java:comp/env");
> >>
> >>          // Look up our data source
> >>          DataSource ds = (DataSource)
> >> envCtx.lookup("jdbc/report-viewer");
> >>
> >>          // Allocate and use a connection from the pool
> >>          Connection conn = ds.getConnection();
> >>          PreparedStatement ps = null;
> >>          ResultSet rs = null;
> >>
> >> .....
> >>
> >> if (rs.next()) {
> >>                  OracleResultSet ors = (OracleResultSet)rs;
> >> ----------------(THIS IS LINE 86 of ViewReport.java)
> >>                  BLOB blob = ors.getBLOB(2);
> >>
> >>                  if (blob != null) {
> >>                      response.setContentType(rs.getString(1));
> >>
> >>                      InputStream is = blob.getBinaryStream();
> >>                      int size = blob.getBufferSize();
> >>                      byte[] buffer = new byte[size];
> >>                      int length = -1;
> >>
> >>                      while ((length = is.read(buffer)) != -1) {
> >>                          out.write(buffer, 0, length);
> >>                      }
> >>                  }
> >>              }
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >>
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>



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


Re: what could cause this Class Cast exception

Posted by Nathan Maves <Na...@Sun.COM>.
So what would be the best way of converting that wrapper class into an  
OracleResultSet?

Is the problem that an OracleResultSet extends java.sql.ResultSet and  
the Wrapper implements it?

Nathan
On Apr 21, 2004, at 5:29 PM, Niall Pemberton wrote:

> So if its not an OracleResultSet, then what kind is it?
>
> I believe if you use something like Commons dbcp it uses wrappers for
> everything - DelegatingResultSet
>
> Niall
>
> ----- Original Message -----
> From: "Nathan Maves" <Na...@Sun.COM>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Sent: Thursday, April 22, 2004 12:15 AM
> Subject: what could cause this Class Cast exception
>
>
>> I know this is not truly struts related but...
>>
>> I had a servlet that loaded blobs from oracle (pdf files).  This  
>> worked
>> perfect.  I copied the code into my action and I know receive this
>> error:
>>
>> java.lang.ClassCastException
>>          at
>> reporting.viewer.presentation.actions.ViewReport.execute(ViewReport.ja 
>> va
>> :86)
>>          at
>> org.apache.struts.action.RequestProcessor.processActionPerform(Request 
>> Pr
>> ocessor.java:484)
>>          at
>> org.apache.struts.action.RequestProcessor.process(RequestProcessor.jav 
>> a:
>> 274)
>>
>> here is the code....
>>
>>
>> // Obtain our environment naming context
>>          Context initCtx = new InitialContext();
>>          Context envCtx = (Context) initCtx.lookup("java:comp/env");
>>
>>          // Look up our data source
>>          DataSource ds = (DataSource)
>> envCtx.lookup("jdbc/report-viewer");
>>
>>          // Allocate and use a connection from the pool
>>          Connection conn = ds.getConnection();
>>          PreparedStatement ps = null;
>>          ResultSet rs = null;
>>
>> .....
>>
>> if (rs.next()) {
>>                  OracleResultSet ors = (OracleResultSet)rs;
>> ----------------(THIS IS LINE 86 of ViewReport.java)
>>                  BLOB blob = ors.getBLOB(2);
>>
>>                  if (blob != null) {
>>                      response.setContentType(rs.getString(1));
>>
>>                      InputStream is = blob.getBinaryStream();
>>                      int size = blob.getBufferSize();
>>                      byte[] buffer = new byte[size];
>>                      int length = -1;
>>
>>                      while ((length = is.read(buffer)) != -1) {
>>                          out.write(buffer, 0, length);
>>                      }
>>                  }
>>              }
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


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


Re: what could cause this Class Cast exception

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
So if its not an OracleResultSet, then what kind is it?

I believe if you use something like Commons dbcp it uses wrappers for
everything - DelegatingResultSet

Niall

----- Original Message ----- 
From: "Nathan Maves" <Na...@Sun.COM>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Thursday, April 22, 2004 12:15 AM
Subject: what could cause this Class Cast exception


> I know this is not truly struts related but...
>
> I had a servlet that loaded blobs from oracle (pdf files).  This worked
> perfect.  I copied the code into my action and I know receive this
> error:
>
> java.lang.ClassCastException
>          at
> reporting.viewer.presentation.actions.ViewReport.execute(ViewReport.java
> :86)
>          at
> org.apache.struts.action.RequestProcessor.processActionPerform(RequestPr
> ocessor.java:484)
>          at
> org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:
> 274)
>
> here is the code....
>
>
> // Obtain our environment naming context
>          Context initCtx = new InitialContext();
>          Context envCtx = (Context) initCtx.lookup("java:comp/env");
>
>          // Look up our data source
>          DataSource ds = (DataSource)
> envCtx.lookup("jdbc/report-viewer");
>
>          // Allocate and use a connection from the pool
>          Connection conn = ds.getConnection();
>          PreparedStatement ps = null;
>          ResultSet rs = null;
>
> .....
>
> if (rs.next()) {
>                  OracleResultSet ors = (OracleResultSet)rs;
> ----------------(THIS IS LINE 86 of ViewReport.java)
>                  BLOB blob = ors.getBLOB(2);
>
>                  if (blob != null) {
>                      response.setContentType(rs.getString(1));
>
>                      InputStream is = blob.getBinaryStream();
>                      int size = blob.getBufferSize();
>                      byte[] buffer = new byte[size];
>                      int length = -1;
>
>                      while ((length = is.read(buffer)) != -1) {
>                          out.write(buffer, 0, length);
>                      }
>                  }
>              }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>



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