Solved

java.sql.SQLException: java.lang.ClassCastException

Posted on 2006-10-26
11
1,183 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
ID: 17813043
Please post the whole stack trace
ex.printStackTrace()
0
 
LVL 9

Expert Comment

by:owenli27
ID: 17813074
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
ID: 17816457
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
ID: 17818654
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
ID: 17824742
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Expert Comment

by:owenli27
ID: 17825464
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
ID: 17827859
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
ID: 17831937
Hi owen,

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

Expert Comment

by:owenli27
ID: 17833758
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
json example 39 130
Error with Java/Cache JDBC Classpath 2 30
Java JRE greater than 1.6 5 49
xampp tool 12 21
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

948 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

20 Experts available now in Live!

Get 1:1 Help Now