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.
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...
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.
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.
posted 5 years ago
SQLServer statement that does more than one SELECT then it will return more than one ResultSet.