UTF-8适用于Eclipse,但无法使用导出的jar

时间:2012-08-27 23:43:18

标签: java mysql jdbc utf-8 jar

我正在使用Java Swing处理桌面应用程序,并使用MySQL以阿拉伯语保存UTF-8数据库中的数据。

当我从Eclipse运行应用程序时,一切正常,但是当我完成并使用runnable jar Eclipse将工作导出到export时,与数据库无关的工作原理。

  • 登录不起作用
  • 当我尝试在Arabic中将日期保存到我的数据库时,我在数据库中得到??????

但是,正如我之前提到的,当我从Eclipse运行时,一切正常。任何人都可以帮助我,我必须完成我的工作

这是我的工作样本:

这就是我连接数据库的方式:

static Connection conn = null;
static String url      = "jdbc:mysql://localhost:3306/";
static String dbName   = "gestiondestock";
static String driver   = "com.mysql.jdbc.Driver";
static String userName = "root"; 
static String password = "";
static String unicode= "?useUnicode=yes&characterEncoding=UTF-8";

这是buttonLogin ActionPerformed的代码:

private void buttonLogin_ActionPerformed(ActionEvent e) {
    if(textField.getText().equals("") || passwordField.getText().equals(""))
    {
       jd =new JDialog();
        jd.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        jd.setTitle("الرجاء ملء الفراغ");
        jd.setVisible(true);
        jd.setLocationRelativeTo(null);
        jd.setSize(400,200);
        jd.setContentPane(buildpp());   
    }
    else{
        if(textField.getText().equals("Adel91") || passwordField.getText().equals("Adel91"))
        {
            CardLayout cardLayout = (CardLayout) contentPane.getLayout();
            cardLayout.show(contentPane, "Panel_Home");
        }
        else{
            try{
                String usernamena = new String(textField.getText());
                String passwordlogin = new String(passwordField.getText());

                MessageDigest mdEnc4 = MessageDigest.getInstance("MD5");

                mdEnc4.update(passwordlogin.getBytes(), 0, passwordlogin.length());
                String passwordlogindmd5 = new BigInteger(1, mdEnc4.digest()).toString(16); // Encrypted 

                try{
                    Class.forName(driver).newInstance();
                    conn = DriverManager.getConnection(url+dbName+unicode,userName,password);
                    Statement st = conn.createStatement();

                    ResultSet res = st.executeQuery("SELECT username,password FROM client ");

                    String user = null;
                    String pass = null;

                    if(res.next()) {
                        user = new String( res.getBytes(1), "UTF-8");
                        pass =  new String( res.getBytes(2), "UTF-8");
                    }

                    if(usernamena.equals(user)&&passwordlogindmd5.equals(pass)){
                        CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                        cardLayout.show(contentPane, "Panel_Home");
                    }
                    else{
                        jd =new JDialog();
                        jd.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                        jd.setTitle("كلمة المرور والإسم غير مناسبان");
                        jd.setVisible(true);
                        jd.setLocationRelativeTo(null);
                        jd.setSize(430,200);
                        jd.setContentPane(buildwronglogin());
                    }
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            } catch (NoSuchAlgorithmException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } 
        }
    }   
}

使用此cmd生成的MySQL变量:SHOW VARIABLES LIKE'c%'; abd每个都是uts8

enter image description here

2 个答案:

答案 0 :(得分:5)

首先,您不应该使用未指定的字符集将字符串转换为字节(此处您使用的平台默认值可能会更改):

passwordlogin.getBytes()

由于这是针对您要哈希的字节,因此只要它是一致的,您使用的字符集就不重要了。像passwordlogin.getBytes("UTF-8")这样的东西似乎是合理的。

此外,这肯定是一个有问题的代码:

                if(res.next()) {
                    user = new String( res.getBytes(1), "UTF-8");
                    pass =  new String( res.getBytes(2), "UTF-8");
                }

这似乎暗示你在某个地方有角色分解。

最后,你是如何运行你的罐子的?您是否在命令行上指定了一个字符集(例如java -Dfile.encoding="UTF-8" ...)。

答案 1 :(得分:3)

使用java -Dfile.encoding="UTF-8" -jar JarFile.jar

启动它