Revision as of 16:41, 22 March 2011 by Han84 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
/**
 * Print the ResultSet object.
 * @param rs the ResultSet object
 */
public static void sqlResultSetPrint(java.sql.ResultSet rs) {
	try {
		java.sql.ResultSetMetaData metaData = rs.getMetaData();
		int columnCount = metaData.getColumnCount();
		for (int i = 1; i <= columnCount; i++) {
			if (i > 1) {
				System.out.print(", ");
			}
			System.out.print(metaData.getColumnLabel(columnCount));
		}
		System.out.println();
		while (rs.next()) {
			for (int i = 0; i <= columnCount; i++) {
				if (i > 1) {
					System.out.print(", ");
				}
				System.out.print(rs.getString(i));
			}
			System.out.println();
		}
		// This is required for further action on the result set.
		rs.beforeFirst();
	} catch (java.sql.SQLException ex) {
		Ts.printErr(ex);
	}
}
 
public static void printErr(java.sql.SQLException ex) {
	System.out.println("SQL State: " + ex.getSQLState());
	System.out.println("Error Code: " + ex.getErrorCode());
	Ts.printErr((java.lang.Exception) ex);
}
 
public static void printErr(java.lang.Exception ex) {
	ex.printStackTrace();
	System.exit(-1);
}


Back to JavaHowTo

Alumni Liaison

Prof. Math. Ohio State and Associate Dean
Outstanding Alumnus Purdue Math 2008

Jeff McNeal