本文介绍了如何一次从JTable,数据库中删除多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一次从JTable中删除几行,但我一次只能删除一行。 JTable一次只允许一个选择吗?如果我想通过选择删除多行,java允许我们这样做吗?即使我选择多行,给定代码也只能一次删除一行。

I want to delete several rows from JTable at once but I'm only able to delete only one row at a time. Does JTable only allow one selection at a time? If I want to delete multiple rows by selecting them does java allow us to do so? The given code is only able to delete one row at a time even if I select multiple rows.

DBConnection 是一个连接到MySql数据库的类。

DBConnection is a Class to connect to MySql Database.

staffTable1 是一个变量名称 JTable

ShowStaffInStaffTable()是一个更新我的 JTable的函数使用Getter Setter类从MySql导入数据的行和列

ShowStaffInStaffTable() is a Function to update my JTable rows and columns by importing data from MySql using Getter Setter Class

这是我尝试过的代码:

int row = staffTable1.getSelectedRow();
DefaultTableModel model = (DefaultTableModel) staffTable1.getModel();

String selected = model.getValueAt(row, 9).toString();

if (row >= 0) {
    try {
        DBConnection DB12 = new DBConnection();
        Connection conn = DB12.Connect();
        String sql = "delete from staff where Contact_No= ?";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, selected);
        stmt.executeUpdate();
        form12.ShowStaffInStaffTable();
    } catch (Exception w) {
        JOptionPane.showMessageDialog(this, "Connection Error!"+w);
        w.printStackTrace();
    }
}


推荐答案

下面显示了如何从JTable(和JList)中删除多个选定的行:

The following shows how to delete multiple selected rows from a JTable (and JList):

import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;

public class ItemDeletion extends JPanel
{
    private JList<String> list;
    private JTable table;

    public ItemDeletion()
    {
        setLayout( new BorderLayout(5, 5) );

        String[] items =
        {
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine",
            "Ten"
        };

        //  Add the list

        DefaultListModel<String> listModel = new DefaultListModel<String>();

        for (String item: items)
            listModel.addElement( item );

        list = new JList<String>( listModel );


        JButton listDelete = new JButton( "Delete From List" );
        listDelete.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                DefaultListModel model = (DefaultListModel)list.getModel();
                int row = list.getSelectedIndex();

                while (row != -1)
                {
                    model.removeElementAt( row );
                    row = list.getSelectedIndex();
                }
            }
        });

        JPanel listPanel = new JPanel( new BorderLayout(5, 5) );
        listPanel.add(new JScrollPane( list ), BorderLayout.CENTER);
        listPanel.add(listDelete, BorderLayout.PAGE_END);

        //  Add the table

        DefaultTableModel tableModel = new DefaultTableModel(0, 1);
        List<String> tableItems = Arrays.asList( items );
        Collections.shuffle( tableItems );

        for (String item: tableItems)
        {
            System.out.println( item );
            tableModel.addRow( new String[]{item} );
        }

        table = new JTable( tableModel );

        table.setAutoCreateRowSorter(true);
        ((DefaultRowSorter)table.getRowSorter()).toggleSortOrder(0);

        JButton tableDelete = new JButton( "Delete From Table" );
        tableDelete.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                DefaultTableModel model = (DefaultTableModel)table.getModel();
                int row = table.getSelectedRow();

                while (row != -1)
                {
                    int modelRow = table.convertRowIndexToModel( row );
                    model.removeRow( modelRow );
                    row = table.getSelectedRow();
                }
            }
        });

        JPanel tablePanel = new JPanel( new BorderLayout(5, 5) );
        tablePanel.add(new JScrollPane( table ), BorderLayout.CENTER);
        tablePanel.add(tableDelete, BorderLayout.PAGE_END);

        add(listPanel, BorderLayout.LINE_START);
        add(tablePanel, BorderLayout.LINE_END);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Multiple Item Deletion");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new ItemDeletion(), BorderLayout.NORTH);
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

显然你需要修改代码才能删除也是数据库中的一行。

Obviously you will need to modify the code to delete the row from the database as well.

这篇关于如何一次从JTable,数据库中删除多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:52