Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • 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?

 
K. Tsang
Bartender
Posts: 3585
16
Android 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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic