result Set To String Format - Java java.sql

Java examples for java.sql:ResultSet

Description

result Set To String Format

Demo Code

/*/*from   www  . j av a  2 s. c  o m*/
 * codjo.net
 *
 * Common Apache License 2.0
 */
//package com.java2s;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Main {
    public static String resultSetToStringFormat(ResultSet rs,
            String separator, String quote, boolean column)
            throws SQLException {
        StringBuilder sb = new StringBuilder();
        ResultSetMetaData metadata = rs.getMetaData();
        int colmumnCount = metadata.getColumnCount();
        if (column) {
            for (int i = 1; i <= colmumnCount; i++) {
                sb.append(quote).append(metadata.getColumnName(i))
                        .append(quote);
                if (i + 1 <= colmumnCount) {
                    sb.append(separator);
                }
            }
            sb.append("\n");
        }
        while (rs.next()) {
            for (int i = 1; i <= colmumnCount; i++) {
                sb.append(quote).append(rs.getObject(i)).append(quote);
                if (i + 1 <= colmumnCount) {
                    sb.append(separator);
                }
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}

Related Tutorials