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

while(rs.next()) Skips first Record

 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi al I have an application in which there is one receipt number and which will be given to the same customer as customer is going to buy more than one product at a time. For example customer buys products as below

product x = 250
product y = 300
product z = 400

hence total bill will be of
x + y + z = 950
but the receipt number will be the same

now below is my query



now what is it doing that if the customer has purchased the 3 products namely x ,y ,z then it actually skips the first record i.e. it fetches only y & z product but displays the total = 950 where in it should display the total as 550.

Can anybody help me why it is skipping the very first record? Please help me out with this
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you getting the total from? A direct SQL query?
 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops sorry forgot to paste that snippet here



ok this is how am calculating and storing the grand total by fetching the amount(s) of the total column's records one by one. It is calculating the total correctly but not printing the first record. What I wish to print is

x = 250
y = 300
z = 400

grand total = 950


but it prints it like

y = 300
z = 400

grand total = 950


so while displaying it is actually skipping the first record, why?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't shown us the code which prints the values. The bug might lie there as well.
 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:You haven't shown us the code which prints the values. The bug might lie there as well.


well its a huge code but here it is

 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am trying to generate an invoice and in the form of a pdf
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two while(rs.next()) loops in your code - one nested in another. Each of these advances to the next row. I'd guess that this is the issue - you don't want to advance to the next row at the first execution of the inner while(...).

My advice would be to separate the database and PDF generation code. Create a method which will read the data from the database and store it into a list of objects (these objects are usually called Data Transfer Objects), and then use this list in a separate method to create the PDF. This will allow you inspect the values read from the database (by debugging or logging) and easily see whether the bug is in the database code, or in the PDF code. At the same time both methods will be simpler.

Also note that while you're using a PreparedStatement, you're not using it correctly. The way you use it doesn't bring any of the PrepraredStatement's benefits. The proper way would be:
Even worse thing is that you're not closing the DB resources. You need to close all database resources in a finally block, so that they are released even when an error occurs. Using the new Java 7 syntax (try with resources) is even better option, if you're on Java 7 already.

And one last point - don't catch Exception, catch just the SQLException, and - apart from displaying a message box - print out the stack trace to the console or into a log file. It makes resolving the issues much easier (especially once in production).

Edit: I've edited your post to wrap/shorten long lines which made the code and this thread hard to read. Please see UseCodeTags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic