【JAVA】DB取得結果分をHashMapを使いListに格納する

DBからの取得結果一覧表示などでよく使う処理。簡単。

import java.sql.*;
import java.util.*;

public static void test(HttpServletRequest request) {
  try {
    Statement st = null;
    ResultSet rs = st.executeQuery("select * from table");
    HashMap map = new HashMap();
    ArrayList list = new ArrayList();

    while (rs.next()) {
      map.clear();
      map.put("項目1", rs.getString("項目1"));
      map.put("項目2", rs.getString("項目2"));
      map.put("項目3", rs.getString("項目3"));
      list.add(map);
    }
    request.setAttribute("HashMapList", list);
    rs.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
}