Java SQL ResultSet to convertToMap(Map metaData, ResultSet rs)

Here you can find the source of convertToMap(Map metaData, ResultSet rs)

Description

convert To Map

License

Apache License

Declaration

public static Map<String, Object> convertToMap(Map<String, Integer> metaData, ResultSet rs)
            throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.Assert;

public class Main {
    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public static Map<String, Object> convertToMap(Map<String, Integer> metaData, ResultSet rs)
            throws SQLException {
        fillRowNames(metaData, rs);//from  w  ww.jav a 2  s  . c  o m
        Map<String, Object> row = new HashMap<String, Object>();
        for (Map.Entry<String, Integer> entry : metaData.entrySet()) {
            Object value = null;
            if (Types.DATE == entry.getValue()) {
                value = formartDate(rs.getDate(entry.getKey()));
            } else if (Types.TIMESTAMP == entry.getValue()) {
                value = formartDateTime(rs.getTimestamp(entry.getKey()));
            } else if (Types.TIME == entry.getValue()) {
                value = formartTime(rs.getTime(entry.getKey()));
            } else {
                value = rs.getObject(entry.getKey());
            }
            row.put(entry.getKey(), value);
        }
        return row;
    }

    private static void fillRowNames(Map<String, Integer> metaData, ResultSet rs) throws SQLException {
        Assert.notNull(metaData, "The column names can't be null.");
        if (metaData.isEmpty()) {
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                metaData.put(rs.getMetaData().getColumnName(i), rs.getMetaData().getColumnType(i));
            }
        }
    }

    /**
     * pattern : yyyy-MM-dd
     * 
     * @param date
     * @return
     */
    public static String formartDate(Date date) {
        if (null == date) {
            return null;
        }
        return dateFormat.format(date);
    }

    /**
     * pattern : yyyy-MM-dd HH:mm:ss
     * 
     * @param date
     * @return
     */
    public static String formartDateTime(Date date) {
        if (null == date) {
            return null;
        }
        return dateTimeFormat.format(date);
    }

    /**
     * pattern : HH:mm:ss
     * 
     * @param date
     * @return
     */
    public static String formartTime(Date date) {
        if (null == date) {
            return null;
        }
        return timeFormat.format(date);
    }
}

Related

  1. convert(int sqlType, String type, ResultSet rs, String name)
  2. convert(ResultSet rs)
  3. convertResultSetToJSON(ResultSet resultSet)
  4. convertResultSetToMap(final ResultSet rs)
  5. resultSet2List(ResultSet rs)
  6. resultSet2Map(ResultSet rs)
  7. resultSetAsCSV(ResultSet rsh)
  8. resultSetCurrentData(ResultSet rs)