Java JDBC MySQL Connection readMapListBySQL(String driverClass, String host, String port, String database, String user, String password, String sql)

Here you can find the source of readMapListBySQL(String driverClass, String host, String port, String database, String user, String password, String sql)

Description

read Map List By SQL

License

Apache License

Declaration

public static List<Map<String, Object>> readMapListBySQL(String driverClass, String host, String port,
            String database, String user, String password, String sql) throws Exception 

Method Source Code


//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;
    }
}

Related

  1. getSearchReaderConnection()
  2. openConnection()
  3. putData(int tID, String toolID)
  4. query(String sql)
  5. queryAll(String sql, List params)
  6. removeById(int Id)
  7. searchForItemId(String item)
  8. searchServerPropertySplitMinutes()
  9. tablesOk(String url, String user, String password)

  10. HOME | Copyright © www.java2s.com 2016