cancel
Showing results for 
Search instead for 
Did you mean: 

ClassCastException oracle.sql.CLOB

Former Member
0 Kudos

Hi,

i am having problems to write CLOBs to an oracle database using a datasource 1.x with vendor sql. i have read the thread:

Here is my coding:

con.setAutoCommit(false);

StringBuffer sql = new StringBuffer("SELECT ACTIONS FROM "getInstanceTable()" ");

sql.append("WHERE PID = " + DBTypeHelper.makeString( ((GlobalProcessInformation)gd.getParent()).getPid()) + " ");

sql.append("AND TSTAMP = " + gdi.getTstamp().longValue() + " ");

sql.append("AND OBJECTTYPE = " + DBTypeHelper.makeString( gdi.getObjectType()) + " ");

sql.append("AND MESSAGEID = " + gdi.getMessageId().longValue() + " ");

sql.append("FOR UPDATE");

System.out.println(sql);

Statement stmt = con.createStatement();

ResultSet lobDetails = stmt.executeQuery( sql.toString() );

if(lobDetails.next())

{

// Get the Clob locator and open an output stream for the Clob

Clob sugBookClob = lobDetails.getClob(1);

System.out.println("CLOB: " + sugBookClob);

System.out.println("CLOB (instanceof CLOB): " + (sugBookClob instanceof oracle.sql.CLOB));

System.out.println("CLOB cl: " + sugBookClob.getClass().getClassLoader());

System.out.println("CLOB direct cl: " + oracle.sql.CLOB.class.getClassLoader());

Writer clobWriter =

((oracle.sql.CLOB)sugBookClob)

.getCharacterOutputStream();

// CLOB clob = CLOB.createTemporary(con, true, CLOB.DURATION_SESSION); // Throws ClassCastException

// clob.open(CLOB.MODE_READWRITE);

//

// Writer clobWriter = clob.getCharacterOutputStream();

// Buffer to hold chunks of data to being written to the Clob.

char[] cbuffer = new char[1024];

// Read a chunk of data from the sample file input stream,

// and write the chunk into the Clob column output stream.

// Repeat till file has been fully read.

int nread = 0;

while( (nread= sugFileReader.read(cbuffer)) != -1 )

clobWriter.write( cbuffer, 0, nread); // Write to Clob

clobWriter.flush();

clobWriter.close();

}

con.commit();

I am facing the problem, that sugBookClob is an oracle.sql.CLOB that should be easily casted into oracle.sql.CLOB, but instanceof is false. This is because these classes have been loaded by different classloaders.

I get my Connection from a datasource (and also the sugBookCob), for the cast i need to add oracle.sql.CLOB to my component that is used within a servlet.

Any ideas how i can succeed with this?

Best regards,

Christian

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

If I may say so;

Please change the way you create your sql statememt and use the prepared statement. It is much safer...

Appending the parameter information in text to a statement leaves you wide open to an injection attack.

As for the ClassCastException ...

First move to an Object obj.

Use obj.getClass().getName(); to display the name of the class really being returned. You can then find out whether you can work with that or need to change your DataSource jar files.

Enjoy

Former Member
0 Kudos

Hi F.J.Brandelik

thanks for your answer! I think you right with the prepared statement, but that is not my problem. The problem is the ClassCastException and yes i am getting an oracle.sql.CLOB as i expected. I also searched for the driver of the standard schema and it´s also the expected one.

Searching the net i found that some guys had similar problems on tomcat with oracle. They managed to get an OracleConnection from the DataSource. But unfortunately i don´t how to get to that.

Also i tried the OPENSQL approach, but after being confident inserting CLOb into the standrad db schema, OPENSQL is not supported if we create another db schema. And that is the way our administration forces us to develop.

Thanks anyay!

Best regards,

Christian

Former Member
0 Kudos

Hi,

I have the same problem. I am using WebLogic 8.1 Server and Oracle Database.

My code is like this..

java.sql.Clob clob = rs.getClob("waiver");

java.io.OutputStream os =

((weblogic.jdbc.common.OracleClob) clob).getAsciiOutputStream();

byte[] b = data.getBytes("ASCII");

os.write(b);

os.flush();

It is throwing me ClassCastException. How did you solve this. I even used typecasting to oracle.sql.OracleClob.

Is this depending on the type of driver being used as well..I am using weblogic.jdbc.oci.Driver.

Please share your solution.

Thanks and Regards

Former Member
0 Kudos

Hi,

i am sorry, not having good news for you, but for me as i already told the only way was to get around LOBs.

You might be a little more lucky. Is it possible for you to get the physical Connection (OracleConnection) from the ManagedConnectionFactory WebLogic. There is an example on the net for TOMCAT and Oracle using DBCP! Search for that for information.

Is this depending on the type of driver being used as well..I am using weblogic.jdbc.oci.Driver.

--> No i have used the original oracle drivers and there is also a ClassCastException!

I found out, that oracle CLOB can be filled with set standard setString() method(JDBC) up to 4000 characters!

I hope you get anywhere with this

Christian