Java SQL ResultSet Print printResults(ResultSet rst)

Here you can find the source of printResults(ResultSet rst)

Description

Helper method to print out the resultSet.

License

Open Source License

Parameter

Parameter Description
rst the resultSet returned from the query execution.

Exception

Parameter Description
SQLException if a database error occurs

Return

the number of tuples returned

Declaration

public static int printResults(ResultSet rst) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Main {
    /**//  ww  w .  ja  v a  2 s . c  o m
      * Helper method to print out the resultSet.
      * 
      * @param rst the resultSet returned from the query execution.
      * @return the number of tuples returned
      * @throws SQLException
      *             if a database error occurs
      */
    public static int printResults(ResultSet rst) throws SQLException {
        //       if(!rst.isBeforeFirst()){
        //          throw new SQLException("Result set not before first");
        //       }
        // Print out your results
        ResultSetMetaData meta = rst.getMetaData();
        int numColumns = meta.getColumnCount();
        System.out.print(meta.getColumnName(1));
        for (int j = 2; j <= meta.getColumnCount(); j++)
            System.out.print(", " + meta.getColumnName(j));
        System.out.println();

        int count = 0;
        while (rst.next()) {
            System.out.print(rst.getObject(1));
            for (int j = 2; j <= numColumns; j++)
                System.out.print(", " + rst.getObject(j));
            System.out.println();
            count++;
        }
        return count;
    }
}

Related

  1. printItems(ResultSet rs)
  2. printoutDB(ResultSet rs)
  3. printRecord(PrintWriter pw, ResultSet rs)
  4. printResult(ResultSet rs)
  5. printResults(final ResultSet results)
  6. printResultSet(List> rows, List colNames)
  7. printResultSet(ResultSet rs)
  8. printResultSet(ResultSet rs)
  9. printResultSet(ResultSet rs)