java - Java-Output all the columns and rows of a ResultSet
问题描述
ta.setText
is a TextArea where I want to show all my data from the database, after a button click. But with rs.get("name")
I just output one value and it is always the last. How can I print out the whole table from the database, so all the information which are stored there?
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
解决方案
您可以构建一个字符串,然后使用 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“);
}
推荐阅读
- r - 使用单个元素中的数值编辑和过滤 R 中的列表
- bash - grep for duration=N while N 比 X 长,这句话的位置在行之间变化
- python - 如何使用opencv在图像中堆叠黑色像素区域
- javascript - 操作无法完成: - 创建/更新:未设置必填字段。奥多 14
- python - 安装柯南包没有要求?
- node.js - 带 POST 和不带 POST 的电报 sendMessage
- pagespeed-insights - 不同站点的不同字段数据,有人知道为什么吗?
- javascript - 如何弹出隐藏溢出的祖先之外的嵌套子组件?
- c - 我分配的内存数组上出现意外的数组长度
- powershell - Active Directory ADUser whenCreated 属性