ta.setText
是一个TextArea,我想在按下按钮后显示数据库中的所有数据。但是使用rs.get("name")
我只输出一个值,它始终是最后一个。如何从数据库中打印出整个表格,以便存储在那里的所有信息?
try { String newquery = "SELECT * FROM kunden";
java.sql.PreparedStatement ps = con.prepareStatement(newquery);
rs = ps.executeQuery(newquery);
while (rs.next()){
ta.setText(rs.getString("name"));
ta.setText(rs.getString("nachname"));
}
}// try
catch(Exception e1) {
JOptionPane.showMessageDialog(null, "fail");
}
}//actionperformed
答案 0 :(得分:2)
要么构建一个字符串,然后使用setText()
设置该字符串StringBuilder builder = new StringBuilder();
while (rs.next()) {
builder.append(rs.getString(“name”));
builder.append(“ “);
builder.append(rs.getString(“nachname”));
builder.append(“\n“);
}
ta.setText(builder.toString());
或者您使用TextArea
存在的append方法while (rs.next()) {
ta.append(rs.getString(“name”));
ta.append(“ “);
ta.append(rs.getString(“nachname”));
ta.append(“\n“);
}