Solved

java.sql.SQLException: java.lang.ClassCastException

Posted on 2006-10-26
11
1,180 Views
Last Modified: 2012-08-13
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?
0
Comment
Question by:java_kevin
  • 5
  • 4
11 Comments
 
LVL 14

Expert Comment

by:hoomanv
Comment Utility
Please post the whole stack trace
ex.printStackTrace()
0
 
LVL 9

Expert Comment

by:owenli27
Comment Utility
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();
0
 

Author Comment

by:java_kevin
Comment Utility
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.
0
 
LVL 9

Accepted Solution

by:
owenli27 earned 125 total points
Comment Utility
Hi Kevin,

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

Owen
0
 

Author Comment

by:java_kevin
Comment Utility
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.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:java_kevin
Comment Utility
Now I'm pending to check with my DB team on it...
0
 
LVL 9

Expert Comment

by:owenli27
Comment Utility
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
0
 

Author Comment

by:java_kevin
Comment Utility
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..
0
 

Author Comment

by:java_kevin
Comment Utility
Hi owen,

But shouldn't jndi be unique for each connection?
0
 
LVL 9

Expert Comment

by:owenli27
Comment Utility
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
changeXy challenge 13 56
Eclipse IDE - Cannot copy/paste from console output 8 58
bitbucket vs gitbucket 3 29
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now