Java SQL ResultSet to resultSetCurrentData(ResultSet rs)

Here you can find the source of resultSetCurrentData(ResultSet rs)

Description

result Set Current Data

License

Open Source License

Declaration

public static String resultSetCurrentData(ResultSet rs) throws SQLException 

Method Source Code

//package com.java2s;
/*//w  w  w. jav  a2 s  .  co  m
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */

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

public class Main {
    private static final int MAX_WIDTH = 20;

    public static String resultSetCurrentData(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        StringBuilder column = new StringBuilder();

        int columns = metaData.getColumnCount();

        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= columns; i++) {
            column.setLength(0);
            if (i > 1) {
                sb.append(" | ");
            }
            sb.append(trimOrPad(column.append(rs.getString(i))));
        }
        return sb.toString();
    }

    private static StringBuilder trimOrPad(StringBuilder buffer) {
        if (buffer.length() > MAX_WIDTH) {
            buffer.setLength(MAX_WIDTH - 1);
            buffer.append("~");
        } else {
            for (int i = buffer.length(); i < MAX_WIDTH; i++) {
                buffer.append(" ");
            }
        }
        return buffer;
    }
}

Related

  1. convertResultSetToMap(final ResultSet rs)
  2. convertToMap(Map metaData, ResultSet rs)
  3. resultSet2List(ResultSet rs)
  4. resultSet2Map(ResultSet rs)
  5. resultSetAsCSV(ResultSet rsh)
  6. resultSetParseVO(ResultSet rs, Class cls)
  7. resultSetToArrayList(ResultSet rs)
  8. resultSetToHtmlTable(java.sql.ResultSet rs)
  9. resultSetToMap(Map mappedValues, java.sql.ResultSet rs, int key, int value)