wood burning stoves*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 18120
    
    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
cannot execute insert statement two times in WSAD
Redeploying an app
How to use two query in one servlet
jboss - mysql Table not found in statement
EJB unable to find my Home interface