jeudi 6 août 2015

pad the list with zeroes if it has less than 3 entries

I have 5 checkboxes and I have limitted the selection to 3 only. The list values of each selected checkbox will be stored to the database and pad the list with zeroes if it has less than 3 entries. How can I pad the list with zeroes?

List < Integer > preferences = new ArrayList < Integer > ();
if (chckbxLei.isSelected()) {
    preferences.add(Integer.valueOf(chckbxLei.getName()));

}

if (chckbxAdv.isSelected()) {
    preferences.add(Integer.valueOf(chckbxAdv.getName()));
}
if (chckbxHis.isSelected()) {
    preferences.add(Integer.valueOf(chckbxHis.getName()));
}
if (chckbxOut.isSelected()) {
    preferences.add(Integer.valueOf(chckbxOut.getName()));
}
if (chckbxFAK.isSelected()) {
    preferences.add(Integer.valueOf(chckbxFAK.getName()));
}

if (preferences.size() > 3) {
    JOptionPane.showMessageDialog(frame,
        "Please Select Only 3! ");
    return;

}
Place place = new Place();
try {
    place.addPre(preferences);
} catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

Place.java

public void addPre(List < Integer > preferences) throws Exception {
DatabaseConnection db = new DatabaseConnection();
Connection connect = db.getConnection();
String sql = "Insert into preferences(Pre1,Pre2,Pre3,Pre4,Pre5)VALUES (?,?,?,?,?) ";
PreparedStatement ps = connect.prepareStatement(sql);
for (int i = 0; i < 5; i++) {

    ps.setInt(i + 1, preferences.get(i));

}
ps.executeUpdate();
connect.close();
ps.close();

}




Aucun commentaire:

Enregistrer un commentaire