Description
Get the first row of the ResultSet as Map, converting the column names to upper case.
License
Apache License
Parameter
Parameter | Description |
---|
rs | a parameter |
Exception
Parameter | Description |
---|
SQLException | an exception |
Return
the {columnLabel, value} mappings
Declaration
public static Map<String, Object> getResultsAsMap(ResultSet rs) throws SQLException
Method Source Code
//package com.java2s;
/*******************************************************************************
* Copyright 2011 Adrian Cristian Ionescu
* //from w w w .java2 s. c o m
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Main {
/**
* Get the first row of the ResultSet as Map, converting the column names to upper case.
*
* @param rs
* @return the {columnLabel, value} mappings
* @throws SQLException
*/
public static Map<String, Object> getResultsAsMap(ResultSet rs) throws SQLException {
Map<String, Object> result = new TreeMap<String, Object>();
ResultSetMetaData meta = rs.getMetaData();
int count = meta.getColumnCount();
List<String> colNames = new ArrayList<String>();
for (int i = 1; i <= count; i++) {
// colNames.add(meta.getColumnName(i).toLowerCase());
colNames.add(meta.getColumnLabel(i).toUpperCase());
}
if (rs.next()) {
for (int i = 1; i <= count; i++) {
int colType = meta.getColumnType(i);
if (colType == Types.DATE || colType == Types.TIMESTAMP) {
result.put(colNames.get(i - 1), rs.getTimestamp(i));
} else {
result.put(colNames.get(i - 1), rs.getObject(i));
}
}
}
return result;
}
}
Related
- getRecordsFromResultSet(ResultSet rs)
- getResult(ResultSet rs)
- getResultArray(ResultSet resultSet, int size, java.util.Date startDate)
- getResultByMap(ResultSet rs)
- getResults(ResultSet result)
- getResultSet(Connection c, String query)
- getResultSet(Connection conn, String query)
- getResultSet(Connection connection, String query)
- getResultSet(String sql)