Calculate sum of values in column in a tabel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mehbub
    New Member
    • Oct 2014
    • 2

    #1

    Calculate sum of values in column in a tabel

    I had a table with 3 columns and 4 rows in website view. I need to add the column values and show at the bottom of the table as total. I'm using spring mvc3. How can I calculate the sum in JSP?
    am using spring mvc3 in java
    but this code i got in internet. and it's input type is "text". and my requirement is to add the values of a column retrieved from db to the table
    Attached Files
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    How can I calculate the sum in JSP?
    You should not calculate the sum in JSP page. Read about MVC (Model-View-Controller). You should not do it in the view, but do it in the model. That means you should do it in the servlet code.

    but this code i got in internet and it's input type is "text"
    I do not understand. What code are you talking about? Please quote the code here.

    Usually a number is stored in a database column of type number or Decimal and not text. So if you retrieve the value, you can directly read it into a long or int variable. If it is text, you can convert it to a long or int with Integer.parseIn t() or Long.parseLong( ).
    Then you just sum up all values with sum = sum + newValue

    By the way, the attached image is so small and has such a bad resolution that I cannot read anything even if I zoom it.

    Comment

    Working...