Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
    Bookmark Topic Watch Topic
  • New Topic

my resultset return what????

 
alex han
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
i use while(rs.next()) to get data from a table named "xx" in oracle,
code is :
int j=0;
while (rs.next())
{
j=rs.getint(1)+1;
System.out.println(rs.getint(1))
}
someone told me that if there are no record in table "xx",rs.next() return false,so j should equals 0 and System.out.println should not execute
but i find that even there are no record in table "1", j equals 1! and print 0 in tomcat's console
why?i believe if rs.next() return false,j=rs.getint(1)+1 and System.out.println should not execute,but infact it execute.
is anybody can help me? thanks a lot
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
it depends on your query. If you use "select count(*) from ..." it will always return at least a 0. If in fact you have a "select column1, ... from XX ..." then rs.next() will never be true when no matches are found in the database unless your driver your driver is not even close to being JDBC compliant. Also, verify using a SQL client that no results are returned for the exact query you are trying to execute in java. If you find that it is still not working properly, post the JDBC code ( including the SQL statement ) and identify the JDBC driver/database combo you are using so we can help you solve this problem.
Jamie
 
Cobras9
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
depending on different database, some db return false, some returns 0(like our db Informix)
becuase u asked for an Integer and it doesnt find a record in the database so it returns a "0" then u added 1 on so the System.out.println prints a "1"
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hmm, it appears that this thread is continued HERE
I'll close this one to prevent duplicate conversations.
Dave
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic