Result Sets
You can use ResultSetMetaData to retrieve metadata about the results of a query.
The query can contain any of the following:
- Aliases
- Fully qualified names
- Generated columns
You can instantiate a ResultSetMetaData object by invoking the getMetaData method of the Statement class. A ResultSetMetaData instance is populated with data after the statement has been executed. The following query prints out the columns in the result of the query:
String connectionString = "jdbc:postgresql:User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5432"; Connection conn = DriverManager.getConnection(connectionString); PreparedStatement pstmt = conn.prepareStatement("SELECT ShipName, ShipCity AS My_ShipCity, GETDATE() FROM \"postgres\".\"schema01\".Orders WHERE ShipCountry = 'USA'"); pstmt.executeQuery(); ResultSetMetaData rs = pstmt.getMetaData(); for(int i=1;i<=rs.getColumnCount();i++) { System.out.println(rs.getColumnName(i)); }