gpt4 book ai didi

java - 从数据库到JTable

转载 作者:行者123 更新时间:2023-11-29 08:23:30 25 4
gpt4 key购买 nike

我的代码正确地将数据从数据库显示到表中但是当我想删除一行时,行是从数据库和表模型中删除的,

但是发生 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1 异常:

public class Model1 extends DefaultTableModel {

public Model1(Vector<Vector<String>> data, Vector<String> column) {
super(data, column);
}

public void removeRow(int rowToModel, Object rowId) {
JDBC1 jdbc1 = new JDBC1();

try {
Connection con;
PreparedStatement ps = null;
con = DriverManager.getConnection(...);

ps = con.prepareStatement("delete from table where id=?");
ps.setObject(1, rowId);

if (ps.executeUpdate() == 1) {
jdbc1.getColumns().remove(rowToModel);
fireTableRowsDeleted(rowToModel, rowToModel);
}

} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}

我的 JDBC 类:

public class JDBC1 {

private Vector<String> columns = new Vector<String>();
Vector<Vector<String>> data = new Vector<Vector<String>>();

Connection con;
Statement statement;
ResultSet result;
String query = "Select * from table";

public Vector<String> getColumn() throws SQLException {

con = DriverManager.getConnection(...);
statement = con.createStatement();
result = statement.executeQuery(query);

int c = result.getMetaData().getColumnCount();
for (int i = 1; i <= c; i++) {
getColumns().add(result.getMetaData().getColumnName(i));
}
return getColumns();
}

public Vector<Vector<String>> getData() throws SQLException{
con = DriverManager.getConnection(...);
statement = con.createStatement();
result = statement.executeQuery(query);
int c = result.getMetaData().getColumnCount();

while (result.next()) {
Vector<String> newRow = new Vector<String>(c);

for (int i = 1; i <= c; i++) {
newRow.add(result.getString(i));
}
data.add(newRow);
}
return data;

}

public Vector<String> getColumns() {
return columns;
}

public void setColumns(Vector<String> columns) {
this.columns = columns;
}
}

GUI 类:

public class GUI1 extends JFrame implements ActionListener {

JTable table;
Model1 model1;
JDBC1 jdbc1;
JButton dellButton;

public GUI1() {
try {
jdbc1 = new JDBC1();
model1 = new Model1(jdbc1.getData(), jdbc1.getColumn());
table = new JTable(model1);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
add(new JScrollPane(table), BorderLayout.CENTER);
add(dellButton() , BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 550);
setLocation(250, 85);
setVisible(true);
}

public static void main(String[] args) {
new GUI1();
}

public JPanel dellButton(){
JPanel panel = new JPanel();
dellButton = new JButton("Delete Row");
dellButton.addActionListener(this);
panel.add(dellButton);
return panel;
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == dellButton) {
dellAction();
}
}

public void dellAction() {
if (table.getSelectedRow() > -1) {
int rowToDelete = table.getSelectedRow();
int rowToModel = table.convertRowIndexToModel(rowToDelete);
Object rowId = table.getValueAt(table.getSelectedRow(), 0);
model1.removeRow(rowToModel, rowId);

} else JOptionPane.showMessageDialog(null, "Select Row");
}
}

输出:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
at java.util.Vector.remove(Vector.java:827)
at test.Model1.removeRow(Model1.java:29)
at test.GUI1.dellAction(GUI1.java:76)
at test.GUI1.actionPerformed(GUI1.java:67)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

最佳答案

您似乎将列信息与数据混合在一起。

列描述了水平显示在表格中的列,其中行描述了实际数据(结构适合列)...

而不是...

jdbc1.getColumns().remove(rowToModel);

尝试...

removeRow(rowToModel);

这将使您无需自己调用 fireTableRowsDeleted...

观察

就个人而言,我会尝试限制 dellAction 方法删除行所需的信息量。

我的意思是,该方法已经知道要删除的行,它不应该也查找 ID。这表明 dellAction 方法了解 View 的结构,该结构将来可能会发生变化。

相反,我个人会从 removeRow 方法本身的行数据中提取 ID。这使得模型有机会根据您的需求对 ID 进行建模或不建模...

恕我直言

关于java - 从数据库到JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18624312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com