插入,更新,删除具有mysql数据的jtable行 [英] Insert,Update,Delete rows of jtable having mysql data

查看:53
本文介绍了插入,更新,删除具有mysql数据的jtable行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此类显示了一个雇员列表,该列表是mysql中的一个表,它可以正常工作,要点是我想以这种方式动态地进行插入,更新或删除:

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. JTable具有选择特定行中的单元格的功能,只需用鼠标光标单击它即可,
  2. 我想利用这一事实来删除值,并在更新的情况下插入另一个值,或者如果删除则删除整个行,或者插入新行,
  3. 所有这些一旦完成的操作将被保存在数据库表中.我可以按照我刚才描述的去做吗?

.

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

推荐答案

是的,可以.我的挥杆能力不是很好,但是你可以做到.将需要大量的工作,实现大量的侦听器,更改和重新加载表模型,在数据更改后重新绘制表...非常困难.但是,当您说删除值并在更新的情况下插入另一个值,或者在删除的情况下删除整个行,或者插入新行"时,我知道这不会通过单击单元格"来完成.您应该在该行上实施更多控件,以了解是否要删除该行或在上/下插入一个以及所有所需的操作.希望这对您有所帮助,因为您刚刚询问是否可以这样做.您认为使用单元格快捷键在表格上打开新框架并编辑行会容易得多吗?

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?

这篇关于插入,更新,删除具有mysql数据的jtable行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆