Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java.sql.SQLException: java.lang.ClassCastException

Posted on 2006-10-26
11
Medium Priority
?
1,206 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Accepted Solution

by:
owenli27 earned 500 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
 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

926 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