wood burning stoves 2.0*
The moose likes JDBC and the fly likes ClassCastException when casting my resultset to OracleResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ClassCastException when casting my resultset to OracleResultSet" Watch "ClassCastException when casting my resultset to OracleResultSet" New topic
Author

ClassCastException when casting my resultset to OracleResultSet

Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
I am working on some code that creates an empty blob in the database and then selects it to write to it. I have successfully used the exact same code before in an application I have created.

I am adding it to a different application and I get the following error:
java.lang.ClassCastException: org.jboss.resource.adapter.jdbc.WrappedResultSet

I thought maybe it was not loading the oracle driver class but this seems to be working?

Here is the code it fails on:

conn = easDBUtil.getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(easDBConstants.UPDATE_DRAWING);
pstmt.setInt(1,pkey);
Right Here*** rs = (OracleResultSet) pstmt.executeQuery();
rs.next();

Thanks for your help.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
jboss probably wraps the oracle ResultSet, so the cast fails. Based on the documenation it looks like you can get the underlying oracle resultset, however unless you need to use some oracle extension then I would avoid that.

http://docs.jboss.org/jbossas/javadoc/3.2.7/connector/org/jboss/resource/adapter/jdbc/WrappedResultSet.html#getUnderlyingResultSet()


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Thank you, I am confused why my other apps that are running on jboss with the same code work and this one does not?
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Based on your response, I tried the following:
conn = easDBUtil.getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(easDBConstants.UPDATE_DRAWING);
pstmt.setInt(1,pkey);
wrs = pstmt.executeQuery();
System.out.println("Wrapped ResultSet class:" + wrs.getClass().getName());
ResultSet urs =
((org.jboss.resource.adapter.jdbc.WrappedResultSet) wrs)
.getUnderlyingResultSet();
System.out.println("Underlying ResultSet class:" + urs.getClass().getName());
OracleResultSet rs = (OracleResultSet) urs;
System.out.println ("Oracle ResultSet class:" + rs.getClass().getName());
rs.next();



I get the following error:
java.lang.ClassCastException: oracle.jdbc.driver.OracleResultSetImpl


on this line:
wrs = pstmt.executeQuery();
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
what is the data type of wrs? please post the full code...
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Thank you very much. I am past that line, I had it defined wrong from some prior attempts. Here is the code now:

public void updateDrawing(BufferedInputStream bis) throws SQLException, EASException{
EASDBUtil easDBUtil = new EASDBUtil(logger);
Connection con = null;
int BUFFER_SIZE = 1024*2;
byte[] buffer = new byte[BUFFER_SIZE];//byte buffer 1024
byte[] tempBuffer = new byte[BUFFER_SIZE];//byte buffer 1024
int bytesRead = 0;
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean badRecord = false;
int counter=0;
int totalBytesRead = 0;
OutputStream outbb = null;
GZIPOutputStream out = null;
// OracleResultSet rs = null;
ResultSet wrs = null;
int pkey = 0;

try {
//insert the empty blob so the record can be updated
con = easDBUtil.getConnection();
pstmt = con.prepareStatement(EASDBConstants.UPDATE_DRAWING);
pstmt.setInt(1,pkey);
wrs = pstmt.executeQuery();
System.out.println("Wrapped ResultSet class:" + wrs.getClass().getName());
ResultSet urs =
((org.jboss.resource.adapter.jdbc.WrappedResultSet) wrs)
.getUnderlyingResultSet();
System.out.println("Underlying ResultSet class:" + urs.getClass().getName());
rs = (OracleResultSet) urs;
System.out.println("Oracle ResultSet class:" + rs.getClass().getName());
rs.next();
oracle.sql.BLOB blob=((oracle.jdbc.driver.OracleResultSet)rs).getBLOB(1);
outbb=blob.getBinaryOutputStream();
out = new GZIPOutputStream(outbb);

// Simple read/write loop.
while((bytesRead = bis.read(buffer)) != -1) {
counter=counter+1;
out.write(buffer, 0, bytesRead);
totalBytesRead = totalBytesRead + bytesRead;
out.flush();
}

}catch(EASException ce){
logger.error("Exception in updateDrawing", ce);
throw ce;
}catch(Exception e){
logger.error("Exception in updateDrawing", e);
throw new EASException("EAS_EXEC_001");
}finally{
easDBUtil.closeDBResources(con, pstmt, null, rs);
}

}


It fails on:
rs = (OracleResultSet) urs;


The log shows:
16:06:09,560 INFO [STDOUT] Wrapped ResultSet classrg.jboss.resource.adapter.jdbc.WrappedResultSet
16:06:11,107 INFO [STDOUT] Underlying ResultSet classracle.jdbc.driver.OracleResultSetImpl
16:07:52,002 INFO [STDOUT] 16:07:52,002 ERROR [EASAttachDrawingDAO] Exception in updateDrawing
java.lang.ClassCastException: oracle.jdbc.driver.OracleResultSetImpl
at com.mud.eas.dao.EASAttachDrawingDAO.updateDrawing(EASAttachDrawingDAO.java:141)
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
You are casting your result set as an OracleResultSet to a variable that is a ResultSet and so you are getting a cast error. Also, you should break that method up into simpler more atomic methods. For example put them in your easDBUtil class. For example easDBUtil.getOracleResultSet(...)

ResultSet rs = null;
rs = (OracleResultSet) urs;
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
I added all the extra stuff because of the jdbc wrapper I was trying to get the underlying resultset. Here is my original code that I have copied from my other application and the code works on jboss on the other application.

public void updateDrawing(BufferedInputStream bis) throws SQLException, EASException{
EASDBUtil easDBUtil = new EASDBUtil(logger);
Connection con = null;
int BUFFER_SIZE = 1024*2;
byte[] buffer = new byte[BUFFER_SIZE];//byte buffer 1024
byte[] tempBuffer = new byte[BUFFER_SIZE];//byte buffer 1024
int bytesRead = 0;
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean badRecord = false;
int counter=0;
int totalBytesRead = 0;
OutputStream outbb = null;
GZIPOutputStream out = null;
// OracleResultSet rs = null;
ResultSet wrs = null;
int pkey = 0;

try {
//insert the empty blob so the record can be updated
con = easDBUtil.getConnection();
pstmt = con.prepareStatement(EASDBConstants.UPDATE_DRAWING);
pstmt.setInt(1,pkey);
rs = pstmt.executeQuery();
rs.next();
oracle.sql.BLOB blob=((oracle.jdbc.driver.OracleResultSet)rs).getBLOB(1);
outbb=blob.getBinaryOutputStream();
out = new GZIPOutputStream(outbb);

// Simple read/write loop.
while((bytesRead = bis.read(buffer)) != -1) {
counter=counter+1;
out.write(buffer, 0, bytesRead);
totalBytesRead = totalBytesRead + bytesRead;
out.flush();
}

}catch(EASException ce){
logger.error("Exception in updateDrawing", ce);
throw ce;
}catch(Exception e){
logger.error("Exception in updateDrawing", e);
throw new EASException("EAS_EXEC_001");
}finally{
easDBUtil.closeDBResources(con, pstmt, null, rs);
}

}


It fails on this line:
oracle.sql.BLOB blob=((oracle.jdbc.driver.OracleResultSet)rs).getBLOB(1);


With this error:
09:03:12,765 INFO [STDOUT] 09:03:12,359 ERROR [EASAttachDrawingDAO] Exception in updateDrawing
java.lang.ClassCastException: org.jboss.resource.adapter.jdbc.WrappedResultSet


I have been stuck on this issue for a few days and I am very confused as to why it would be working differently from on app to another.

Thank you for your help.
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Just in case anyone is as confused as me. This application, unlike the others, is a web application and is picking up jboss's implemenation of the resultSet org.jboss.resource.adapter.jdbc.WrappedResultSet rather than my datasource Implementation(Oracle's oracle.jdbc.driver.OracleResultSet).

I am not sure if there is a way for it to know to use the Oracle implementation? This could be a post for jboss?
Aks kr
Greenhorn

Joined: Dec 14, 2011
Posts: 1
@Katie Doody
Did you get any alternate solution ?? i am stuck with the same problem.
 
jQuery in Action, 2nd edition
 
subject: ClassCastException when casting my resultset to OracleResultSet