http://blog.csdn.net/whucyl/article/details/20838079
package com.laifeng.util; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Iterator; import java.util.Map; /** * 支持中文输入和preparedStatement * Created by wangqiao on 2015/4/21. */ public class DbStoreHelper { private Connection con = null; private String dbUrl = null; private String dbClass = "com.mysql.jdbc.Driver"; private String dbUser = null; private String dbPasswd = null; private PreparedStatement prepstmt = null; private boolean error = false; private static final Logger LOG = LoggerFactory.getLogger(DbStoreHelper.class); public DbStoreHelper(final String sqlDBUrl, final String sqlUser, final String sqlPassword){ this.dbUrl = sqlDBUrl; this.dbUser = sqlUser; this.dbPasswd = sqlPassword; } /* * get connection and return a Connection object */ private Connection getConnection(final String sqlDBUrl, final String sqlUser, final String sqlPassword) throws ClassNotFoundException, SQLException { StringBuilder builder = new StringBuilder(); builder.append(sqlDBUrl).append("?useUnicode=true&characterEncoding=utf8&user=").append(sqlUser).append("&password=").append(sqlPassword); dbUrl = builder.toString(); Class.forName(dbClass); con = DriverManager.getConnection(dbUrl); return con; } public void execute(Map<String, String> map, final String sql){ if(this.con == null){ try { this.con = this.getConnection(this.dbUrl, this.dbUser, this.dbPasswd); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block LOG.error("execute:" + e.getMessage()); } catch (SQLException e) { // TODO Auto-generated catch block LOG.error("execute:" + e.getMessage()); } } if(this.con == null){ LOG.error("execute:conn get failed. drop sql:" + sql); return; } try { this.con.setAutoCommit(false); int count = 0; prepstmt = this.con.prepareStatement(sql); if(null!=map && map.size()>0){ //map循环 Iterator<Map.Entry<String, String>> keys = map.entrySet().iterator(); while (keys.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) keys.next(); if (StringUtils.isNotBlank(entry.getValue())) { LOG.info("sql excute :" + entry.getValue()); // 初始化语句 String[] infos = StringUtils.split(entry.getValue(), "|"); if(null!=infos && infos.length >=6){ if(StringUtils.isNumeric(infos[0]) && StringUtils.isNumeric(infos[3])){ prepstmt.setInt(1, Integer.parseInt(infos[0])); prepstmt.setString(2, infos[1]); prepstmt.setString(3, infos[2]); prepstmt.setInt(4, Integer.parseInt(infos[3])); prepstmt.setString(5, infos[4]); prepstmt.setString(6, infos[5]); prepstmt.addBatch(); // 加入批量处理 count++; } } } } } prepstmt.executeBatch(); // 执行批量处理 this.con.commit(); // 提交 System.out.println("All down : " + count); } catch (SQLException e) { // TODO Auto-generated catch block LOG.error("execute:execute failed. message = " + e.getMessage()+" drop sql:" + sql); this.error = true; } if(this.error){ try { this.con.close(); this.con = null; } catch (SQLException e) { // TODO Auto-generated catch block LOG.error("execute:conn.close failed. message = " + e.getMessage()); } } this.error = false; } }