All pastes #951724 Raw Edit

Something

public text v1 · immutable
·published 2008-03-21 16:49 UTC
rendered paste body
	public void displayResultSet(ResultSet inputResultSet) {					// displays the input resultset type object in table form
		ResultSetMetaDeta metaData = inputResultSet.getMetaData();
		int columnCount = metaData.getColumnCount();
		int max[columnCount];													// array to contain the maximum length of each column
		ResultSet temp = inputResultSet;
		for (i = 0; i < columnCount; i++) {
			max[i] = metaData.getColumnName(i+1).length();						// initial values set to the column names' length
		}
		do {
			for (i = 0; i < columnCount; i++) {
				if (inputResultSet.getString(i+1).length() < max[i]) {
					max[i] = inputResultSet.getString(i+1).length();			// if found a longer string, insert its length into the array
				}
			}
		} while (temp.next());													// we shouldn't have false on this at the first time
		for (i = 0; i < columnCount; i++) {										// display of the columns' name
			result.append(addSpaces(metaData.ColumnName(i+1), max[i]));
			if ( i + 1 != columnCount ) result.append("|");
		}
		result.append("\n");
		for (i = 0; i < columnCount; i++) {										// some design on the display
			for (j = 0; j < max[i]; j++) {
				result.append("-");
			}
			if ( i + 1 != columnCount ) result.append("|");
		}
		result.append("\n");
		temp = inputResultSet;													// re-initializing temp, it already reached end in the second loop
		do {
			for (i = 0; i < columnCount; i++) {
				result.append(addSpaces(temp.getString(i+1), max[i]));
				if ( i + 1 != columnCount ) result.append("|");
			}
			result.append("\n");
		} while (temp.next());
		temp.close();
	}