Forums Register Login

How to check record exists

+Pie Number of slices to send: Send
Hi all,
How to check whether resultset is retrieving records or not?.I tried
by giving
if(rs!=null) or if(rs.next())

Still it goes into the loop and giving invalid cursor state when there are no records.

Help me please
+Pie Number of slices to send: Send
Hi,
I would think that
if(rs != null){
// code block
}
would send you into the code block ... isnt there always a resultset returned even if there are no records.
but i would also think that
if (rs.next()){
// code block
}
SHOULD not let you into the code block if you have no results.
+Pie Number of slices to send: Send
Usually, I want to track the number of records found, so I use:

If I only want to check if a record exists, or expect only one row back:

there are many other ways as well, but these two should get you started.
Jamie
+Pie Number of slices to send: Send
also, note that

will never be true. The executeQuery() method always return a ResultSet object or throws a SQLException which will cause it to immediately jump to the exception block of code anyways.
Jamie
+Pie Number of slices to send: Send
I tried rs.next() but still goes into code block and giving invalid cursor state.
+Pie Number of slices to send: Send
If you could please show us the entire code block and not just one line, then we might be able to help better.
Keep in mind that if you are still doing this:

Because of the (rs != null) part, it will drop into the block because, as Jamie already mentioned, executeQuery will always return a ResultSet object or throw an exception.
+Pie Number of slices to send: Send
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs =st.executeQuery("union query here"); //which retrieves more than one record
if (rs.next()==true)
{

rs.beforeFirst();
while(rs.next())
{
....
}

}
+Pie Number of slices to send: Send
 

Originally posted by Vasudevan Gopalan:
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs =st.executeQuery("union query here"); //which retrieves more than one record
if (rs.next()==true)
{

rs.beforeFirst();
while(rs.next())
{
....
}

}


What if you don't use the IF statement at all? What if you just use the
while(rs.next())
{
}
Unless you have an ELSE clause with your IF, there is no need for the IF.
+Pie Number of slices to send: Send
Also, in your while block, do not reuse the same statement that you created your resultset with or the resultset will also be closed automatically.
eg.

also, please use the code tags to keep your code readable to others!
Jamie
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6564 times.
Similar Threads
Fetchsize
Locking of all records in db.db
Time Zone problem
result set exhausted
How does one insert a column value into an existing record?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:10:03.