Generic API which display data in resultset along with column names - Java java.sql

Java examples for java.sql:ResultSet

Description

Generic API which display data in resultset along with column names

Demo Code


//package com.java2s;

import java.sql.*;

public class Main {
    /**//from w w  w. j  a  v a2s  . c om
     * Generic API which display data in resultset along with column names
     * @param rs
     * @throws SQLException 
     */
    public static void displayData(ResultSet rs) throws SQLException {
        ResultSetMetaData resultSetMetaData = rs.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();

        // Prints Column names
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            System.out.print(resultSetMetaData.getColumnLabel(columnIndex));
            System.out.print("  ");
        }

        // Prints data
        while (rs.next()) {
            System.out.println();

            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                System.out.print(rs.getString(columnIndex));
                System.out.print("  ");
            }
        }
    }
}

Related Tutorials