This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes PreparedStatement not throwing error when it should Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PreparedStatement not throwing error when it should" Watch "PreparedStatement not throwing error when it should" New topic
Author

PreparedStatement not throwing error when it should

Micky Johnson
Greenhorn

Joined: Sep 29, 2010
Posts: 3
Given the following code...



...and assuming I have the table structure...



....I would expect to get an error because I am trying to insert a record into a table without a value for LastLogin. However I do not get an error and therefore experience unexpected behavior in my application. The same SQL executed in a Query Analyzer returns an error.

Can anyone explain why this is not working as expected?

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

How is it working as not expected, then? Does it insert a record? If so then what is the value of the LastLogin column of that record?
Micky Johnson
Greenhorn

Joined: Sep 29, 2010
Posts: 3
No record is inserted into the UserSession table. I stepped through the code and after "prepStmt.execute();" the debugger skips directly to the "finally" block. Given that I know a SQL error has occurred it should have been caught in the "exception" block but for some reason it is not.
Micky Johnson
Greenhorn

Joined: Sep 29, 2010
Posts: 3
It seems that if you have multiple SQL statements you should use a PreparedStatement for each one instead of trying to bundle them all into one PreparedStatement (or Statement for that matter). If you do this and use transactions you should be all set. To use transactions within the Java code set autoCommit(false) on the Connection instance and then commit or rollback depending on the outcome of the execution of the PreparedStatements (don't forget to set autoCommit(true) when you are finished).

See http://download.oracle.com/javase/tutorial/jdbc/basics/transactions.html for details.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PreparedStatement not throwing error when it should
 
Similar Threads
How to use two query in one servlet
EJB unable to find my Home interface
Redeploying an app
cannot execute insert statement two times in WSAD
jboss - mysql Table not found in statement