• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

lack of exception

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
author & internet detective
Posts: 41502
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41502
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomasz,
Thanks! Don't be a stranger.
 
reply
    Bookmark Topic Watch Topic
  • New Topic