Hi,
I just wondered if there is a way of stopping an exception being raised
when my SQL query returns an empty result set?
I am using an ODBC connection to a MySQL database.
My code for using the result set is shown below:
ResultSet rs = st.executeQuery(DateOD);
//Only one row returned no need for looping
if (rs.next()) {
//if the result set is not empty return the date
java.sql.Date DOD = rs.getDate(1);
return DOD;
}
//date chosen to indicate failure - could not work out how to return
null
else {
System.out.println("fake date:"+java.sql.Date.valueOf("?-?-?"));
return java.sql.Date.valueOf("?-?-?");
}
}
catch(Exception ex)
{System.out.println("Problem executing query "+ex+" ");
//date chosen to indicate failure - could not work out how to return
null
return java.sql.Date.valueOf("2200-01-01");
}
This code generates an exception:
Problem executing query java.lang.NumberFormatException: ?
While I am asking database questions. Is there a way of returning a
java.sql.Date null value? So far I have been substituting an incorrect date
as a substitute for a null.
Thanks in advance.
Paul
andreas - 24 Sep 2004 13:15 GMT
> This code generates an exception:
> Problem executing query java.lang.NumberFormatException: ?
why don't you want the exception to be raised in case of "exceptional
behaviour"?
> While I am asking database questions. Is there a way of returning a
> java.sql.Date null value? So far I have been substituting an incorrect
> date as a substitute for a null.
what about "return null" ?
andreas
kaeli - 24 Sep 2004 14:26 GMT
> Hi,
> I just wondered if there is a way of stopping an exception being raised
[quoted text clipped - 5 lines]
> //Only one row returned no need for looping
> if (rs.next()) {
if (rs != null && rs.next())
is how I always test, JIC. No exception then.

Signature
--
~kaeli~
Shotgun wedding: A case of wife or death.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
nioTo - 24 Sep 2004 15:50 GMT
Le 24/09/2004 15:26, kaeli a écrit :
>>Hi,
>> I just wondered if there is a way of stopping an exception being raised
[quoted text clipped - 9 lines]
>
> is how I always test, JIC. No exception then.
the test rs != null is not necessary:
http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#executeQuery(jav
a.lang.String)
Returns:
a ResultSet object that contains the data produced by the given query; never null
^^^^^^^^^^^
nioTo
kaeli - 24 Sep 2004 19:13 GMT
> > if (rs != null && rs.next())
> >
[quoted text clipped - 6 lines]
> a ResultSet object that contains the data produced by the given query; never null
>
I wonder why it stopped those nasty exceptions, then.
Hrm.
How would you test for an empty resultSet before executing next()?
I have the same problem.

Signature
--
~kaeli~
Practice safe eating - always use condiments.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
Joe Weinstein - 24 Sep 2004 23:36 GMT
>>>if (rs != null && rs.next())
>>>
[quoted text clipped - 12 lines]
> How would you test for an empty resultSet before executing next()?
> I have the same problem.
executeQuery() must return a result set. That's the spec. An *empty* result set is
a real result set nevertheless. It signifies it's being empty by returning false from the first
call to next().
If executeQuery() is done with a non-query (such as an update) etc, most drivers will
throw an exception. Some ones will actually return a totally bogus result set (oracle thin),
but I have never seen a driver so bad/non-compliant that it would return a null from
executeQuery().
In fact there are many valid cases where a query will purposely or expectedly have
zero rows at any given time, but regardless, the user wants/needs a result set, from which
they can call getMetaData() to find out what sort of columns the query would return. Eg:
select * from sometable where 1 = 0. This will still send the metadata that will tell me the
number of columns in the table and their types etc.
Joe Weinstein at BEA
kaeli - 27 Sep 2004 14:07 GMT
> executeQuery() must return a result set. That's the spec. An *empty* result set is
> a real result set nevertheless. It signifies it's being empty by returning false from the first
> call to next().
> If executeQuery() is done with a non-query (such as an update) etc, most drivers will
> throw an exception. Some ones will actually return a totally bogus result set (oracle thin),
I use that driver.
Maybe the driver sucks.
Thanks for the tips.

Signature
--
~kaeli~
I can't sleep.
The clowns might eat me.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
Gerard Krupa - 24 Sep 2004 15:17 GMT
> Hi,
> System.out.println("fake date:"+java.sql.Date.valueOf("?-?-?"));
> return java.sql.Date.valueOf("?-?-?");
>
> This code generates an exception:
> Problem executing query java.lang.NumberFormatException: ?
The exception you describe is caused by passing an invalid string into
Date.valueOf(). The method requires a valid numeric date to be passed
in and not '?' placeholders.
HTH
Gerard
paul.foreman - 25 Sep 2004 21:40 GMT
>> return java.sql.Date.valueOf("?-?-?");
> The exception you describe is caused by passing an invalid string into
> Date.valueOf(). The method requires a valid numeric date to be passed in
> and not '?' placeholders.
Gerard I agree and understand now why the exception was raised.
However I am still trying to provide a return from my method when the
resultset has been returned empty (no date was found which satisfied my
select query).
I have tried:
return java.sql.Date.valueOf(null);
and
return java.sql.Date.valueOf("");
the only return that does not raise an exception is:
return java.sql.Date.valueOf("2200-01-01");
Of course I then have to code around this in the receiving method - there
must be a better way?
Regards
Paul
Alex Potter - 25 Sep 2004 22:48 GMT
>>> return java.sql.Date.valueOf("?-?-?");
>> The exception you describe is caused by passing an invalid string into
[quoted text clipped - 20 lines]
>
> Paul
What's wrong with something like the following?
public Date foo(String sql) throws SQLException{
Date date = null; // return null if empty ResultSet
// get a connection from somewhere
Connection con = somewhere.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
date = rs.getDate(column);
}
return date;
}
Just check for a null return in the caller.

Signature
Regards
Alex
The email address above is a spam-trap.
Use alexp@ the same domain to reply via email.
paul.foreman - 26 Sep 2004 20:43 GMT
> What's wrong with something like the following?
>
[quoted text clipped - 14 lines]
>
> Just check for a null return in the caller.
Alex,
Thanks for your suggestion.
It looks like it should work, the technique seems to be to return null and
not to attempt to load a null value into a Date.valueOf() function.
I did have trouble testing for a null in the calling method, but andreas has
kindly helped me out with this.
Thanks again.
Paul