• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PreparedStatement not throwing error when it should

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic