* @throws UnsupportedEncodingException
*/
private String getStringFromResultSet(ResultSet rs) throws SQLException, UnsupportedEncodingException {
ResultSetMetaData meta = rs.getMetaData();
StrBuilder sb = new StrBuilder();
int numColumns = meta.getColumnCount();
for (int i = 1; i <= numColumns; i++) {
sb.append(meta.getColumnName(i));
if (i==numColumns){
sb.append('\n');
} else {
sb.append('\t');
}
}
JMeterVariables jmvars = null;
String varnames[] = getVariableNames().split(COMMA);
if (varnames.length > 0){
jmvars = getThreadContext().getVariables();
}
int j = 0;
while (rs.next()) {
j++;
for (int i = 1; i <= numColumns; i++) {
Object o = rs.getObject(i);
if (o instanceof byte[]) {
o = new String((byte[]) o, ENCODING);
}
sb.append(o);
if (i==numColumns){
sb.append('\n');
} else {
sb.append('\t');
}
if (jmvars != null && i <= varnames.length) {
String name = varnames[i - 1].trim();
if (name.length()>0){ // Save the value in the variable if present
jmvars.put(name+UNDERSCORE+j, o == null ? null : o.toString());
}
}
}
}
// Remove any additional values from previous sample
for(int i=0; i < varnames.length; i++){
String name = varnames[i].trim();
if (name.length()>0 && jmvars != null){
final String varCount = name+"_#"; // $NON-NLS-1$
// Get the previous count
String prevCount = jmvars.get(varCount);
if (prevCount != null){
int prev = Integer.parseInt(prevCount);
for (int n=j+1; n <= prev; n++ ){
jmvars.remove(name+UNDERSCORE+n);
}
}
jmvars.put(varCount, Integer.toString(j)); // save the current count
}
}
return sb.toString();
}