Search code examples
resultsetspring-jdbc

Extracting ResultSetMetaData from Spring JdbcTemplate


I am trying to fetch the resultsetmeta data using Spring jdbc template. It works fine if there is atleast one row returned.

The problem arises when there is no rows returned i.e. an empty resultSet.

I have tried a lot and still stuck up with the same. If there is any solution to this, please help me with this.

Also, I found ResultSetWrappingSqlRowSetMetaData class in spring. Is this of some use in my context?

Thanks for the help.


Solution

  • Finally I found the answer to my question. Below is the code:

    template.query(builder.toString(),new ResultSetExtractor<Integer>() {
    
        @Override
        public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
    
            ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        for(int i = 1 ; i <= columnCount ; i++){
            SQLColumn column = new SQLColumn();
        column.setName(rsmd.getColumnName(i));
        column.setAutoIncrement(rsmd.isAutoIncrement(i));
        column.setType(rsmd.getColumnTypeName(i));
        column.setTypeCode(rsmd.getColumnType(i));
        column.setTableName(sqlTable.getName().toUpperCase());
        columns.add(column);
        }
    
        return columnCount;
    }
    });
    

    For a detailed explanation you can visit here