This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# i need help on java database arithemetic

muntago Richard
Ranch Hand
Posts: 82
Good day experts, this code was aimed at performing calculation and inserting the total to database. however, it can only add the first and second amount hence

for example amount = first or existing amount in the database=300
rs.getInt(1) = second amount to be inputed =200
total =500.
Now when I want to input the third amount as per rs.getInt eg 400
the total amount will still remain 500 instead of 900 that is 300+200+900
My question is how do solve the problem so that the total column will be on the increase whenever new amount as per rs.getInt(1) is added
amount = first table column and data type is int hence getInt(1)
total = second column
student= third column
below is the code

Bear Bibeault
Author and ninkuma
Marshal
Posts: 64701
86

This post has been moved to a more appropriate forum.

Paul Clapham
Sheriff
Posts: 20958
31
This isn't "database arithmetic", this is just ordinary programming logic. You want to find the sum of rs.getInt(1) over all rows?

Edit: no, actually now that I look closer at your post, that isn't what you want. But I can't understand what you do want. You said
Now when I want to input the third amount as per rs.getInt eg 400

I don't understand. You want to "input" an amount? From where?

muntago Richard
Ranch Hand
Posts: 82
Thanks I have done what you said and i have done some modifications, but now the student score is added from the input form with respect to a particular student id,
the total will be updating anything it likes. assuming in the first place at test1.jsp i added
student id=2011
student name=mutago
student score=80
function = addition and click submit, the value will be submitted.
I now go back to test1.jsp and add again

student id=2011
student name=mutago
student score=70
when i click submit, my total is supposed to be 150 but rather it will update any value it likes

If try the third time assuming the score now is 50 , my total is supposed to be 200 ie 80+70+50 but it will keep on displaying any thing it like below is the full code thanks

Martin Vajsar
Sheriff
Posts: 3752
62
There are probably more issues with your code, but on line 40 of test2.jsp you're trying to insert a new record for student even if it already exists in the database. It seems syntactically incorrect to me, but maybe your database can accept it. If it is the case, you then have more than one record per student here, which would allow for same scenarios corresponding to the symptoms you've described above.

You should use UPDATE instead of INSERT there. Moreover, you should define primary key on your STUDENT table so that inserting a duplicate record would fail immediately (this would provide you more clues as to where the problem actually is).

There are other potential issues.

I don't do JSP, but seasoned JSP programmers would probably advice you not to include database code in JSP pages.

The STUDENT table has fields named SCORE and TOTAL. Is the requirement really to keep total of the score and the last score assigned to the student? Should not all individual scores be recorded for every student, not just the last one?

muntago Richard
Ranch Hand
Posts: 82
i have tried it but it doesn't work. thanks