get Current Row from ResultSet - Java java.sql

Java examples for java.sql:ResultSet

Description

get Current Row from ResultSet

Demo Code


//package com.java2s;
import java.sql.*;

import java.util.Vector;

public class Main {
    public static Vector getCurrentRow(ResultSet rs, ResultSetMetaData rsmd) {
        Vector<String> currentRow = new Vector<String>();
        int count = 0;
        try {//from   www. j  a v a  2  s. co  m
            count = rsmd.getColumnCount();
            for (int i = 1; i <= count; ++i) {
                try {
                    if (rs.getObject(i) == null) {
                        currentRow.addElement("{null}");
                    } else {
                        setColumnValue(currentRow, rsmd.getColumnType(i),
                                rs, i);
                    }
                } catch (SQLException qqqq) {
                    currentRow.addElement("{error}");
                }
            }
        } catch (SQLException eee) {
            eee.printStackTrace();
        }
        return currentRow;
    }

    public static void setColumnValue(Vector<String> currentRow,
            int columnType, ResultSet resultSet, int index) {
        int dataType = columnType;
        try {
            switch (dataType) {
            case Types.CHAR:
                currentRow.addElement(resultSet.getString(index));
                break;
            case Types.VARCHAR:
                currentRow.addElement(resultSet.getString(index));
                break;
            case Types.DATE:
                currentRow.addElement(resultSet.getDate(index).toString());
                break;
            case Types.TIME:
                currentRow.addElement(resultSet.getTime(index).toString());
                break;
            case Types.TIMESTAMP:
                currentRow.addElement(resultSet.getTimestamp(index)
                        .toString());
                break;
            case Types.LONGVARCHAR:
            case Types.CLOB:
                currentRow.addElement(resultSet.getString(index));
                break;
            case Types.LONGVARBINARY:
            case Types.VARBINARY:
            case Types.BINARY:
            case Types.INTEGER:
                currentRow.addElement(String.valueOf(resultSet
                        .getInt(index)));
                break;
            case Types.DECIMAL:
                currentRow.addElement(resultSet.getBigDecimal(index)
                        .stripTrailingZeros().toPlainString());
                break;
            case Types.DOUBLE:
                currentRow.addElement(String.valueOf(resultSet
                        .getDouble(index)));
                break;
            case Types.FLOAT:
                currentRow.addElement(String.valueOf(resultSet
                        .getFloat(index)));
                break;
            case Types.BLOB:
                currentRow.addElement(resultSet.getBlob(index).toString());
                break;
            default:
                currentRow
                        .addElement(resultSet.getObject(index).toString());
                break;
            }
        } catch (Exception e) {
            try {
                currentRow.addElement(resultSet.getString(index));
            } catch (SQLException ee) {
                currentRow.addElement("{error}");
            }
        }
    }
}

Related Tutorials