如何将checkBoxList中的已检查项插入数据库 - JAVA SE

时间:2015-09-06 15:30:19

标签: java database checkboxlist

我知道将checkBoxList中的选定值插入数据库的正确方法时遇到问题。

我的代码:

public class AdminPanel extends javax.swing.JFrame {

  public static  CheckBoxList cbList = new CheckBoxList();
/**
 * Creates new form AdminPanel
 */
public AdminPanel() throws SQLException {
    initComponents();
    Database.startConnection();

    ResultSet rset = ServiceDb.getAllServices();
    if(rset==null) {}
    else {
        List<JCheckBox> checkboxes = new ArrayList<JCheckBox>();
        JCheckBox[] myList = null;
        while(rset.next()){
        JCheckBox check = new JCheckBox(rset.getString("service"));
        checkboxes.add(check);
        }
        cbList.setListData(checkboxes.toArray());
        testPanel.add(cbList);
        testPanel.revalidate();
        testPanel.repaint();
    }
    List<String> f = cbList.getSelectedValuesList();




@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    serviceTab = new javax.swing.JTabbedPane();
    typePanel = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    typeField = new javax.swing.JTextField();
    jLabel5 = new javax.swing.JLabel();
    testPanel = new javax.swing.JPanel();
    servicePanel = new javax.swing.JPanel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    serviceField = new javax.swing.JTextField();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    typePanel.setBackground(new java.awt.Color(51, 51, 255));

    jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(255, 255, 255));
    jLabel1.setText("Add Type");

    jLabel2.setForeground(new java.awt.Color(255, 255, 255));
    jLabel2.setText("Type");

    jButton1.setText("Submit");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jLabel5.setForeground(new java.awt.Color(255, 255, 255));
    jLabel5.setText("Services");

    testPanel.setBackground(new java.awt.Color(51, 51, 255));

    javax.swing.GroupLayout typePanelLayout = new javax.swing.GroupLayout(typePanel);
    typePanel.setLayout(typePanelLayout);
    typePanelLayout.setHorizontalGroup(
        typePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(typePanelLayout.createSequentialGroup()
            .addGap(10, 10, 10)
            .addGroup(typePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(typePanelLayout.createSequentialGroup()
                    .addComponent(jLabel5)
                    .addGap(18, 18, 18)
                    .addComponent(testPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(typePanelLayout.createSequentialGroup()
                    .addComponent(jLabel2)
                    .addGap(34, 34, 34)
                    .addComponent(typeField, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    typePanelLayout.setVerticalGroup(
        typePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(typePanelLayout.createSequentialGroup()
            .addGap(24, 24, 24)
            .addComponent(jLabel1)
            .addGap(32, 32, 32)
            .addGroup(typePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(typePanelLayout.createSequentialGroup()
                    .addGap(3, 3, 3)
                    .addComponent(jLabel2))
                .addComponent(typeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(28, 28, 28)
            .addGroup(typePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(typePanelLayout.createSequentialGroup()
                    .addComponent(jLabel5)
                    .addGap(0, 49, Short.MAX_VALUE))
                .addComponent(testPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGap(18, 18, 18)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    serviceTab.addTab("Type", typePanel);

    servicePanel.setBackground(new java.awt.Color(51, 51, 255));

    jLabel3.setForeground(new java.awt.Color(255, 255, 255));
    jLabel3.setText("Add Service");

    jLabel4.setForeground(new java.awt.Color(255, 255, 255));
    jLabel4.setText("Service");

    jButton2.setText("Submit");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout servicePanelLayout = new javax.swing.GroupLayout(servicePanel);
    servicePanel.setLayout(servicePanelLayout);
    servicePanelLayout.setHorizontalGroup(
        servicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(servicePanelLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(servicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(servicePanelLayout.createSequentialGroup()
                    .addGroup(servicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addGroup(servicePanelLayout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addGap(40, 40, 40)
                            .addComponent(serviceField, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(0, 205, Short.MAX_VALUE)))
            .addContainerGap())
    );
    servicePanelLayout.setVerticalGroup(
        servicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(servicePanelLayout.createSequentialGroup()
            .addGap(28, 28, 28)
            .addComponent(jLabel3)
            .addGap(39, 39, 39)
            .addGroup(servicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4)
                .addComponent(serviceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(53, 53, 53))
    );

    serviceTab.addTab("Service", servicePanel);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(serviceTab)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(serviceTab)
    );

    pack();
}// </editor-fold>                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(serviceField.getText().matches("^\\s*$")) JOptionPane.showMessageDialog(null, "fill");
    else{

        ServiceDb.createService(serviceField.getText().toString());
        JOptionPane.showMessageDialog(null, "Done");
        serviceField.setText("");
        CheckBoxList cbList = new CheckBoxList();
    }
}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (typeField.getText().matches("^\\s*$")) JOptionPane.showMessageDialog(null, "fill");
    else
    {
        //TypeDb.createType(typeField.getText());
        JOptionPane.showMessageDialog(null, "Done");
        typeField.setText("");

        List<JCheckBox> f = cbList.getSelectedValuesList();
        System.out.println(f.size());
    for (int i = 0; i<3; i++) {


    }
    }
}                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(AdminPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(AdminPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(AdminPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(AdminPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                new AdminPanel().setVisible(true);
            } catch (SQLException ex) {
                Logger.getLogger(AdminPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField serviceField;
private javax.swing.JPanel servicePanel;
private javax.swing.JTabbedPane serviceTab;
private javax.swing.JPanel testPanel;
private javax.swing.JTextField typeField;
private javax.swing.JPanel typePanel;
// End of variables declaration                   
}

0 个答案:

没有答案