This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes executeUpdate() return Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "executeUpdate() return Value" Watch "executeUpdate() return Value" New topic
Author

executeUpdate() return Value

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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,


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

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

Joined: May 26, 2003
Posts: 30789
    
157

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: executeUpdate() return Value