To accomplish this you will need to make two passes through the ResultSet. In the first pass you need to figure out the widest string to be display in each column. On the second pass you need to pad output with spaces to ensure that all items have the same width. REMEMBER: The column name may be the widest value.
To determine column header names and other useful metadata about the query result you will make use of a ResultSetMetaData object.
Since you will be making two passes through the ResultSet you need to make the result set scrollable. See the paragraphs of documentation at the top of https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html for information on how to do that.
I recommend (but don't require) that you use an object-oriented design for this program. In particular it would be helpful to have a class to manage a database connection whose constructor will connect to the database. Then the connection object (which will be a classwide attribute) will available to your displayQuery() method without have to re-establish a connection to the database every time a query is issued.