简体   繁体   English

如何使用Java将Jtable中的选定复选框项保存到mysql?

[英]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. 在我的项目中,我想安排课程,这是我的GUI。 PIC

Can someone guide me, how to save the checked value into the mysql database? 有人可以指导我,如何将检查的值保存到mysql数据库中?

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? 像字符串一样将其保存在数据库中的String变量中? Not pretty sure if it is possible at all. 不太确定是否有可能。 Hope can help. 希望能帮上忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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