【MySql】Java 批量插入数据库addBatch

//addBatch批量插入数据库
    public static void insertCommentToMySql(Set<String> commentList) {
        
        Iterator<String> it = commentList.iterator(); 
        
        Statement st=null;
        try {
            st = DBConnection.getConnection(dbName,dbUser,dbPwd).createStatement();
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        };
        
        String sql = null;
        String[] strs=null;
        while (it.hasNext()) { 
            strs = it.next().split("\\|");
            //0zid,1zname,2zmid,3ztext,4zsource,5zzan,6ztime
            sql="insert into weibo_comment(uid,name,mid,text,source,zan,time) values ('"+strs[0]+"','"+strs[1]+"','"+strs[2]+"','"+strs[3]+"','"+strs[4]+"','"+strs[5]+"','"+strs[6]+"') ON DUPLICATE KEY UPDATE updatetime=NOW()";
//            System.out.println(sql);
            try {
                st.addBatch(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }  
        
        try {
            st.executeBatch();
            st.clearBatch();
            st.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            logger.error(e);
        }

    }

  

posted @ 2016-11-26 23:41  陈泽泽  阅读(3144)  评论(0编辑  收藏  举报