OiRc
OiRc

Reputation: 1622

Insert,Update,Delete rows of jtable having mysql data

This class shows a list of employees that is a table in mysql, it works, the point is that I want to do an insert or update or delete dynamically in this way:

  1. The JTable has the function to select the cell in a specific row, just by clicking on it with the mouse cursor,
  2. i would like to take advantage of this fact to remove the value and insert another in the case of update, or remove the entire row if the delete, or insert a new row,
  3. all these operations once made ​​will be saved in the database table. I can do what I just described?

.

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTable;
import DataBaseConnectionSingleton.Connection;
import DataBaseConnectionSingleton.CreationStatement;
import net.proteanit.sql.DbUtils;

public class ShowEmployee {

public JFrame frame=new JFrame();
JTable Table = new JTable();
JButton b = new JButton();

public ShowEmployee() {
    showEmployee();
}

public void showEmployee() {

    try {
        Connection.getConnectionInstance();
        Statement st = CreationStatement.getCreationStatementInstance();
        ResultSet rs = st.executeQuery("select * from employee");
        frame.getContentPane().setLayout(null);
        Table.setBounds(0, 0, 405, 361);
        Table.setModel(DbUtils.resultSetToTableModel(rs));
        Table.setAutoCreateRowSorter(true);
        frame.getContentPane().add(Table);
        frame.setTitle("EMPLOYEES'S LIST");
        frame.setSize(650,400);
        frame.setLocation(265,10);
        frame.setVisible(true);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    new ShowEmployee();
 }
}

Upvotes: 0

Views: 1581

Answers (1)

Thrash Bean
Thrash Bean

Reputation: 658

Yes, you can. I'm not very good in swing but you can do this. Will be lot of work, implementing lot of listeners, changing and reloading table model, repainting the table after data change... very hard. But when you say " to remove the value and insert another in the case of update, or remove the entire row if the delete, or insert a new row" I understand that this wont be done by "clicking a cell". You should implement more controls over the row to know if you want to delete it or insert one above/below and all the operations you want. Hope this helps you, since you just asked if you can do that. Dont you think will be much easier on cell clik to open a new frame over the table and edit the row?

Upvotes: 1

Related Questions