从 ResultSet 获取结果的问题 [英] Troubles getting results from ResultSet
问题描述
如何让 var 包含我从结果集中获得的所有记录?
How can I have a var which contains all the records I get from a resultset?
到目前为止我有这个代码:
So far I have this code:
while (rs.next()) {
for (int i = 1; i <= columnCount; i++) {
String resultado = "";
resultado = rs.getString(i);
columnValue += resultado;
}
jTextPane2.setText(jTextPane2.getText() + columnValue + ", ");
}
我希望当 resultado
从 rs.getString(i)
获取值时,填充 var columnValue
以便我有一个var 应该有我从 rs 得到的所有记录,但它不工作.有什么帮助吗?
I want that when resultado
gets the value from the rs.getString(i)
, fills the var columnValue
so that I have a var which SHOULD have all the records I get from the rs, but is not working. Any help?
我得到的结果是:
(id_tlf, cod_area)
(id_tlf, cod_area)
1+58、1+582+104、1+582+1043+60
1+58, 1+582+104, 1+582+1043+60
如您所见,前 2 个结果在每一行中重复
so as you see, the first 2 results repeat in every row
推荐答案
请更喜欢 StringBuilder
来创建大量 String
临时值(它们会污染内部缓存事物).接下来,您不需要将每一列存储在另一个局部变量中.基本上,我会做类似的事情
Please prefer a StringBuilder
to creating lots of String
temporary values (they pollute the intern cache for one thing). Next, you don't need to store each column in another local variable. Basically, I would do something like
StringBuilder sb = new StringBuilder();
while (rs.next()) {
for (int i = 1; i <= columnCount; i++) {
if (i != 1) {
sb.append(", ");
}
sb.append(rs.getString(i));
}
sb.append(System.lineSeparator());
}
jTextPane2.setText(sb.toString());
注意上面清除了jTextPane2
,如果你打算追加,那么你可以将第一行更改为类似
Note the above clears jTextPane2
, if you intend to append then you could change the first line to to something like
StringBuilder sb = new StringBuilder(jTextPane2.getText());
sb.append(System.lineSeparator()); // <-- start the next line... and then iterate rs
这篇关于从 ResultSet 获取结果的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!