In this Tutorial i shown how to Calculate JTable Column Values in Java Swing, Watch Video and its source Code
String customer=customername.getText();
String invoicenumber=invoiceno.getText();
String invoiceun=invoiceunits.getText();
int units_left1=Integer.parseInt(leftUnits.getText());
if(units_left1<=0)
{
JOptionPane.showMessageDialog(null, "Stock Completed Please Order");
}else{
if(invoiceun.isEmpty() && customer.isEmpty())
{
customername.setBorder(BorderFactory.createLineBorder(Color.RED));
invoiceunits.setBorder(BorderFactory.createLineBorder(Color.RED));
JOptionPane.showMessageDialog(null, "This Please enter Customername and Units");
return;
}
else{
customername.setBorder(BorderFactory.createLineBorder(Color.GRAY));
invoiceunits.setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
try {
PreparedStatement statement = connection.prepareStatement("insert into invoice values(?,?,?)");
statement.setString(1, customer);
statement.setString(2, invoicenumber);
statement.setString(3, invoiceun);
int x=statement.executeUpdate();
JOptionPane.showMessageDialog(null, "Data inserted sucessfully");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String sql="update additem set completestock = completestock-? where item=?";
int i = Integer.parseInt(invoiceunits.getText());
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,i);
statement.setString(2, (String)dropcombox.getSelectedItem());
statement.executeUpdate();
} catch (Exception e2) {
// TODO: handle exception
}
try {
String sql="select * from additem where item=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, (String)dropcombox.getSelectedItem());
ResultSet set = statement.executeQuery();
while(set.next())
{
// dropcombox.addItem(set.getString("item"));
// pricetext.setText(set.getString("price"));
// taxtext.setText(set.getString("tax"));
leftUnits.setText(set.getString("completestock"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String salerpt="insert into salesreport values(?,?,?,?,?,?,?,?,?,?)";
PreparedStatement statement =connection.prepareStatement(salerpt);
statement.setString(1, customername.getText());
statement.setString(2, invoiceno.getText());
statement.setString(3, dateLabel.getText());
statement.setString(4, (String)dropcombox.getSelectedItem());
statement.setString(5, pricetext.getText());
statement.setString(6, taxtext.getText());
statement.setString(7, invoiceunits.getText());
statement.setString(8, leftUnits.getText());
statement.setString(9, null);
statement.setString(10, null);
statement.executeUpdate();
JOptionPane.showMessageDialog(null, "Sales Report Data saved");
} catch (Exception e2) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, e2);
}
try {
String update_tax="update salesreport set taxamount = (price*tax)/100";
PreparedStatement statement=connection.prepareStatement(update_tax);
statement.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String total_amount="update salesreport set total =(price * noofunits )+ taxamount";
try {
PreparedStatement statement=connection.prepareStatement(total_amount);
statement.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String print_total="select sum(total) from salesreport where INOVICENO=?";
PreparedStatement statement=connection.prepareStatement(print_total);
statement.setString(1, invoiceno.getText());
ResultSet set=statement.executeQuery();
while(set.next())
{
totalsum.setText(set.getString("sum(total)"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
row[0]=dropcombox.getSelectedItem(); // get text which you want to add in table
row[1]=invoiceunits.getText();
row[2]=taxtext.getText();
row[3]=pricetext.getText();
model.addRow(row);
// double total=0;
// for(int i=0; i<table.getRowCount();i++)
// // get Row Count here
// { // convert it to String and Display in Jlable
// int amount=Integer.parseInt((String) table.getValueAt(i, 3));
// total+=amount;
// }
//
// totalsum.setText(String.valueOf(total));
try {
String update="";
} catch (Exception e2) {
// TODO: handle exception
}
}