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

Ade*_*ene 4 java mysql jar jdbc utf-8

我正在使用桌面应用程序,Java SwingMySQL使用阿拉伯语将数据保存在数据库中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";
Run Code Online (Sandbox Code Playgroud)

这是以下代码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();
            } 
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

jta*_*orn 5

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

passwordlogin.getBytes()
Run Code Online (Sandbox Code Playgroud)

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

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

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

它似乎暗示你在某个地方有一个角色分解.

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