mysql连接中解决中文输入的问题

参考
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;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值