It's not a secret anymore!*
The moose likes Servlets and the fly likes very strange Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "very strange" Watch "very strange" New topic
Author

very strange

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I am getting an error from my servlet.
problems connecting to jdbc dbc:MagicShop
However the information ends up in the database anyway
Here is the relevant code:

If the attempt at getting a connection failed, how did the database get updated?


SCJP
Visit my download page
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Just curious but, what driver are you using (ODBC right?) and
what is your DB.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

im using JDBC/ODBC
My database is in Access.
Im not sure if I have had this error message all along or not, but I think it is new. I dont look at the dos window often.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Hello Randall,
I don't think the error you are getting is a connection error. I think one of the method calls
st = con.createStatement();
or
stmt = (PreparedStatement)con.createStatement();
is throwing the error.
Change your catch statement to

Let us know what error is getting thrown!
------------------
Hope This Helps
Carl Trusiak


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I will do that as I am very curious about it. I probably should do that as a general principle.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

It says the exception is from this line:
stmt = (PreparedStatement)con.createStatement();
The API says createStatement() throws SQL exception.
PreparedStatement is a subclass of Statement, which is what the method returns, so it cant be ClassCastException.
the same questions remains. How is it updating the database?
And why is it throwing an exception?
[This message has been edited by Randall Twede (edited March 07, 2001).]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
stmt = (PreparedStatement)con.createStatement();
createStatement returns a Statement Object, PreparedStatement is a subclass of Statement. It's illegal to cast a super class Object to a Subclass Object. A ClassCastException isn't a checked Exception. You won't find it listed by any method but can occur on any cast operation if you don't follow the rules.
The query is succeeding because later at
stmt = con.prepareStatement("INSERT INTO sales ([Date], Customer) values (?, ?)");
You are properly creating a PreparedStatement Object. There isn't any need in your code for the line
stmt = (PreparedStatement)con.createStatement();

------------------
Hope This Helps
Carl Trusiak
[This message has been edited by Carl Trusiak (edited March 07, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Thanks Carl,
I see now that there is no need for that line.
I still dont see why it threw an exception though since I did follow the casting rules.

perhaps there is a problem with calling createStatement() twice?
[This message has been edited by Randall Twede (edited March 07, 2001).]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

i am not very sure, but Access I don't think supports
Prepared Stmts. I know it doesn't support Transactions.
Could someone comment on this.....
regds.
- satya
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

d'oh I am trying to cast from Statement to PreparedStatement. That is not following the rules
Access does support PreparedStatement because my code works. I will delete the bad line. Thanks again Carl.
[This message has been edited by Randall Twede (edited March 07, 2001).]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
This is probably more appropriate for BIG but, here we go. Don't confuse an Object with an Object Reference.
I have 3 classes A with a Subclass B and a Subclass C
So the Hierarchy is:

An A Refernce can be used to refence an Object of A, B or C.
B reference can be used to access a B or C object but not an A
C can only refernce a C Object.
Casting rules are Based on the Actual Object. You can always cast to a superclass. Thats because an A reference can access B or C.
However, if you have an A reference, you can only cast it to a B reference if the Object itself is actually a B or C Object. You can only cast it to a C reference if the Object is a C Object.
The reason for this is pretty reasonable. Lets say when I create B, I add an instance variable to it that isn't in A and methods to access it. If I create an A object, it doesn't have this variable. If I were allowed to change the refernce to a B reference and used the method that accesses the variable that isn't there...
This is the case in Statement and PreparedStatement. PreparedStatement has a String Object that contains the PreCompiled Sql in it. This is actually stored in the database. If you do the stmt = con.prepareStatement("INSERT INTO sales ([Date], Customer) values (?, ?)"); in the same session, you get a reference to the first PreparedStatement (or you should if the database and the Driver you are using implements it properly) Statement doesn't create this in the database, how can you access it?

Sorry, I was typing this when you put in your last post. It took me too long to enter to delete so
[This message has been edited by Carl Trusiak (edited March 07, 2001).]
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
Just for confirmation, did u load driver classes before getting connection object.
U can load driver classes using following stmt.
class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
for JDBC-ODBC Driver


SCJP 1.5, SCEA, ICED (287,484,486)
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

yes. I didnt include all the code. My brain must have not been at optimum the day I wrote that code.
An unnecessary line of code that contained an illegal cast
and it went unnoticed for months because 1)I never looked at my java window, and 2)The program worked in spite of it.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Thanks Randall. I was not very sure of this and someone
mentioned to me that Access doesn't support Prepared stmts
and Transactions. But, I never went back to verify
that stmt. Sorry abt the wrong stmt.
regds.
- satya
Geoff Tate
Ranch Hand

Joined: Feb 06, 2001
Posts: 55
Using a PreparedStatement on a DB that doesn't support prepared statements does no harm, there is just no effect when you do so. Its good to keep in there just in case you switch to a DB that supports it.


<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR> fantastic, a towel? <HR></BLOCKQUOTE>
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Now I am unsure. Do you mean it will work properly even if the db doesnt support PreparedStatement? If that is so then perhaps Access doesnt.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by Randall Twede:
Now I am unsure. Do you mean it will work properly even if the db doesnt support PreparedStatement? If that is so then perhaps Access doesnt.

I'm not sure about Access but, yes, a driver is required to implement the PreparedStatement Object if if the database doesn't. The driver stores the String and winds up reusing that String in the exact same way that Statement does. It sends the SQL String to the Database on each call and the Database is required to compile it each time and return the results.

------------------
Hope This Helps
Carl Trusiak
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

The driver stores the String and winds up reusing that String
in the exact same way that Statement does. It sends the SQL String to the Database on each
call and the Database is required to compile it each time and return the results.


Carl:
I would have to disagree with you on the abv stmt.....
Most of my knowledge is from this tutorial from Sun.
Pl. take a look when you get a chance.....
I seem to be missing something between what you said and whats'
mentioned in the tutorial.
The stmt is sent to the DBMS not the driver, as I understand.
Thanks.
- satya
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Randall/Carl:
I found this about MS Access and Prepared stmts.
It seems that Access doesn't support prepared stmts.
Check out this discussion from JDBC forum.
ps: maybe we should move to that forum, for our
own benefit.
regds.
- satya
 
jQuery in Action, 2nd edition
 
subject: very strange
 
Similar Threads
JTable with inseting and autonumbering problem
(jdbc+jtable+oracle) urgent please!!
does anyone used updateDate() ?
(jdbc+oracle) urgent please!!
Java - Mysql connectivity