• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lack of exception

 
tomasz brymora
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!
Another DB greenhorn query.
I've got this method that works fine retrieving an entry from a table, when the enry actually exists.
If I try retrieving, say "Bob" if bob doesn't exist, it doesn't throw any sort of exception. Do SQL exceptions have to be declared or caught for the program to cough up a warning when something goes wrong?

Thank you in andvance.

public void retrieveStudent(Connection c, String studentName)
throws IOException
{
// r is ResultSet defined for the entire class
this.r = null;
try{
Statement s = null;
s = c.createStatement();
this.r = s.executeQuery(
" SELECT student_ID, name, student_psw FROM Student WHERE name = '" + studentName + "'");
//s.close();
}

catch(Exception e){
System.out.println("PROBLEM WITH STUDENT: " + e);
e.printStackTrace();
}
}
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomasz,
The select statement doesn't throw an exception for no rows. It returns an empty resultset with no rows. You can test this by seeing that resultset.next() returns false.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In SQL, a query that returns no rows is not considered an error. As Jeanne suggested, you can test if a ResultSet returned no rows by calling its next() method.

But, that doesn't mean that you can't throw an exception if your application considers it to be an "error". For example, it's pretty common to do something like this:
 
tomasz brymora
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... that's what I suspected. The app didn't cough so I didn't quiet trust myself. So I handled it in a way similar to your suggestion.

This is a really wonderful forum by the way! For whatever a greenhorn's opinion is worth, much more helpful than Sun's official forum

Tomasz
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomasz,
Thanks! Don't be a stranger.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic