Insert,Update,Delete rows of jtable having mysql data

允我心安 提交于 2020-01-07 00:35:09

问题


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();
 }
}

回答1:


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?



来源:https://stackoverflow.com/questions/21464638/insert-update-delete-rows-of-jtable-having-mysql-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!