UTF-8适用于Eclipse,但无法使用导出的jar [英] UTF-8 works on Eclipse but fails to work with an exported jar
问题描述
我正在使用 Java
Swing
的桌面应用程序,并在上保存数据MySQL
阿拉伯语数据库,使用 UTF-8
。
I am working on a desktop application with Java
Swing
and save data on a MySQL
database in arabic language with using UTF-8
.
当我运行来自 Eclipse
的应用程序一切正常但是当我完成并使用<$ c $将我的工作导出到 runnable jar
c> Eclipse export
,与数据库无关。
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
但是,正如我前面提到的,一切正常当我从 Eclipse
运行它时正确。任何人都可以帮助我,我必须完成我的工作
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 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";
这是的代码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
Resulting MySQL variables using this cmd :SHOW VARIABLES LIKE 'c%'; abd every thins is uts8
推荐答案
首先,你永远不应该使用未指定的字符集将字符串转换为字节(这里你使用的是平台默认值,可能会改变):
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()
因为这是你要哈希的字节,你使用哪个字符集并不重要,只要它是一致的。类似 passwordlogin.getBytes(UTF-8)
似乎是合理的。
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.
最后,你是如何运行你的罐子的?您是否在命令行上指定了一个字符集(例如 java -Dfile.encoding =UTF-8...
)。
Lastly, how are you running your jar? Are you specifying a charset on the command line (e.g. java -Dfile.encoding="UTF-8" ...
).
这篇关于UTF-8适用于Eclipse,但无法使用导出的jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!