---
title: "HashMap 数据集 插入到数据库中"
author: "自动放假"
date: "2012-04-05T15:13:00Z"
tags: []
slug: "hashmap"
draft: false
lastmod: "2013-06-25T20:41:09Z"
id: 1894325
url: "https://sunraise.iteye.com/blog/1894325"
---

```
  
/**通过获取的连接，list里面添加有map对象，将map对象取出，并将它的value通过sql语句插进数据库中
     *   显示插入结果
     */
    @Override
    public void insertListtoDb(Connection conn, List<Map<String, String>> list)
    {
        try
        {
            /**
             *  迭代取出list中的Map对象，
             */
            
            Iterator<Map<String, String>> iter = list.iterator();
            
            while (iter.hasNext())
            {
                String insertSql = null;
                Map<String, String> m = (Map<String, String>)iter.next();
                Object key = m.keySet();
                
                /** 取出字段的值 **/
                String sid = m.get("studentId");
                String classId = m.get("classId");
                String sname = m.get("studentName");
                
                /** record 是动态参数 **/
                String record = "(" + sid + ",'" + sname + "'," + classId + ")";
              
                insertSql = "insert into student(sid,sname,classId) values" + record;
                System.out.println("----sql-----:" + insertSql);
               
                /** 执行插入操作 **/
                Statement stmt = conn.createStatement();
                stmt.execute(insertSql);
                System.out.println("succes insert into database!");
                
            }
            
        }
        
        catch (Exception e)
        {
            System.out.print("get data error!");
            e.printStackTrace();
            return;
        }
    }
    
```

关键的插入方法。