The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

I trying to calculate total price.

i have two columns named quantity and unit price in mysql database. Am using an abstactTablemodel. i need some help to calculate the general total prices of all products in the database. thanx in Advance.

Re: I trying to calculate total price.

Originally Posted by Muhanguzi

i have two columns named quantity and unit price in mysql database. Am using an abstactTablemodel. i need some help to calculate the general total prices of all products in the database. thanx in Advance.

To get the total unit price you just multiply the two columns together. then add all the total unit prices together to have a sum() of unit prices.

Re: I trying to calculate total price.

Originally Posted by Muhanguzi

Thanks William, but i need a sample code which would retrieve data from those columns, then use the methods above to calculate the general total.

We are not a code repository, so asking for (and expecting) code most likely will not get you very far. Rather, take William's advice and try to write the code yourself. Alternative to William's advice, you might also be able to write an SQL query that performs the operation for you. If you have a specific problem then by all means ask. Lastly, please use the code tags, as they actually help make the code readable.

Re: I trying to calculate total price.

Originally Posted by Muhanguzi

i need a sample code which would retrieve data from those columns, then use the methods above to calculate the general total.

There are many ways to program Java and SQL together, you can have the SQL functions do it for you or just gather all the data and write the program for calculation. You have your connection you query the table.

select * from mytable;

get the ResultSet= rs then loop through your rs in a for loop and do your calculation during that time. Return out of your Method with the answer.