geeky · java

Spring: check if a column exist using resultset

If you have a strange requirement in the rowMapper to check if a column exists or not, you could try to catch a SQLException of invalid column name or you could try:

public static boolean doesColumnExist(String columnName, ResultSet rs) throws SQLException{
	ResultSetMetaData meta = rs.getMetaData();
	int numCol = meta.getColumnCount();
	for (int i = 1; i <= numCol; i++) {
		if(meta.getColumnName(i).equalsIgnoreCase(columnName)) {
			return true;

		}

	}
	return false;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s