5

I am working on a desktop application with Java Swing and save data on a MySQL database in arabic language with using UTF-8.

When I run the application from Eclipse everything work well but when I finish and I export my work to a runnable jar using Eclipse export, nothing related to the database works.

  • Login doesn't work
  • When I try to save date to my database in Arabic I get ?????? in the database

However, as I mentioned earlier, everything work correctly when I run it from Eclipse. Can any one help me, I must deliver my work

This is a sample of my work:

This is how I connect connect my database :

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";

This is the code for 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();
            } 
        }
    }   
}

Resulting MySQL variables using this cmd :SHOW VARIABLES LIKE 'c%'; abd every thins is uts8

enter image description here

Adel Bachene
  • 974
  • 1
  • 14
  • 34
  • This question is horrid. We need code, errors, anything! Plus it is in need of formating. – Cole Tobin Aug 27 '12 at 23:50
  • @SLaks when i use the exported jar i cant do the login but when i run it from eclipse i can ,so i removed the login and i try to save data from the exported jar i find on the data base ?????? but when i run it from eclipse i can save the data – Adel Bachene Aug 27 '12 at 23:50
  • How do you expect anyone to possibly help you without at the least a snippet of code? – obataku Aug 27 '12 at 23:52
  • @veer ok i will edit and add an exemple how im donig it – Adel Bachene Aug 27 '12 at 23:56
  • @veer any thing wrong on my code ?? – Adel Bachene Aug 28 '12 at 00:01
  • **Do not use MD5**. You should use a secure, salted, iterated hash. Preferably scrypt, bcrypt, or PBKDFv2. – SLaks Aug 28 '12 at 00:05
  • @SLaks when i insert data i dont use MD5 but the seem thing !! – Adel Bachene Aug 28 '12 at 00:07
  • @SLaks i said when i run it from exlipe every thing work i dont thing the problem is on MD5 – Adel Bachene Aug 28 '12 at 00:08
  • 1
    I know the problem is not with MD5. However, when your database gets hacked, the problem will be with MD5. _Do not use MD5._ – SLaks Aug 28 '12 at 00:15
  • @Slaks ok thank you about that , but do you have any idea for my problem , plz i need help on this one – Adel Bachene Aug 28 '12 at 00:17
  • @user1417996: This is off-topic but next time when you post your code, please make sure that it is correctly formatted. Especially when you're posting a considerable size of your code :) – Sujay Aug 28 '12 at 00:21
  • @Sujay thank you for your help sir , i will be sure next time – Adel Bachene Aug 28 '12 at 00:32
  • Your logic will fail if you have more than one user. Use a WHERE clause with parameters. – SLaks Aug 28 '12 at 00:33
  • @Slaks i have only one user and it work when i run the app from eclipse i want any idea why when i export it on jar file its don't work ?????? – Adel Bachene Aug 28 '12 at 00:35
  • Error message would be usefull, Also are you using the same version of Java when running from Eclipse and standalone. Some charsets are in Charset.jar (rather than runtime.jar). If there is a problem/missing charset.jar in the verson of java use to run the jar you would get this type of issue – Bruce Martin Aug 28 '12 at 01:04
  • @BruceMartin sir i don't get any Error when i run it from eclipse and i save a data in my database i found the data on the table in ut8 but when i save a data from the jar file i find on the database only ????????? – Adel Bachene Aug 28 '12 at 01:24
  • Thaaank's for evey one for your help and advice ^^ – Adel Bachene Aug 28 '12 at 01:42

2 Answers2

5

First, you should never convert a String to bytes using an unspecified charset (here you are using the platform default, which could change):

passwordlogin.getBytes()

Since this is for the bytes you are going to hash, which charset you use doesn't really matter as long as it is consistent. something like passwordlogin.getBytes("UTF-8") would seem reasonable.

Also, this is certainly a problematic bit of code:

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

it seems to imply that you have a characterset breakdown somewhere.

Lastly, how are you running your jar? Are you specifying a charset on the command line (e.g. java -Dfile.encoding="UTF-8" ...).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • if i dont convert String to bytes using an unspecified charset how i gave to to convert them because i have to due to mysql – Adel Bachene Aug 28 '12 at 01:31
  • no sir i'm not specifying a charset on the command line , how to do this ? and thank you for your help – Adel Bachene Aug 28 '12 at 01:32
  • 1
    updated above. also, i show how to specify the charset in my example command line. – jtahlborn Aug 28 '12 at 01:38
  • @jtahlborn: Hi..Can you please draw pointers on this question. At least for UTF part. http://stackoverflow.com/questions/12059807/utf-encoded-data-works-with-jsp-but-not-when-export-pdf-using-xmlworkerhelper – Hardik Mishra Aug 28 '12 at 05:10
3

Launch it with java -Dfile.encoding="UTF-8" -jar JarFile.jar

mastov
  • 2,942
  • 1
  • 16
  • 33