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

Java with JDBC: When will method execute() return False?

 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranch expert!

As far as I know, the execute() method in java.sql.Statement returns true if the result is ResultSet. Otherwise, it returns false. (right?)

So I create method deleteStudent(String name) which connect to SQL database by JDBC connection.
I use a boolean named ret to determine if the name in parameter exists in the database.

The boolean:

Then I use that boolean for conditional statement:

My problem is the ret boolean returns true even if the selectTest query return nothing.
Example is, I call the method:

the result is true
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API:
"
true if the first result is a ResultSet object; false if it is an update count or there are no results
"

Now, this is actually a little unclear, but a SELECT statement will always return a ResultSet.
That ResultSet may have no rows (which is the case you encountered), but it is still returned.
So you would need to get the ResultSet and then check next(). Which, of course, begs the question of why not simply use executeQuery().

The "or there are no results" part means a statement that has nothing to return...I can't think of one offhand.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
... begs the question of why not simply use executeQuery().

Hi Dave, it is because I need the ret boolean to check whether the record I need to delete exists in database or not
 
Campbell Ritchie
Sheriff
Posts: 51439
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always read the documentation. I think this question is too difficult for the “beginning” forum, so I shall move it.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Now, this is actually a little unclear, but a SELECT statement will always return a ResultSet.
That ResultSet may have no rows (which is the case you encountered), but it is still returned.
...
The "or there are no results" part means a statement that has nothing to return...I can't think of one offhand.

Thanks for this explanation, I have read this API but don't get it clear.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Always read the documentation. I think this question is too difficult for the “beginning” forum, so I shall move it.

Thank you, I appreciate that.
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Thompson wrote:
Hi Dave, it is because I need the ret boolean to check whether the record I need to delete exists in database or not


But that boolean doesn't mean what you think it does.
It returns true because, for any and every SELECT statement there will be a ResultSet. All SELECT statements (unless they throw an exception) will result in the boolean from execute() returning true.

In order to know if there is a record returned you need the ResultSet.

Indeed, why do you need to know the record exists?
Why not just do the DELETE.
The worst that will happen is it will delete nothing.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Indeed, why do you need to know the record exists?
Why not just do the DELETE.
The worst that will happen is it will delete nothing.

This is what will happen when the program run that deleteStudent method:

so if I don't have that boolean, I don't know another way to do the condition statement.
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The normal route is:

executeUpdate the DELETE statement for name.
Check the int that is returned from that method.
If it's > 0 you've deleted something, else you haven't.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:The normal route is:

executeUpdate the DELETE statement for name.
Check the int that is returned from that method.
If it's > 0 you've deleted something, else you haven't.

Thank you, my problem is resolved.
Your solution works for me!
 
Tushar Goel
Ranch Hand
Posts: 927
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wondering why have used 'Boolean' instead of 'boolean'.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Just wondering why have used 'Boolean' instead of 'boolean'.

It's my typo, should be 'boolean' (but both seem to provide the same result). Thanks for pointing it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic