mssql数据往mysql导数据时,出现mysql数据为乱码问题?

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TempClass {

 /**
  * @param args
  * @throws UnsupportedEncodingException
  * @throws ClassNotFoundException
  * @throws SQLException
  */
 public static void main(String[] args) throws UnsupportedEncodingException {
  String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=tran";
  try {
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
   Connection conn = DriverManager.getConnection(url, "sa", "sa");
   Statement st = conn.createStatement();
   ResultSet rs = st.executeQuery("select vcword from cn_other");

   // mysql
   String url_mysql = "jdbc:mysql://127.0.0.1:3306/zwspace";
   Class.forName("org.gjt.mm.mysql.Driver");
   Connection conn_mysql = DriverManager.getConnection(url_mysql,
     "root", "root");
   PreparedStatement st_mysql = conn_mysql
     .prepareStatement("insert into chds_cihui(name) values(?)");

   if (rs.next()) {

    String rsname = rs.getString("vcword");
    // String name = new String(rsname.getBytes("GB2312"),
    // "GB2312");
    // String name = new String(rsname.getBytes("UTF-8"), "GB2312");
    // String name = new String(rsname.getBytes("utf8"), "GB2312");
    // String name = new String(rsname.getBytes("iso-8859-1"),
    // "GB2312");
    // String name = new String(rsname.getBytes("GBK"), "GB2312");

    // String name = new String(rsname.getBytes("GBK"), "GBK");
    // String name = new String(rsname.getBytes("GB2312"), "GBK");
    // String name = new String(rsname.getBytes("UTF-8"), "GBK");
    // String name = new String(rsname.getBytes("iso-8859-1"),
    // "GBK");

    // String name = new String(rsname.getBytes("GBK"), "UTF-8");
    // String name = new String(rsname.getBytes("GB2312"), "UTF-8");
    // String name = new String(rsname.getBytes("UTF-8"), "UTF-8");
    // String name = new String(rsname.getBytes("iso-8859-1"),
    // "UTF-8");

    // String name = new String(rsname.getBytes("GBK"),
    // "iso-8859-1");
    // String name = new String(rsname.getBytes("GB2312"),
    // "iso-8859-1");
    // String name = new String(rsname.getBytes("UTF-8"),
    // "iso-8859-1");
    // String name = new String(rsname.getBytes("iso-8859-1"),
    // "iso-8859-1");

    // String name = new String(rsname.getBytes(), "GBK");
    // String name = new String(rsname.getBytes(), "GB2312");
    // String name = new String(rsname.getBytes(), "UTF-8");
    // String name = new String(rsname.getBytes(), "iso-8859-1");
    String name = rsname;

    System.out.println(rsname);

    st_mysql.setString(1, name);
    st_mysql.executeUpdate();
   }
   rs.close();
   st.close();
   conn.close();

   st_mysql.close();
   conn_mysql.close();

  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

}



试过以上的所有编码都无法解决数据在MYSQL中的乱码问题,看来是jdbc连mysql时的编码不对,于是
将   String url_mysql="jdbc:mysql://127.0.0.1:3306/zwspace";后加上编码
   String url_mysql="jdbc:mysql://127.0.0.1:3306/zwspace?useUnicode=true&characterEncoding=utf8";终于OK了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值