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

          }



          試過以上的所有編碼都無法解決數(shù)據(jù)在MYSQL中的亂碼問題,看來是jdbc連mysql時(shí)的編碼不對,于是
          將   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 摩西 閱讀(867) 評論(0)  編輯  收藏 所屬分類: work_2007
          主站蜘蛛池模板: 浦东新区| 保德县| 南康市| 沙雅县| 元江| 南雄市| 吴桥县| 应城市| 娱乐| 定陶县| 苍溪县| 平安县| 沁源县| 漯河市| 措美县| 甘德县| 富蕴县| 罗平县| 隆回县| 元谋县| 陆川县| 射洪县| 禹城市| 桃园市| 云南省| 莱西市| 黔南| 郯城县| 抚宁县| 江永县| 湟中县| 长海县| 三亚市| 大安市| 通山县| 乐业县| 眉山市| 南部县| 丹江口市| 镇雄县| 左权县|