Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

executeUpdate() return Value

 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This method returns an int which is number of rows affected by the query, otherwise will throw an exception. This is how the method behaves right? Having some doubt about any other posibilities. I'm handling a DBConnection class which encapsulate the JDBC API methods.

Thanks in advance,
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijitha Kumara:
This method returns an int which is number of rows affected by the query, otherwise will throw an exception. This is how the method behaves right?

Yes. By definition a method either returns a value (or void) or throws an exception. Do keep in mind that it might return the number zero if no rows are updated.

If you describe what scenario you are thinking of, people may give a more useful answer.
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne. I'm just checking a code some other developer has done for the DBConncetion I mentioned above. One method would be like this. Here when the connection is created it sets the autocommit to false. This method has few problems as I see. Please asume all the variables are properly defined and initialized. (stmt--> Statement).


Consider an Insert Statement (one for the master record and few records for breakdown table)

The problem I see is if the calling method has some other queries to be executed(say records for the breakdown table) after this call, if this throws an exception (which it handles itself) other queries may still get executed, which may not what the developer wants it to do. Any comments would really appretiated.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijitha Kumara:
The problem I see is if the calling method has some other queries to be executed(say records for the breakdown table) after this call, if this throws an exception (which it handles itself) other queries may still get executed, which may not what the developer wants it to do. Any comments would really appretiated.

That would depend on whether the caller did anything with the false return value. It's a bit odd to use a boolean rather than an exception though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic