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 摩西 閱讀(867) 評論(0)  編輯  收藏 所屬分類: work_2007
          主站蜘蛛池模板: 栾川县| 石台县| 儋州市| 乌鲁木齐县| 郎溪县| 靖远县| 临沂市| 西和县| 泰州市| 莱西市| 武鸣县| 麻阳| 孝义市| 东丰县| 巢湖市| 平泉县| 阿克陶县| 哈密市| 石景山区| 阿拉善右旗| 呼和浩特市| 出国| 个旧市| 红安县| 余江县| 五寨县| 瓮安县| 广元市| 图片| 胶南市| 九龙城区| 商城县| 岳普湖县| 吉安市| 资中县| 楚雄市| 凤山市| 永清县| 临城县| 东乌珠穆沁旗| 天柱县|