我试图在JTable
(两列)中显示结果集的一部分,我只能设法看到一行。
JTable
位于现有GUI中,结果集将传递给GUI类中的方法。
当我做Vector size()
时它会返回一个。
这告诉我列表中只有一个条目。我应该看到4个条目我看到的条目是我期望的最后一个条目。
请参阅以下方法: 任何帮助将不胜感激。
public static void getResultSet(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
//JTable name is resultsTable includes Scrollpane
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = 2;
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
System.out.println("ColumnNames "+columnNames );
}
// data from the table
// Vector<Vector<Object>> data = new Vector<Vector<Object>>();
Vector<Vector<String>> data = new Vector<Vector<String>>();
while (resultSet.next()) {
Vector<String> vector = new Vector<String>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(resultSet.getString(columnIndex));
}
data.add(vector);
System.out.println("Vector Value = "+ data);
System.out.println("Vector Size =" + data.size()); //Returning 1 - Should see 4 entries
DefaultTableModel datamodel = new DefaultTableModel(data,columnNames);
resultsTable.setModel(datamodel);
}
答案 0 :(得分:3)
“这告诉我列表中只有一个条目。我应该看到4个条目我看到的条目是我期望的最后一个条目4。”
您每次迭代都会创建一个新的DefaultTableModel
。不要那样做。
DefaultTableModel datamodel = new DefaultTableModel(data,columnNames);
resultsTable.setModel(datamodel);
采取以上措施。而是先设置模型。然后只是循环中的.addRow(row)
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())
中逐个动态添加行。对于您在Vector
datamodal.addRow(vector)
<强>更新强>
就像我说的,我不知道你做错了什么,但我的代码没有错。这是一个测试程序
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();
}
}
}