This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes lack of exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "lack of exception" Watch "lack of exception" New topic
Author

lack of exception

tomasz brymora
Greenhorn

Joined: May 31, 2004
Posts: 22
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
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:


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
tomasz brymora
Greenhorn

Joined: May 31, 2004
Posts: 22
... 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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Tomasz,
Thanks! Don't be a stranger.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: lack of exception
 
Similar Threads
I need help with Hibernate
org.hibernate.MappingException: table refers to an unmapped class
Connection Pooling & ResultSet behavior
i need help on java database arithemetic
Display same Data in JSP after reloading it