How to use the getMetaData method to create a ResultSetMeataData object:
Method | Description |
getColumnCount() | Returns the number of columns in this ResultSetMetaData object as an int |
getColumnName(intColumn) | Returns the name of the column as a string object. |
getColumnLabel(intColumn) | Returns the label of the column as a string object. |
getColumnType(intColumn) | Returns an int type that represents the SQL data type that's used to store the data in this column. |
getColumnTypeName(intColumn) | Returns a String object that indentifies the SQL data type that's used to store the data in this column. |
Example select statement
select UserID, FirstName, LastName, EMAILADDRESS , EMAILADDRESS as email from user where FirstName = ? or FirstName = ?"
Example code to extract ColumnName, ColumnTypeName and ColumnType
public static void displayColumns(ResultSet resultSet) throws SQLException { ResultSetMetaData meta = resultSet.getMetaData(); for (int i = 1; i <= meta.getColumnCount(); i++) { System.out.println(meta.getColumnName(i) + " — " + meta.getColumnLabel(i) + " — " + meta.getColumnTypeName(i) + " — " + meta.getColumnType(i)); } }
Results of displayColumns method
UserID — UserID — INT — 4 FirstName — FirstName — VARCHAR — 12 LastName — LastName — VARCHAR — 12 EmailAddress — EMAILADDRESS — VARCHAR — 12 EmailAddress — email — VARCHAR — 12
Description:
-
If you do not use the 'as' statement, your getColumnTypeName() will return the value of getColumnLabel()
-
You can use the fields of the Types class of the java.sql package to specify an int value for a SQL data type