This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
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

What is the difference between executeUpdate and execute?

 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between executeUpdate and execute?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic