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();
        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?
Who is Participating?
Weiping DuConnect With a Mentor Senior Web DeveloperCommented:
Hi Kevin,

I just doubt that java.lang.ClassCastException is from (DataSource)initCtx.lookup(dsJndi), which could be caused by wrong "dsJndi" value.

Please post the whole stack trace
Weiping DuSenior Web DeveloperCommented:
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();
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

java_kevinAuthor Commented:
Hi hoomanv,

the stack trace:
      java.sql.SQLException: java.lang.ClassCastException
      at weblogic.jdbc.rmi.SerialConnection.prepareStatement(
      at batchbill.LWBDataExtractor.process(
      at batchbill.BillingAgent.process(
      at batchbill.BillingAgent.main(

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.
java_kevinAuthor Commented:
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.
java_kevinAuthor Commented:
Now I'm pending to check with my DB team on it...
Weiping DuSenior Web DeveloperCommented:

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.

java_kevinAuthor Commented:
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..
java_kevinAuthor Commented:
Hi owen,

But shouldn't jndi be unique for each connection?
Weiping DuSenior Web DeveloperCommented:
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)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.