• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mysql 搞代码 7年前 (2018-06-08) 117次浏览 已收录 0个评论

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

欢迎大家阅读《mysql连接中解决中文输入的有关问题_mysql》,跪求各位点评,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mysql连接中解决中文输入的有关问题_mysql
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址