Hello, I need your help, please...
I have a following code in my application:
PreparedStatement pstmt = con.prepareStatement(
"SELECT * FROM USERS WHERE LOGIN = ?");
pstmt.setRef(1, login);
ResultSet rs2 = pstmt.executeQuery();
I want to get all rows from the table MEETINGS for certain user whos
got an unique login. But for some users (logins) there are no meetings
(no rows) in the table, so in this case ResultSet should give a "null"
value in return.
My question is how to check if ResultSet has null value?
Thank you in advance,
Paulina
Daniel Dittmar - 10 Feb 2005 10:58 GMT
> I have a following code in my application:
> PreparedStatement pstmt = con.prepareStatement(
[quoted text clipped - 6 lines]
> value in return.
> My question is how to check if ResultSet has null value?
I'm not quite sure that I understand what you want. I'm assuming you
have tables USERS and MEETINGS that can be joined through a column LOGIN:
select USERS.LOGIN, MEETINGS.MEETINGID from USERS left outer join
MEETINGS on USERS.LOGIN = MEETINGS.LOGIN
where USERS.LOGIN = ?
The syntax for the left outer join could be different for your database.
If there are no entries in MEETINGS for a specific user, you will
receive a single row containing the username and a NULL value for the
MEETINGID.
Hint: USERS.LOGIN is not necessary in the output list as you specify
that user in the where condition. But if you test the statement in some
query tool, it is quite useful if you make changes to the where condition.
Daniel
dar7yl - 11 Feb 2005 02:53 GMT
> Hello, I need your help, please...
>
[quoted text clipped - 8 lines]
> value in return.
> My question is how to check if ResultSet has null value?
I don't think you have to worry about a null value being
returned from executeQuery(). If it does, that means that
something drastic has happened, and the exception
mechanism should take care of it.
Perhaps you meant to ask:
"Does the ResultSet return a NULL SET,
and how do I test for it?"
The simplest way is to use one of the ResultSet accessor functions
( see http://java.sun.com/j2se/1.4.2/docs/api/index.html )
such as first() which returns a boolean
true if the cursor is on a valid row;
false if there are no rows in the result set
The conventional method for traversing the resultset
automatically takes care of the null set condition:
<code>
while ( rs.next() )
{
// do something
}
</code>
regards,
Dar7yl