[英]How to get query resultset into a string
In Java, after executing a query say I got a result set like: 在Java中,执行查询后说我得到了如下结果集:
bat 20 10
fat 20 5
cat 10 25
I want this resultset in a string such that the string will be: 我希望此结果集中在字符串中,以便该字符串为:
bat,20,10 |fat,20,5 |cat,10,25
I am confused. 我很困惑。 How can I do that in Java?
如何用Java做到这一点?
ResultSet rs = ...
StringBuilder b = new StringBuilder();
while(rs.next()) {
String s = rs.getString(1);
int n1 = rs.getInt(2);
int n2 = rs.getInt(3);
b.append(s);
b.append(",");
b.append(n1);
b.append(",");
b.append(n2);
b.append("|");
}
while iterating through the resultset you can append its contents to a string ..right.? 在遍历结果集的同时,您可以将其内容附加到字符串..right。?
String finalStr = "";
while(resultSet.next()){
finalStr+=resultSet.getString(1)+",";
finalStr+=resultSet.getInt(2).toString()+",";
finalStr+=resultSet.getInt(3).toString();
finalStr+="|";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.