问题
Possible Duplicate:
I can’t get my JTable to show anything
I can show my Table but I can't get any data in the table, just the column name. My code looks like this.
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
java.sql.ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
and my kaldSql looks like this.
public ResultSet Hentalleordreliste(Connection con){
ResultSet Hentalleordreliste = null;
try {
PreparedStatement statement = con.prepareStatement("select varebestillinger.BestillingsID, " +
"varebestillinger.LeverandoerID, "+
"varebestillinger.BestillingsDato, varebestillinger.LeveringsDato, "+
"varebestillinger.BestillingsStatus, varebestillinger.ModtagetAf, "+
"varebestillinger.ModtagelsesDato, varebestillingsliste.Vare, " +
"varebestillingsliste.Antal from varebestillinger left outer join " +
"varebestillingsliste on ListeID = BestillingsID");
ResultSet rs = statement.executeQuery();
while(rs.next()) {
Hentalleordreliste = rs;
}
} catch (Exception e) {
e.printStackTrace();
}
return Hentalleordreliste;
}
and the GUI looks like this.
public GUIHentOrdre() {
try {
con = ks.connectNow();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResultSet rs = ks.Hentalleordreliste(con);
GUIOrdreHandler gh = new GUIOrdreHandler();
JTable table = null;
try {
table = new JTable(gh.buildTableModel(rs));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Handler handler = new Handler();
JPanel info = new JPanel();
info.setLayout(new BorderLayout());
LavBestilling = new JButton("Lav ny bestilling");
TableModel ClassOrVoidOrModelNameReturnsTableModel = null;
JScrollPane scroll = new JScrollPane(table);
info.setSize(1024, 768);
add(scroll, BorderLayout.CENTER);
add(LavBestilling, BorderLayout.NORTH);
LavBestilling.addActionListener(handler);
}
As told before, I can get the column name, I can create my table but cant get any data stored inside.
回答1:
Unless you're using a database that supports cursors, I would take a look hard look at
while(rs.next()) {
Hentalleordreliste = rs;
}
Which would suggest that you're exhausted the result BEFORE you've tried to populate the table model...
来源:https://stackoverflow.com/questions/13737417/i-get-the-jtable-but-no-data