Link to home
Start Free TrialLog in
Avatar of java_kevin
java_kevin

asked on

java.sql.SQLException: java.lang.ClassCastException

Hi guys,

Wonder if any help can be rendered.

=====================
        Connection conn;
        PreparedStatement ps;
        conn = null;
        ps = null;
        InitialContext initCtx = new InitialContext(env); // env has been initialised properly
        DataSource ds = (DataSource)initCtx.lookup(dsJndi); // dsJndi has been initialised, but not sure if properly
        conn = ds.getConnection();
        String query = "SELECT * FROM TLWB_AHP_SESSIONS WHERE (STATUS = '0' OR STATUS = '00') AND TRANSACTION_DATE LIKE ? ORDER BY SESSION_ID ASC";
        ps = conn.prepareStatement(query);
======================

Upon reaching the last line, an error was thrown:

java.sql.SQLException: java.lang.ClassCastException.

I tried to print out the value of conn.prepareStatement(query) but got the error as well. Does this error mean that prepareStatement is unable to accept "query" properly? I read that, this method throws SQLException when database access error occurs. But this error is ClassCastException. Does it have anything to do with database access? Or somehow, binding is not recognised?
Avatar of hoomanv
hoomanv
Flag of Canada image

Please post the whole stack trace
ex.printStackTrace()
print out and check "dsJndi" value before initCtx.lookup(dsJndi);
How you setup your datasource, admin did it on server or you have data-sources.xml to configure it?
The datasource location (jndiName usually like jdbc/pool/xxxDS) should be passed to lookup();
Avatar of java_kevin
java_kevin

ASKER

Hi hoomanv,

the stack trace:
========
      java.sql.SQLException: java.lang.ClassCastException
      at weblogic.jdbc.rmi.SerialConnection.prepareStatement(SerialConnection.java:195)
      at batchbill.LWBDataExtractor.process(LWBDataExtractor.java:50)
      at batchbill.BillingAgent.process(BillingAgent.java:89)
      at batchbill.BillingAgent.main(BillingAgent.java:191)
==========

Hi owenli,

Even though the dsJndi value has been set in a config file, I should try to print it out first? The datasource is set by our DB team. And the value has been verified by the DB to be correct.

And the funny thing is, when I run this program for the first time in the day, there is the error. For the 2nd run, there is not.
ASKER CERTIFIED SOLUTION
Avatar of Weiping Du
Weiping Du
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi owen,

Thanks for the advice.

I printed out debug statements and it seemed that the Datasource obtained differs each time the program is run.

When it runs fine, the DS is valueA.
When it fails, the DS is valueB. Although each and everytime it runs, the DS should stay the same.
Now I'm pending to check with my DB team on it...
Kevin

So, there could be two dataSources use same JNDI name(but they may or may not have same dataSource name). The one wrong one lead you to connect different database server but it will fail on permission problem.

Owen
Hi Owen,

You've got a point there.

But does it sound odd/funny, that sometimes, it gets the correct datasource, and another time it gets the wrong one? We are using clustered server, by the way, and the program is only migrated to one server. But this "migrate to only 1 server" method is used by other programs as well, and working fine..
Hi owen,

But shouldn't jndi be unique for each connection?
Hi Kevin

I created DataSouces connection on my WSAD server by myself.  It may be different environment from your DB team.
For me, both dsName and jndiName must be unique, but it is Container-unique.

(dsName: an identifier to name of a data source)
(jndiName: that data source should be bound to)

Owen