-5

i want to multiply 2 arrays and show to the table but this code doesn't work , here this code , please help me

public void hitung () 

    int hasil = 0;
    long nilai2 ;
    int quan = 0 ;
    int quans = 0;

    try {

            String sql = ("SELECT tbl_masakan.nama_masakan, banyak, tbl_masakan.harga_masakan FROM tbl_masakan, tbl_det_pesanan WHERE id_det_pesanan = '"+ id +"' and tbl_det_pesanan.id_masakan = tbl_masakan.id_masakan ");
            ResultSet rsuser= cn.stt.executeQuery(sql);
            rsmetadata = rsuser.getMetaData();
            while (rsuser.next()) {  

                int size = 0;

                   if (rsuser != null)
                   {
                       rsuser.beforeFirst();
                       rsuser.last();
                       size = rsuser.getRow();
                   }

                   String str = rsuser.getString(2);
                   quan = Integer.parseInt(str);
                   String strs = rsuser.getString(3);
                   quans = Integer.parseInt(strs);
                   hasil = hasil + (quan*quans);
            }

        } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.toString());
        }

    txt1.setText(String.valueOf(hasil));
}
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58

1 Answers1

0

There some minor problems with your code.

  1. There are variables defined but never used, as is the case with size and rsmetadata (which is not declared in the code you posted).
  2. It is not necessary to retrive the columns from the database as Strings and then perform the conversion using Integer.parseInt(). You can retrieve them directly as Integers using rsuser.getInt(columnIndex).
  3. The documentation for the method executeQuery(String sql) of the Statement class says that it returns "a ResultSet object that contains the data produced by the given query; never null" so checking for null is unnecessary.

My guess is that the cause of your problem (although you didn't actually said what was the output of your code) is the calculation of the size of the ResultSet. Observe that it leaves the cursor pointing to the last position (rsuser.last()), therefore your while(rsuser.next()){...} loop should run only once. If you actually need to calculate the size for some reason, you should do it before the while() loop and make sure to call rsuser.beforeFirst() again so that the cursor points to the beginning of the Resultset before entering the loop. In that case your code should look like this:

    String sql = ...;
    ResultSet rsuser= cn.stt.executeQuery(sql);
    // calculate size
    int size = 0;
    if (rsuser.last()) {
       size = rsuser.getRow();
       rsuser.beforeFirst();
    }

    while (rsuser.next()) {  
        quan = rsuser.getInt(2);
        quans = rsuser.getInt(3);
        hasil = hasil + (quan*quans);
    }

Lastly, observe that this method of calculating the size of the ResultSet is not without problems, all of which are clearly pointed out in this discussion: How do I get the size of a java.sql.ResultSet?

Community
  • 1
  • 1