更新时间:2023-12-03 11:22:16
这是告诉我列表中只有一个条目.我应该看到4个条目.我看到的条目是我期望的最后一个条目,应该是4."
"This is telling me there is only one entry in the list. I should be seeing at 4 entries The entry I am seeing is the last one of what I expect would be 4."
您将在每次迭代中创建一个新的DefaultTableModel
.不要那样做.
You're creating a new DefaultTableModel
each iteration. Don't do that.
DefaultTableModel datamodel = new DefaultTableModel(data,columnNames);
resultsTable.setModel(datamodel);
将以上内容带出.而是先设置模型.然后只需在循环中.addRow(row)
Take the above out. Instead, set the model first. Then just .addRow(row)
in the loop
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
System.out.println("ColumnNames "+columnNames );
}
DefaultTableModel datamodel = new DefaultTableModel(columnNames, 0);
table.setModel(datamodel);
while (resultSet.next()) {
Vector<String> vector = new Vector<String>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(resultSet.getString(columnIndex));
}
datamodel.addRow(vector);
}
DefaultTableModel(columnNames, 0)
的构造函数,其中0
是初始行数.因此,您要做的就是在while(rs.next())
中动态地逐行添加行.对于每行数据,您只需输入datamodal.addRow(vector)
The constructor for the DefaultTableModel(columnNames, 0)
where 0
is the number of initial rows. So all you need to do is add rows dynamically one by one in the while(rs.next())
. For each row of data you get in a Vector
just datamodal.addRow(vector)
更新
就像我说的那样,我不知道您在做什么错,但是我的代码没有错.这是一个测试程序
Like I said, I don't know what you're doing wrong, but there's nothing wrong with my code. Here's a test program
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TestTableModel {
private ResultSet resultSet = null;
public TestTableModel() throws SQLException {
initDB();
JTable table = new JTable(getModel(resultSet));
JScrollPane scroll = new JScrollPane(table);
JFrame frame = new JFrame("Test Table Model");
frame.add(scroll);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
TestTableModel testTableModel = new TestTableModel();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
}
private DefaultTableModel getModel(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
Vector<String> columnNames = new Vector<>();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
System.out.println("ColumnNames " + columnNames);
}
DefaultTableModel dataModel = new DefaultTableModel(columnNames, 0);
while (resultSet.next()) {
Vector<String> vector = new Vector<>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(resultSet.getString(columnIndex));
}
dataModel.addRow(vector);
}
return dataModel;
}
private void initDB() throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/...", "...", "...");
ps = conn.prepareStatement("SELECT * FROM city");
resultSet = ps.executeQuery();
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
}
}
}