gpt4 book ai didi

java - DefaultTableModel 上的类转换异常

转载 作者:行者123 更新时间:2023-12-01 18:44:09 24 4
gpt4 key购买 nike

我有3个类(class)

首先从数据库获取数据

第二个为共同作品

其他用于展示表。

public class TableContent {

private final Vector<String> headers;
private final Vector<Vector<String>> content;

public TableContent(final Vector<String> headers, final Vector<Vector<String>> content) {
this.headers = headers;
this.content = content;
}

public Vector<String> headers() {
return headers;
}

public Vector<Vector<String>> content() {
return content;
}
}

还有:

public class TableData {

public TableContent getData() {
Vector<String> headers = new Vector<String>();
Vector<Vector<String>> content = new Vector<Vector<String>>();

try {
Connection conn = DriverManager.getConnection("");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("Select * from table");

headers = buildHeaders(rs);
content = buildContent(rs);

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

return new TableContent(headers, content);
}

private Vector<String> buildHeaders(final ResultSet rs) throws SQLException {
Vector<String> headers = new Vector<String>();

int col = rs.getMetaData().getColumnCount();
for (int i = 1; i <= col; i++) {
headers.add(rs.getMetaData().getColumnName(i));
}
return headers;
}

private Vector<Vector<String>> buildContent(final ResultSet rs) throws SQLException {
Vector<Vector<String>> content = new Vector<Vector<String>>();

while (rs.next()) {
int col = rs.getMetaData().getColumnCount();
Vector<String> newRow = new Vector<String>(col);

for (int i = 1; i <= col; i++) {
newRow.add(rs.getString(i));
}
content.add(newRow);
}
return content;
}
}

还有:

public class TableGUI extends JFrame {

JTable table;
TableContent data = new TableData().getData();

public TableGUI() {
table = new JTable(new DefaultTableModel(data.headers(), data.content()));

add(new JScrollPane(table), BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 550);
setLocation(300, 80);
setVisible(true);
}

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

输出:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Vector
at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:268)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:224)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:159)
at TableGUI.<init>(TableGUI.java:11)
at TableGUI.main(TableGUI.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我的错误在哪里?

最佳答案

我强烈怀疑问题在于您以错误的顺序传递参数。第一个参数DefaultTableModel(Vector, Vector)意味着数据。这是第二个参数,它是列名称。所以试试这个:

table = new JTable(new DefaultTableModel(data.content(), data.headers()));

关于java - DefaultTableModel 上的类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18580267/

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