PreparedStatement not throwing error when it should
Micky Johnson
Greenhorn
Joined: Sep 29, 2010
Posts: 3
posted
0
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?
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
posted
0
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
posted
0
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).