0

Okay, so this is what I have so far and it works. However, I'm not able to specify to which ? (or parameter) the int 1 or 0 (boolean converted to int) is set (through setInt()). I tried using the getName() to see the component variable name but it returns null when I print it.

  1. How can I assign a checkbox's value as parameter to my PreparedStatement if I'm not able to specify correctly on which textbox the value of 1 or 0 is from?

    ps.setInt(1,valueOfUnknownCheckbox); //need to put the correct checkbox as 2nd argument.

  2. I saw some information about using Java Reflection which I'm totally unfamiliar with. Is there any way without using Java Reflection?

Some of the checkbox values are not going to the correct database table's column.

I hope you can help with me guys. I wanted to be able to use List to collect components from containers to lessen the lines of codes.

Here's my code.

private void saveAdminPermissions(){
    List<Component> adminPermissionsChbxs = fm.getComponentsAsList(administrationPermissionsCheckBoxPanel);

    Boolean bool = null;

    String updateSQL = 
            "UPDATE allusers_admin_permissions SET CURC_BTN=?, DISCOUNTS_BTN=?, SECTIONS_BTN=?,"
            + " USERS_BTN=?, SCHEDULING_BTN=?, YRLEVELS_BTN=?, ACCTG_BTN=? WHERE USERID=? ";
    try(Connection con = DBUtil.getConnection(DBType.MYSQL);
        PreparedStatement ps = con.prepareStatement(updateSQL);)
    {
        int x=1;
        for(Component c : adminPermissionsChbxs){
            if(c instanceof JCheckBox){
               bool = ( (JCheckBox)c ).isSelected(); 
               JOptionPane.showMessageDialog(null,"Name: "+ ((JCheckBox)c).getName() );
                JOptionPane.showMessageDialog(null,bool);
            } //--end of if
            int boolToInt = (bool)?1:0 ;
            ps.setInt(x, boolToInt);
            x++;
        }//--end of forloop
            ps.setInt(8, um.getIdOfSelected(usersList));
            ps.executeUpdate(); JOptionPane.showMessageDialog(null,"Update successful");
    }catch(SQLException e){
        JOptionPane.showMessageDialog(null,"Error@saveAdminPermissions\n"+ e.getMessage());
    }
}//--end of method

Thanks.

heisenberg
  • 1,784
  • 4
  • 33
  • 62

1 Answers1

2

Several approaches are possible:

  • Instead of looping though view components, create a permission model having a collection of states such as List<Boolean>. As shown in How to Use Check Boxes, let each JCheckBox have an ItemListener that updates its Boolean value in the List; an example is seen here. Use the list in constructing your PreparedStatement.

  • Alternatively, store the permission description and state in a suitable TableModel, and use a JTable as the view. As described in How to Use Tables: Concepts: Editors and Renderers, the default renderer/editor for a model value of type Boolean is a check box. Neither the renderer nor editor remember the value between calls to render or edit a cell's value, but the table underlying TableModel must do so. Some guidelines and a typical example are cited here. You should be able to invoke the table's getValueAt() method to find the current setting of the desired cell in a particular row and column, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045