aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 29219
    
135

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: 29219
    
135

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