Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

very strange

 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just curious but, what driver are you using (ODBC right?) and
what is your DB.
regds.
- satya
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will do that as I am very curious about it. I probably should do that as a general principle.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic