Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting a java.lang.String to CLOB

923 views
Skip to first unread message

mareed...@rediffmail.com

unread,
Dec 7, 2007, 6:32:48 AM12/7/07
to
Hi,<br />
i want to convert java.lang.String to oracle.sql.CLOB object and need to insert <br />
it into my Oracle Database.<br />
I am getting ClassCast Exception at the time of creation of CLOB<br />
Exception :com.ibm.ws.rsadapter.jdbc.WSJdbcConnection<br />
<br />
here i get Connection con from jndi.<br />
<br />
oracle.sql.CLOB newClob = oracle.sql.CLOB.createTemporary(con, false, oracle.sql.CLOB.DURATION_CALL);<br />
<br />
can any one please help me in this regard<br />
<br />
java: 1.4<br />
oracle: 9i<br />
Websphere 5.3v

Ken Hygh

unread,
Dec 7, 2007, 7:52:07 AM12/7/07
to
mareed...@rediffmail.com wrote:
> Hi,

> i want to convert java.lang.String to oracle.sql.CLOB object and need to insert
> it into my Oracle Database.
> I am getting ClassCast Exception at the time of creation of CLOB
> Exception :com.ibm.ws.rsadapter.jdbc.WSJdbcConnection

>
> here i get Connection con from jndi.
>
> oracle.sql.CLOB newClob = oracle.sql.CLOB.createTemporary(con, false, oracle.sql.CLOB.DURATION_CALL);
>
> can any one please help me in this regard
>
> java: 1.4
> oracle: 9i
> Websphere 5.3v
>
You're not showing us any WebSphere code. What WebSphere code fails?
That line of code doesn't get a connection.
Ken

berm...@us.ibm.com

unread,
Dec 7, 2007, 5:42:38 PM12/7/07
to
I have the same problem. <br />
It happen any time that you try to create an oracle temporary CLOB or BLOB.<br />
The oracle method is defined as BLOB.createTemporary(Connection con, boolean cache, in duration)<br />
<br />
Inside this method Oracle's JDBC driver casts the connection object to OracleConnection. This works fine when ever you don't use WAS datasource with connection pool.<br />
<br />
As soon as WAS datasoruce is use the cast becomes a problem because the WAS datasource returns a WAS specific connection wrapper WSjdbcConnection. <br />
<br />
The following code cab be added to a simple servlet to show the failure. <br />
boolean runningInWAS = true;<br />
if( runningInWAS ){<br />
Context context = null;<br />
try { <br />
context = new InitialContext(); <br />
} catch(NamingException e) {<br />
throw new ServletException("Could not get Initial Context");<br />
}<br />
<br />
// datasource lookup<br />
try<br />
{<br />
DataSource datasource = <br />
(DataSource)context.lookup("java:comp/env/jdbc/WASDatasource");<br />
<br />
Connection conn = datasource.getConnection();<br />
//The following line trows and error when running in WebSphere env. <br />
BLOB sBlob = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);<br />
conn.close();<br />
} catch(SQLException sql){<br />
<br />
} catch(NamingException e) {<br />
log.error("JNDI lookup failed for datasource for RM database: " <br />
+ RMConstants.JNDI_DATASOURCE_RMDB, e); <br />
throw new ServletException("Could not get connection to Resource Manager datasource: "<br />
+ RMConstants.JNDI_DATASOURCE_RMDB);<br />
}<br />
}else{ // running stand alone, is OK <br />
try{<br />
Class cObj = Class.forName("oracle.jdbc.pool.OracleDataSource");<br />
DataSource datasource = = (DataSource) cObj.newInstance();<br />
((oracle.jdbc.pool.OracleDataSource) datasource).setServerName("host.svl.ibm.com");<br />
((oracle.jdbc.pool.OracleDataSource) datasource).setDriverType("thin");<br />
((oracle.jdbc.pool.OracleDataSource) datasource).setDatabaseName("myDB");<br />
((oracle.jdbc.pool.OracleDataSource) datasource).setPortNumber(1521);<br />
((oracle.jdbc.pool.OracleDataSource) datasource).setUser("scott");<br />
((oracle.jdbc.pool.OracleDataSource) datasource).setPassword("tigger");<br />
<br />
BLOB sBlob = BLOB.createTemporary(datasource.getConnection(), true, BLOB.DURATION_SESSION);<br />
} catch(SQLException sql){<br />
}<br />
}<br />
<br />
I have seen a suggestion to use WSjdbcUtil.getNativeConnection(), but that has the problem I need to include WAS jar files in our build environment an it does not work when we deploy on other Application servers. <br />
<br />
Can any one provide a better workaround to this problem?

berm...@us.ibm.com

unread,
Dec 7, 2007, 8:03:20 PM12/7/07
to
I found information about calling the PL/SQL function directly it works fine. <br />
<br />
This code works fine for me. NOTE: type has to be oracle.jdbc.OracleTypes.BLOB or <br />
oracle.jdbc.OracleTypes.CLOB <br />
<br />
private Object createTemporaryLOB(Connection conn, int type)<br />
throws SQLException<br />
{<br />
Object lob = null;<br />
CallableStatement createTemporaryStmt = conn.prepareCall("{ call DBMS_LOB.CREATETEMPORARY(?,TRUE)}");<br />
createTemporaryStmt.registerOutParameter(1, type);<br />
createTemporaryStmt.execute();<br />
lob = createTemporaryStmt.getObject(1);<br />
if( lob==null){<br />
log.error("Fail to create a temporary lob");<br />
}<br />
return lob;<br />
}

ashokk...@rediffmail.com

unread,
Jul 31, 2008, 2:24:27 AM7/31/08
to
oracle.sql.CLOB lob = null;

CallableStatement createTemporaryStmt = null;


createTemporaryStmt = conn.prepareCall("{ call DBMS_LOB.CREATETEMPORARY(?,TRUE)}");

createTemporaryStmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CLOB);

createTemporaryStmt.execute();


Object obj = createTemporaryStmt.getObject(1);

System.out.println("obj = "+obj);

lob = (oracle.sql.CLOB)obj;// throws class cast exception


The above code throws java.lang.ClassCastException at lob = (oracle.sql.CLOB)obj in jboss but it works perfectly in orion. But when trying to print obj it shows

obj = oracle.sql.CLOB@fe676b so why is it not able to type cast in jboss. pls help

Ken Hygh

unread,
Jul 31, 2008, 5:09:22 AM7/31/08
to
You might ask in a jboss newsgroup instead of a competing appserver's
newsgroup :-)
Ken

sen....@valuecentric.com

unread,
Sep 26, 2013, 9:58:47 AM9/26/13
to
0 new messages