如何一次从JTable删除数据库中的多行

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

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

staffTable1 是变量的名称 JTable

ShowStaffInStaffTable()JTable通过使用Getter Setter类从MySql导入数据来更新我的行和列的函数

这是我尝试过的代码:

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)中删除多个选定的行:

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

}

});

}

}

显然,您将需要修改代码以从数据库中删除该行。

以上是 如何一次从JTable删除数据库中的多行 的全部内容, 来源链接: utcz.com/qa/399359.html

回到顶部