awsdocs/aws-java-developer-guide

java Multhreading

OwiyeD opened this issue · 0 comments

Hello,
I really like your topics and the way you explain. I am a computer science student. I am developing an desktop app that would help farmers get to know the best seed for the area based on the database of experts. I did that well but I also wanted them to be updated on what to do every time there has to be an activity on their crops for instance weed, apply fertilizer etc. I have a table of timing which stores duration for a given crop and what my program does it subtract the starting date as the planting date from the current date and checks from the timing table if the differences in dates matches the duration stored for the crop it fetches the action. I have tried doing a thread but it is not fetching the action. Kindly this is the section of the code.
String selcted_crop = crop();
String selcted_place = activity_Area();

try {
    String s = "Insert into activities(id_Number, f_Name, l_Name, phone_Number, start_Date, crop_ID, place_ID, farm_Size)values(?,?,?,?,?,?,?,?) ";
    pst = conn.prepareStatement(s);
    pst.setString(1, id_Number.getText());
    pst.setString(2, f_Name.getText());
    pst.setString(3, s_Name.getText());
    pst.setString(4, phone_Number.getText());
    pst.setString(5, ((JTextField) jDateChooser2.getDateEditor().getUiComponent()).getText());
    pst.setString(6, selcted_crop);
    pst.setString(7, selcted_place);
    pst.setString(8, farm_Size.getText());
    pst.executeUpdate();
    JOptionPane.showMessageDialog(null, "Record Saved");

    String plc1[] = new String[100];
    String placeve = "";

    Thread t = new Thread() {
        public void run() {
            for (;;) {
                Calendar c = new GregorianCalendar();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);

                int hour = c.get(Calendar.HOUR);
                int min = c.get((Calendar.MINUTE));
                int sec = c.get(Calendar.SECOND);

                CurrentDate = year + "-0" + month + day;
                CurrentHour = "0" + hour + min + sec;

            }
        }
    };
    t.start();

    for (int i = 0; i <= 3; i++) {
        try {
            String stl = "Select *from vendors where place_ID = '" + place_ComboBox.getSelectedItem().toString() + "'";
            pst = conn.prepareStatement(stl);
            rst = pst.executeQuery();
            while (rst.next()) {
                plc1[i] = rst.getString("vendor_Name");
                JOptionPane.showMessageDialog(null, plc1[i]);

            }
        } catch (Exception e) {

        }
    }

    String name = f_Name.getText();
    try {
        String g = "select *from activity where s_name=?";
        pst = conn.prepareStatement(g);
        pst.setString(1, name);
        rst = pst.executeQuery();
        if (rst.next()) {
            name = rst.getString("s_Name");
        }
    } catch (Exception e) {

    }
    String crop1 = crop_ComboBox.getSelectedItem().toString();
    String crop_ID = "";
    try {
        String l = "select *from crops where crop_Name=?";
        pst = conn.prepareStatement(l);
        pst.setString(1, crop1);
        rst = pst.executeQuery();
        if (rst.next()) {
            crop_ID = rst.getString("crop_ID");
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
    String plc = place_ComboBox.getSelectedItem().toString();
    String place_ID = "";
    try {
        String re = "select *from places where place_Name=?";
        pst = conn.prepareStatement(re);
        pst.setString(1, plc);
        rst = pst.executeQuery();
        if (rst.next()) {
            place_ID = rst.getString("place_ID");
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

    String pie = "";
    String pie1 = "";
    try {
        String b = "select * from seeds where crop_ID=? and place_ID=?";
        pst = conn.prepareStatement(b);
        pst.setString(1, crop_ID);
        pst.setString(2, place_ID);
        rst = pst.executeQuery();

        if (rst.next()) {
            pie = rst.getString("seed_Name");
            pie1 = rst.getString("seed_ID");
            JOptionPane.showMessageDialog(null, "Hujambo " + name + ", You are being informed that the best seed for you is " + pie + ". KIndly buy the seeds from a trusted vendor");
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

    int uniqueid = 0;
    String action = "";
    String cp[] = new String[1000];
    int diffDays[] = new int[100];
    String pNumber = phone_Number.getText();
    for (int i = 0; i < uniqueid; i++) {
        try {
            String str = "Select *from activities where phone_Number = '" + pNumber + "'";
            pst = conn.prepareStatement(str);
            rst = pst.executeQuery();
            while (rst.next()) {
                String dp = rst.getString("start_Date");
                String kp = rst.getString("cropId");
            }

        } catch (Exception e) {

        }
        int dur = 0;

        try {
            String ke = "select from actions where seed_ID='" + pie1 + "'";
            pst = conn.prepareStatement(ke);
            rst = pst.executeQuery();
            while (rst.next()) {
                action = rst.getString("action");
                if (rst.next()) {
                    dur = rst.getInt("Duration");
                    action = rst.getString("action");
                    if (diffDays[i] == dur) {
                        JOptionPane.showMessageDialog(null, "Kindly it has gone'" + dur + "' you are being informed to '" + action + "'");
                    }

                }

            }
        } catch (Exception e) {

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

}