posts - 44,  comments - 48,  trackbacks - 0

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了

posted on 2007-11-20 15:21 摩西 阅读(850) 评论(0)  编辑  收藏 所属分类: work_2007

只有注册用户登录后才能发表评论。


网站导航: