关于数据库出错了....高手帮忙下
下面就是我的登陆对话框中当点击登陆时 触发的代码
为什么从数据库中取出来的密码是乱码啊?
public void checkfill() {
String lname = Lname.getText();
char lpass[] = Lpass.getPassword();
if (lname.length() == 0) {
JOptionPane.showMessageDialog(this, "请输入用户名 ");
} else if (lpass.length == 0) {
JOptionPane.showMessageDialog(this, "请输入密码 ");
} else {
Logincon();
}
}
public void Logincon() {
String url = "jdbc:mysql://localhost:3306/lucky ";
sqlname = "root ";
sqlpass = "zhangyang ";
try {
Class.forName( "com.mysql.jdbc.Driver ");
con = DriverManager.getConnection(url, sqlname, sqlpass);
// System.out.println( "1 run ing now ");
} catch (ClassNotFoundException e) {
System.out.println( "装载驱动程序失败 ");
e.printStackTrace();
System.exit(1);
} catch (SQLException e) {
System.err.println( "无法连接数据库 ");
e.getMessage();
}
try {
lname = Lname.getText();
String checkname = "select * from user where name= " + lname + " ";
/*
* String strsql = "select * from user where (name= ' " + lname + " '
* and pass= ' " + lpass + " ') ";
*/
stmt = con.createStatement();
rs = stmt.executeQuery(checkname);
boolean flag = rs.next();
if (flag) {
lpass = Lpass.getPassword();
String checkpass = "select * from user where name= " + lname
+ " ";
stmt = con.createStatement();
rs = stmt.executeQuery(checkpass);
rs.next();
char[] repass = rs.getString(1).toCharArray();
System.out.println(repass.toString());
boolean x=Arrays.equals(lpass,repass);
if (x) {
flags = 1;
} else {
JOptionPane.showMessageDialog(this, "密码错误 ");
Lpass.setText( " ");
}
}
else {
JOptionPane.showMessageDialog(this, "用户名不存在 ");
Lname.setText( " ");
Lpass.setText( " ");
return;
}
con.close();
rs.close();
stmt.close();
} catch (SQLException e) {
e.getStackTrace();
}
}
还有就是我在程序中注册用户后,存在数据库中也是乱码
表的结构如下
mysql> desc user;
+--------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------+------+-----+---------+-------+
| name | char(50) | NO | PRI | NULL | |
| pass | char(20) | NO | | NULL | |
| mail | char(80) | NO | | NULL | |
| adress | char(100) | NO | | NULL | |
+--------+-----------+------+-----+---------+-------+
4 rows in set (0.08 sec)
在数据库中存储为
mysql> select * from user;
+-----------+------------+---------------+----------------+
| name | pass | mail | adress |
+-----------+------------+---------------+----------------+
| 111 | [C@9ed927 | 111@163.com | 111 |
| 12 | [C@112f614 | 111@163.com | 111 |
| 121 | [C@12a54f9 | 121@163.com | 121 |
| 121313213 | [C@19106c7 | 111@163.com | 111 |
| 123 | [C@723d7c | 123@163.com | kjnh |
| 1231 | [C@13bad12 | 123@163.com | kjnh |
| 12313 | [C@13582d | 122@163.com | 122 |
| 198827 | [C@1975b59 | 111@163.com | 111 |
| 207 | [C@aeffdf | 123@163.com | kjnh |
| 208 | [C@b42cbf | 123@163.com | kjnh |
在这里用户名和密码一样,但是在数据库中显示不一样
谢谢改正...
[解决办法]
这个应该不是取的问题,应该是你通过你的程序前端保存用户名和密码到数据库中时出的问题,建议检查一下那里
[解决办法]
首先,查看数据库密码是否加密?如果没有加密?应该在存储密码和读取密码是有问题。