This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes result null is null(urgent help) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "result null is null(urgent help)" Watch "result null is null(urgent help)" New topic
Author

result null is null(urgent help)

shilpabijam
Greenhorn

Joined: Dec 17, 2001
Posts: 1
I have inserted fields in my oracle database using :
insert into table values('test','test',NULL);
when I try to retreive all the rows where the last field is NULL, if there are no rows returned ,how do I check for that condition.
I tried using if(ResultSet ==null),
tried using if (RS1.isBeforeFirst())
Nothing works,
Pls help!!!
Hrithik Roshan
Greenhorn

Joined: Dec 12, 2001
Posts: 23
Originally posted by shilpabijam:
I have inserted fields in my oracle database using :
insert into table values('test','test',NULL);
when I try to retreive all the rows where the last field is NULL, if there are no rows returned ,how do I check for that condition.
I tried using if(ResultSet ==null),
tried using if (RS1.isBeforeFirst())
Nothing works,
Pls help!!!

------------------------------------
simple
add this to ur code
if(resultset != null)
{
if(!resultset.next()) System.out.println("No rows retrieved ..");
else
{
while(resultset.next())
{
//dosomething
}
}

bye!
Hrithik Roshan
Greenhorn

Joined: Dec 12, 2001
Posts: 23
Originally posted by Hrithik Roshan:

int i = resultset.executeUpdate()
if(i > 0) System.out.println("Success...")
else
System.out.println("Error: ");

------------------
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

With regards to the code from Hrithik Roshan, the first version is wrong (it will detect if there aren't any results correctly, but if there it discards the first result!) and the second returns the number of rows inserted...
Maybe what you want is more like:

Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"shilpabijam",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp .
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in again with a new name which meets the requirements.
Thanks.
Dave
Hrithik Roshan
Greenhorn

Joined: Dec 12, 2001
Posts: 23
Originally posted by David O'Meara:
[B]With regards to the code from Hrithik Roshan, the first version is wrong (it will detect if there aren't any results correctly, but if there it discards the first result!) and the second returns the number of rows inserted...
Maybe what you want is more like:

Dave[/B]

well Dave!
i think i can do this way!
--------------------------------------------
if(resultset != null)
{
if(!resultset.next()) System.out.println("No rows retrieved");
else
{
while(resultset.next())
{
//dosomething
}
}
else
{
System.out.println("ResultSet is null ...");
}
------------------
Hrithik Roshan
Greenhorn

Joined: Dec 12, 2001
Posts: 23
Originally posted by David O'Meara:
[B]With regards to the code from Hrithik Roshan, the first version is wrong (it will detect if there aren't any results correctly, but if there it discards the first result!) and the second returns the number of rows inserted...
Maybe what you want is more like:

Dave[/B]

well Dave!
i think i can do this way!
--------------------------------------------
if(resultset != null)
{
if(!resultset.next()) System.out.println("No rows retrieved");
else
{
while(resultset.next())
{
//dosomething
}
}
}
else
{
System.out.println("ResultSet is null ...");
}
------------------
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

just a little heads up for you in your:
by the time you start processing your results in the while loop, you have called rs.next() twice. This means that you will start processing on the second record, missing out on the first. In Daves example, after calling the first rs.next() he processes the first record, then proceeds to the while loop to process the rest.
Also, the jdbc specs say that the resultset returned by statement.executeQuery() will never be null. a resultset will always be created (just empty). So the condition if(rs==null) will never occur(unless your driver does not follow the JDBC specifications). If there is a problem creating the resultset then a SQLException will be thrown and it will skip the remaining code anyways.
This link also discussed this problem in depth. There are many ways to check if a resultset has returned any results. Choose the one that best fits your program: http://www.javaranch.com/ubb/Forum3/HTML/001904.html
Jamie
 
Don't get me started about those stupid light bulbs.
 
subject: result null is null(urgent help)
 
Similar Threads
Regarding JTabel
Question about the @SecondaryTable annotation
How to verify if an SQLJ iterator returns zero rows?
call stored procedure
Result Set size