File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes i need help on java database arithemetic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "i need help on java database arithemetic" Watch "i need help on java database arithemetic" New topic
Author

i need help on java database arithemetic

muntago Richard
Ranch Hand

Joined: Nov 13, 2010
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

Joined: Jan 10, 2002
Posts: 60992
    
  65

Please take the time to choose an appropriate forum for your posts. This forum is for questions on JSP. For more information, please click this link ⇒ CarefullyChooseOneForum.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Nov 13, 2010
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
function=addition.
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

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Nov 13, 2010
Posts: 82
i have tried it but it doesn't work. thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i need help on java database arithemetic