Here you can find the source of readMapListBySQL(String driverClass, String host, String port, String database, String user, String password, String sql)
public static List<Map<String, Object>> readMapListBySQL(String driverClass, String host, String port, String database, String user, String password, String sql) throws Exception
//package com.java2s; //License from project: Apache License import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static List<Map<String, Object>> readMapListBySQL(String driverClass, String host, String port, String database, String user, String password, String sql) throws Exception { Class.forName(driverClass); StringBuilder url = new StringBuilder(); url.append("jdbc:mysql://"); url.append(host);// w w w . j ava2 s .co m url.append(":"); url.append(port); url.append("/"); url.append(database); Connection conn = DriverManager.getConnection(url.toString(), user, password); Statement statement = conn.createStatement(); ResultSet results = statement.executeQuery(sql); List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); Map<String, Object> map = null; ResultSetMetaData resultMeta = results.getMetaData(); int resultFieldCount = resultMeta.getColumnCount(); while (results.next()) { map = new HashMap<String, Object>(); for (int i = 1; i < resultFieldCount + 1; i++) { map.put(resultMeta.getColumnLabel(i), results.getObject(i)); } lists.add(map); } results.close(); statement.close(); conn.close(); return lists; } public static List<Map<String, Object>> readMapListBySQL(String driverClass, String url, String user, String password, String sql) throws Exception { Class.forName(driverClass); Connection conn = DriverManager.getConnection(url, user, password); Statement statement = conn.createStatement(); ResultSet results = statement.executeQuery(sql); List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); Map<String, Object> map = null; ResultSetMetaData resultMeta = results.getMetaData(); int resultFieldCount = resultMeta.getColumnCount(); while (results.next()) { map = new HashMap<String, Object>(); for (int i = 1; i < resultFieldCount + 1; i++) { map.put(resultMeta.getColumnLabel(i), results.getObject(i)); } lists.add(map); } results.close(); statement.close(); conn.close(); return lists; } }