GeeCON Prague 2014*
The moose likes JDBC and the fly likes What is the difference between executeUpdate and execute? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "What is the difference between executeUpdate and execute?" Watch "What is the difference between executeUpdate and execute?" New topic
Author

What is the difference between executeUpdate and execute?

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
What is the difference between executeUpdate and execute?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
What is the difference between executeUpdate and execute?

Well, execute() can be used to execute any kind of an SQL statement (i.e. SELECT, UPDATE, INSERT, DELETE) while executeUpdate() can only be used with UPDATE, INSERT or DELETE.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
What is the return type of execute ?
What is the return type of executeUpdate?
If I am deleting records from two table which have primary-foreign key relationship,then if something happened to the first query ,how can I retrieve message and tell the user that some error(primary key violation) occured during deletion ? Cn I use return type of executeUpdate ?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
What is the return type of execute?
What is the return type of executeUpdate?

execute() returns a boolean indicating simple success/failure for the operation you tried to execute. executeUpdate() returns the number of successful record updates (e.g. "2" for "2 rows were deleted").
If I am deleting records from two table which have primary-foreign key relationship,then if something happened to the first query ,how can I retrieve message and tell the user that some error(primary key violation) occured during deletion ? Cn I use return type of executeUpdate ?

No, you can't use the return type of executeUpdate() to tell any specifics of the failure because the return type indicates only successful updates. Errors such as a primary key violation are indicated by the JDBC driver via an SQLException. You can get the database specific error code with SQLException.getErrorCode().
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is the difference between executeUpdate and execute?