0

I need to show the result in Row of JTable by calculate the value of rows using MySQL. Recevied + PCU stored in Balance when the user click the save button. What will be the query?

JTable showing the records; save the record of the table.

Picture of Jtable

JButton btnSave = new JButton("Save");
Image img4= new ImageIcon(this.getClass().getResource("/save.png")).getImage();
btnSave.setIcon(new ImageIcon(img4));
btnSave.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {


        try{
            String query="insert into Detail (ID,Date,Received,PCU,CLR,Sole,Sale,Balance) values (?,?,?,?,?,?,?,?)";
            PreparedStatement pst=connection.prepareStatement(query);

            pst.setString(1,textField.getText());
            pst.setString(2,textField_1.getText());
            pst.setString(3,textField_2.getText());
            pst.setString(4,textField_3.getText());
            pst.setString(5,textField_4.getText());
            pst.setString(6,textField_5.getText());
            pst.setString(7,textField_6.getText());
            pst.setInt(8, 2-3 );

            pst.execute();

            JOptionPane.showMessageDialog(null, "Data Saved");
            pst.close();
            //rs.close();

        }catch(Exception e){
            e.printStackTrace();
        }
        refreshTable();
    }
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

0

Recevied + PCU stored in Balance when the user click the save button.

So do the calculation when you store data to the table.

First of all the Received and PCU should NOT be stored as Strings. They should be stored an int's.

So your code should be something like:

int receivedData = Integer.parseInt( textField_?.getText() );
int pcuData = Integer.parseInt( textField_?.getText() );

pst.setInt(?, receivedData);
pst.setInt(?, pcuData);
...
pst.setInt(8, receivedData + pcuData);

Also, use meaningful variable names. I have no idea what textField_1, 2, 3, 4, represent. Use names like received, pcu, clr etc so we know what you are taking about.

camickr
  • 321,443
  • 19
  • 166
  • 288