Here you can find the source of query(Connection connection, String sql, boolean isClose, Object... params)
public static List<Map<String, Object>> query(Connection connection, String sql, boolean isClose, Object... params)
//package com.java2s; //License from project: Apache License import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static List<Map<String, Object>> query(Connection connection, String sql, boolean isClose, Object... params) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); PreparedStatement prepareStatement = null; try {// www . j av a2 s. c o m sql = sql.replace("<", "<"); sql = sql.replace(">", ">"); sql = sql.replace("&", "&"); sql = sql.replace(""", "\""); sql = sql.replace("'", "'"); prepareStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { Object param = params[i]; if (param instanceof String) { prepareStatement.setString(i + 1, (String) param); } else if (param instanceof Integer) { prepareStatement.setInt(i + 1, (Integer) param); } else if (param instanceof Long) { prepareStatement.setLong(i + 1, (Long) param); } else if (param instanceof Boolean) { prepareStatement.setBoolean(i + 1, (Boolean) param); } else if (param instanceof java.sql.Date) { prepareStatement.setDate(i + 1, (java.sql.Date) param); } else if (param instanceof Date) { prepareStatement.setDate(i + 1, new java.sql.Date( ((Date) param).getTime())); } else if (param instanceof Double) { prepareStatement.setDouble(i + 1, (Double) param); } else if (param instanceof Byte) { prepareStatement.setByte(i + 1, (Byte) param); } else if (param instanceof Short) { prepareStatement.setShort(i + 1, (Short) param); } } ResultSet executeQuery = prepareStatement.executeQuery(); ResultSetMetaData metaData = executeQuery.getMetaData(); while (executeQuery.next()) { Map<String, Object> map = new HashMap<String, Object>(); for (int i = 1; i <= metaData.getColumnCount(); i++) { String columnLabel = metaData.getColumnLabel(i); Object value = null; try { value = executeQuery.getObject(columnLabel); } catch (SQLException e) { String message = e.getMessage(); if ("Value '0000-00-00' can not be represented as java.sql.Date" .equals(message)) { value = null; } } map.put(columnLabel, value); } list.add(map); } return list; } catch (SQLException e) { e.printStackTrace(); } finally { if (prepareStatement != null) { try { prepareStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (isClose) { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } return null; } public static List<Map<String, Object>> query(Connection connection, String sql, Object... params) { return query(connection, sql, false, params); } public void close(Connection connection) { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }