content: package com.vua.common.util;
  
  import com.sun.xml.internal.xsom.impl.scd.Iterators;
  
  import java.sql.*;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  /**
   * Created by liunian on 6/17/17.
   */
  public class JdbcUtil {
      private Connection connection;
      private PreparedStatement pstmt;
      private ResultSet rs;
  
      public JdbcUtil(String driver, String url, String username, String password) {
          try {
              Class.forName(driver);
              connection = DriverManager.getConnection(url, username, password);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      //查询多条记录
      public List<Map> selectByParams(String sql, List params) throws SQLException {
          List<Map> list = new ArrayList<>();
          int index = 1;
          pstmt = connection.prepareStatement(sql);
          if (null != params && !params.isEmpty()) {
              for (int i = 0; i < params.size(); i++) {
                  pstmt.setObject(index++, params.get(i));
              }
          }
          rs = pstmt.executeQuery();
  
          ResultSetMetaData metaData = rs.getMetaData();
          int cols_len = metaData.getColumnCount();
          while (rs.next()) {
              Map map = new HashMap();
              for (int i = 0; i < cols_len; i++) {
                  String cols_name = metaData.getColumnName(i + 1);
                  Object cols_value = rs.getObject(cols_name);
                  if (null == cols_value) {
                      cols_value = "";
                  }
                  map.put(cols_name, cols_value);
              }
              list.add(map);
          }
          return list;
      }
  
      public void release() {
          try {
              if (null != rs) {
                  rs.close();
              }
              if (null != pstmt) {
                  pstmt.close();
              }
              if (null != connection) {
                  connection.close();
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  }
  