Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC why to use Statement.execute() when executeUpdate() and executeQuery() are available

 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys
recently I faced an interview where the interviewer asked a strange question.It goes like this:
when I have specialized methods like executeQuery() to get resultset and executeUpdate() to perform DML operations, why do we need execute() method(which is more generic in use)?
Does execute() has any advantages over executeQuery() or executeUpdate()?
For a moment,I was stumped.Although I babbled something about circumstance where queries are dynamically generated..Dunno whether its correct or not.

can anyone tell me whether its correct or not?
 
Dave Tolls
Ranch Hand
Posts: 2109
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of guessing you could always check the API.

All three do a different thing.
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that all three of them do different thing.Although the point of the question was , in which circumstances we should use execute() and in which circumstances, executeQuery(). Thats the point where I was stumped...
 
Paul Clapham
Sheriff
Posts: 21576
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the API, which Dave already linked to, tells you what the two methods do.
 
Dave Tolls
Ranch Hand
Posts: 2109
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll even quote.

execute(String) :
"
Executes the given SQL statement, which may return multiple results.
"

executeQuery(String) :
"
Executes the given SQL statement, which returns a single ResultSet object.
"
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for being a bit stupid guys, but I needed a practical example (a real time scenario, if you will) where We will prefer using execute () instead of executeQuery().
 
Paul Clapham
Sheriff
Posts: 21576
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if your SQL statement is going to return multiple results, then you'd want to use the method which allows you to handle those multiple results. If it's only going to return one result, though, you'd want to use the method which only handles that one result.

Does that help?
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes this definitely helps.but if there will be any real life use case for this,that would have been great...
 
Paul Clapham
Sheriff
Posts: 21576
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, I use SQL which returns a result set all the time. So does everybody else who uses JDBC. The world is full of real-life use cases.

However if your question is "When would I use SQL which returns more than one result set" it would help if you asked that question, rather than the vague questions you've been asking so far. I haven't ever done that and I expect it's pretty uncommon. But there must be use cases for it otherwise the feature wouldn't be in JDBC. Right now that's good enough for me, and I would recognize the use cases if they showed up.
 
Dave Tolls
Ranch Hand
Posts: 2109
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SQLServer statement that does more than one SELECT then it will return more than one ResultSet.

That's dredging up a bit from my memory, but here's the MS Technet note on it for 2012 version.

Note this is very DB specific.
The above won't work with Oracle for example.
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul and Dave..
 
Anamika Singh
Ranch Hand
Posts: 67
IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the example Dave. I wasnt sure how to use the execute() after reading the API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic