Illegal Operation On Empty Result Set Java Netbeans

in my case, if I add new data, for example tank data, then when I enter the distribution calculation table and click the button, the data cannot be calculated, an error message appears.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       try {
            java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/surya_nusratim2","root","");
            ResultSet jumlah_sumber = cn.createStatement().executeQuery("select count(*) from sumber");
            ResultSet jumlah_minta = cn.createStatement().executeQuery("select count(*) from pesanan_pel where status="0" ");

            jumlah_sumber.next();
            jumlah_minta.next();
            int a = Integer.parseInt(jumlah_sumber.getString(1));
            int b = Integer.parseInt(jumlah_minta.getString(1));
            int demand[] = new int [b];
            int supply[] = new int [a];
            double biaya[][] = new double [a][b];
            double biayaoptimal[][] = new double [a][b];
            int isi[][]= new int [a][b];
            

            ResultSet rs = cn.createStatement().executeQuery("select id_sumber,kapasitas_sumber from sumber ");
            ResultSet rs2 = cn.createStatement().executeQuery("select id_pesan,id_tangki,jumlah_produk from pesanan_pel where status="0"");
            int x = 0;
            while(rs.next()){
                
                supply[x] = Integer.parseInt(rs.getString(2));
             //   System.out.println(supply[x]);
                x++;
                // TODO add your handling code here:
            }

            //System.out.println("select total_biaya from biaya where id_gudang="+rs.getString(1)+" and id_toko='"+rs2.getString(2)+"'");
            rs.beforeFirst();
         //   rs2.beforeFirst();

            int i = 0;
            while(rs2.next()){
                int j = 0;

                while (rs.next()){
                   // System.out.println("select total_biaya from biaya where id_gudang="+rs.getString(1)+" and id_toko='"+rs2.getString(2)+"'");
                    ResultSet rs3 = cn.createStatement().executeQuery("select total_biaya from biaya where id_sumber="+rs.getString(1)+" and id_tangki='"+rs2.getString(2)+"'");
                    rs3.next();
                    biaya[j][i] = Double.parseDouble(rs3.getString(1));
                    biayaoptimal[j][i] = Double.parseDouble(rs3.getString(1));

                    j++;

                }
                rs.beforeFirst();
                demand[i] = Integer.parseInt(rs2.getString(3));

                i++;
            }

            for (int p=0; p<biaya.length; p++) {
                for (int q=0; q<biaya[0].length; q++){
                 //   System.out.print(biaya[p][q]+" ");

                }
                System.out.println("");
            }
            for (int q=0; q<supply.length; q++) {
                //System.out.print(supply[q]+" ");
            }
            for (int p=0; p<demand.length; p++) {
               // System.out.print(supply[p]+" ");
            }
            boolean selesai = false;
            while(!selesai){
                int baris_kecil = 0;
                int kolom_kecil = 0;

                for (int p=0; p<biaya.length;p++){
                    for (int q=0; q<biaya[0].length;q++){
                        if ((biaya [p][q] != 0 && biaya[p][q]<biaya[baris_kecil][kolom_kecil]) || (biaya[baris_kecil][kolom_kecil]==0)){
                           // System.out.println("Kondisi Satu");
                            baris_kecil = p;
                            kolom_kecil = q;
                        }
                        else if ((biaya[p][q]!=0 && biaya[p][q]==biaya[baris_kecil][kolom_kecil])){
                            /*System.out.println("Kondisi dua");
                            System.out.println("isi terkecil sekarang = "+Math.min(supply[baris_kecil], demand[kolom_kecil]));
                            System.out.println("isi posisi sekarang = "+Math.min(supply[p], demand[q]));
                            System.out.println("Supply sekarang = "+supply[p]);
                            System.out.println("Demand sekarang = "+demand[q]);*/

                            if (Math.min(supply[baris_kecil], demand[kolom_kecil]) < Math.min(supply[p], demand[q]))
                            {
                                baris_kecil = p;
                                kolom_kecil = q;
                            }
                        }
                    //    System.out.println("Cek ===== Baris"+baris_kecil+" Kolom "+kolom_kecil+"Biaya"+biaya[baris_kecil][kolom_kecil]);
                    }
                }
                isi[baris_kecil][kolom_kecil] = (int) Math.min(supply[baris_kecil], demand[kolom_kecil]);
                supply[baris_kecil]-=isi[baris_kecil][kolom_kecil];
                demand[kolom_kecil]-=isi[baris_kecil][kolom_kecil];

                if(supply[baris_kecil]==0){
                    for (int p=0;p<demand.length;p++)
                    {
                        biaya[baris_kecil][p]=0;
                    }
                }
                else if (demand[kolom_kecil]==0){
                    for (int q=0;q<supply.length;q++){
                        biaya[q][kolom_kecil] = 0;
                    }

                }
              /*  System.out.println("Baris "+baris_kecil);
                System.out.println("Kolom "+kolom_kecil);
                System.out.println("Isi "+isi[baris_kecil][kolom_kecil]);*/

                int cek = 0;

                for (int q=0; q<demand.length;q++)
                {

                    cek+=demand[q];
                }
                if (cek==0){

                    selesai=true;
                }
              /*  System.out.println("baris terkecil: "+baris_kecil);
                System.out.println("Kolom terkecil: "+kolom_kecil);*/
            }
            rs.beforeFirst();
            rs2.beforeFirst();
            for (int p = 0; p<isi.length;p++){
                for (int q=0; q<isi[0].length; q++){
                    //  System.out.print(isi[p][q]+" ");
                    if (isi[p][q]>0){
                        cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/surya_nusratim2","root","");
                        rs.absolute(p+1);
                        rs2.absolute(q+1);
                        double hasil = biayaoptimal[p][q] * isi[p][q];
                         

                        String sql = "insert into pendistribusian (id_pesan, id_sumber, jumlah_dis, biaya_optimal) values(?,?,?,?)";
                        String sql2 = "update sumber set kapasitas_sumber = kapasitas_sumber-? where id_sumber=?";
                        String sql3 = "update pesanan_pel set status="1" where id_pesan=?";
                      
                        PreparedStatement stat = cn.prepareStatement(sql);
                        stat.setString(1, rs2.getString(1));
                        stat.setString(2, rs.getString(1));
                        stat.setString(3, String.valueOf(isi[p][q]));
                        stat.setString(4, String.valueOf(hasil));
                        stat.executeUpdate();

                        PreparedStatement stat2 = cn.prepareStatement(sql2);
                        stat2.setString(1, String.valueOf(isi[p][q]));
                        stat2.setString(2, rs.getString(1));
                        stat2.executeUpdate();

                        PreparedStatement stat3 = cn.prepareStatement(sql3);
                        stat3.setString(1, rs2.getString(1));
                        stat3.executeUpdate();
                         tampildistribusi2();
                         tampildistribusi();
                    }
                      
                }
                System.out.println("");
            }
            JOptionPane.showMessageDialog(rootPane, "Perhitungan Berhasil","UD Tenu",1);
            JOptionPane.showMessageDialog(rootPane, "Data Perhitungan Berhasil Disimpan","Lihat Di Tabel Distribusi",1);

        }catch (SQLException ex) {
            System.out.println(ex);
        } 
                       
                      
    }

Leave a Comment