• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

This question is simple still not able to figure out what the issue is!

 
Ranch Hand
Posts: 172
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all below is my code and am trying to print the fetched value and its printing 0? Please help me figure out where am I going wrong?

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there

Are you sure the variables s and m have values? Not knowing your table columns, is there a "line" column? Try "select line from..." too. Are there rows returned in the DB? If not then the result set should be null and the while loop doesn't execute.

Also you should consider passing the variables s and m into the prepared statement rather than concat to sql.



Oh for best practice, you should do the close connection, result set stuff in the finally block.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two possibilities:

1) Your where condition is not matched by any row in the table. The while(rs.next()) loop is never entered and the value of i is never changed from its initial value of 0.

2) There are several rows that match the where condition. The database gives them in some order and it just happens that the last row returned by the query has a value of 0 (or NULL - see javadoc!) in the line column. The last encountered value remains stored in i after the loop terminates and is printed.

To find out which of these two possibilities is happening, just move the System.out.println(i); into the loop - it will print all values fetched from the database. If it prints nothing, it's the first case. If it prints something, at least you'll see which values and in which order are selected. Add call to rs.wasNull() to see whether the returned value was NULL or not.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic