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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…