lakshi lakshi - 3 months ago 27x
SQL Question

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.

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) {

String sql="insert into batch (course_code,batch_number,time)values(?,?,?)";


String value1=course_code.getSelectedItem().toString();

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

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

/* for(int i=0;i<time_table.getModel().getRowCount();i++)
if ((Boolean) time_table.getModel().getValueAt(i,0))

} */

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

I want something like this. enter image description here


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:


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.