简体   繁体   中英

How to save the selected checkbox items in Jtable to mysql using java?

In my project, I want to schedule the classes and here is my GUI. PIC

Can someone guide me, how to save the checked value into the mysql database?

This is my save button code :

 private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                     
    try{
       String sql="insert into batch (course_code,batch_number,time,"+ time_table.getColumnName(time_table.getSelectedColumn()).toLowerCase()+")values(?,?,?,?)";

        pst=conn.prepareStatement(sql);

        String value1=course_code.getSelectedItem().toString();
        pst.setString(1,value1);

        String value2=course_code.getSelectedItem().toString();
        pst.setString(2, value2+"-"+year.getText()+"-"+group_no.getText());

        String value3=time_chooser.getSelectedItem().toString();
        pst.setString(3, value3);

       //String value4 = time_table.getModel().getValueAt(time_table.getSelectedRow(), 0).toString();
     //  pst.setString(4, value4);

    int a=time_table.getSelectedRow();
    int b=time_table.getSelectedColumn();
    String c=time_table.getModel().getValueAt(a, 0).toString();


            for(int i = 1 ; i < a ; i++)
                {
               for(int j = 1 ; j < b ; j++)
                    {
                  Boolean val1 = (Boolean)time_table.getModel().getValueAt(a, b);
                  if(Boolean.TRUE.equals(val1)) {
                   pst.setString(4, c );
                        }
                    }
                }

    pst.execute();
        JOptionPane.showMessageDialog(null, "Successfully Inserted");
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e); 
    }
} 

I want something like this. 在此处输入图片说明 This error pop ups. 在此处输入图片说明

I hope that is what you want (It should work as long as the columns in your database have the same name as the columns in your table):

    String sql="insert into batch (course_code,batch_number,time,"+ time.getColumnName(time.getSelectedColumn()).toLowerCase() +")values(?,?,?,?)";

    String value4 = time.getModel().getValueAt(time.getSelectedRow, 0);
    pst.setString(4, value4);

A little explanation:

time.getColumnName(time.getSelectedColumn()).toLowerCase()

should return the day you selected in lower case. It's the same as the name of your column in your database ("monday", "wednesday"). Now you are able to insert the time into the table. To do that you must get the selected row and with it you can get the cell value form the first cell of your checked row which is your time. Finally you can insert it into the database.

EDIT:

See comments (I can't test the code. If there are any mistakes in it, pls let me know):

 private void saveActionPerformed(java.awt.event.ActionEvent evt) {
        try{
            for(int i = 0 ; i < time_table.getModel().getRowCount() ; i++)
            {
                for(int j = 1 ; j < time_table.getModel().getColumnCount() ; j++)
                {
                    String sql="insert into batch (course_code,batch_number,time,"+ time_table.getColumnName(j).toLowerCase()+")values(?,?,?,?)";

                    pst=conn.prepareStatement(sql);

                    String value1=course_code.getSelectedItem().toString();
                    pst.setString(1,value1);

                    String value2=course_code.getSelectedItem().toString();
                    pst.setString(2, value2+"-"+year.getText()+"-"+group_no.getText());

                    String value3=time_chooser.getSelectedItem().toString();
                    pst.setString(3, value3);

                    String c=time_table.getModel().getValueAt(i, 0).toString();
                    Boolean val1 = (Boolean)time_table.getModel().getValueAt(i, j);
                    System.out.println(val1 + "\t");
                    if(Boolean.TRUE.equals(val1)) {
                        pst.setString(4, c );
                        pst.executeUpdate();
                        pst.close();
                        JOptionPane.showMessageDialog(null, "Successfully Inserted");
                    }
                    System.out.println();
                }
                catch(Exception e){
                    JOptionPane.showMessageDialog(null, e);
                }
            }
        }

Taking it like a String and saving it in the database in a String variable? Not pretty sure if it is possible at all. Hope can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM