Java how to set all data must be inserted

int p =JOptionPane.showConfirmDialog(null,"Are you sure to add this staff?","Comfirmation",JOptionPane.YES_NO_OPTION);
        if (p==0){
            String sql ="Insert into login.createstaff (Staff_ID,Staff_Name,Staff_password,Staff_Tel,Staff_position,image) values (?,?,?,?,?,?)";
            try {
                pst=conn.prepareStatement (sql);
                pst.setString(1,staffID.getText());
                pst.setString(2,staffName.getText());
                pst.setString(3,staffPassword.getText());
                pst.setString(4,staffTel.getText());
                String value =staffPosition.getSelectedItem().toString();
                pst.setString(5,value);

                pst.setBytes(6,person_image);


                pst.execute();
                JOptionPane.showMessageDialog(null, "New Staff Added");


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

Hi all, this is my dialog to add a staff record.

But how can I set all the data that must be inserted (No data field can be empty) before I save it into the database? (I want to alert the user if he or she doesn’t enter all the data.)

All data inside the database I have already set “not null” but I can still save a record even though the data is null.

Answer

You can use staffID.getText().trim().isEmpty() to check if an JTextField is empty or not, using JOptionPane message dialog to remind the users. An example would be like:

public static void main(String[] args) {
    int p = JOptionPane.showConfirmDialog(null, "Are you sure to add this staff?", "Comfirmation", JOptionPane.YES_NO_OPTION);

    if (p == 0) {
        if (staffID.getText().trim().isEmpty()) {// validate user input
            JOptionPane.showMessageDialog(null, "Please enter staff ID"); // remind user
        } else if (staffName.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Please enter staff name");
        } else if (staffPassword.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Please enter password");
        } else if (staffTel.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Please enter staff telephone");
        } else {
            String sql = "Insert into login.createstaff (Staff_ID,Staff_Name,Staff_password,Staff_Tel,Staff_position,image) values (?,?,?,?,?,?)";
            try {
                pst = conn.prepareStatement(sql);
                pst.setString(1, staffID.getText());
                pst.setString(2, staffName.getText());
                pst.setString(3, staffPassword.getText());
                pst.setString(4, staffTel.getText());
                String value = staffPosition.getSelectedItem().toString();
                pst.setString(5, value);

                pst.setBytes(6, person_image);


                pst.execute();
                JOptionPane.showMessageDialog(null, "New Staff Added");


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

Leave a Reply

Your email address will not be published. Required fields are marked *