
how to calculate bill extracting quantity n price from one database table and inserting the result into another in java

Hi Friend,
Try the following code:
import java.sql.*;
import java.util.*;
import java.text.*;
class GetTotal
{
public static void main(String[] args)
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from items");
ArrayList list=new ArrayList();
while(rs.next()){
double quantity=rs.getDouble("quantity");
double price=rs.getDouble("price");
double tot=quantity*price;
list.add(Double.toString(tot));
}
rs.close();
Iterator itr = list.iterator();
while(itr.hasNext()){
String str = itr.next().toString();
double tot=Double.parseDouble(str);
int i=st.executeUpdate("insert into total(total) values("+tot+")");
}
}
catch(Exception e){
System.out.println(e);
}
}
}
For the above code, we used following database table:
1)items
CREATE TABLE `items` (
`id` bigint(255) NOT NULL auto_increment,
`item` varchar(255) default NULL,
`quantity` double default NULL,
`price` double default NULL,
PRIMARY KEY (`id`)
)
2)total
CREATE TABLE `total` (
`id` bigint(255) NOT NULL auto_increment,
`total` double default NULL,
PRIMARY KEY (`id`)
)

Thank you it helped a lot for my project
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.