protected void button_actionPerformed(ActionEvent e)
throws HeadlessException, SQLException {
if (textField.getText().trim().equals("")
|| passwordField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null,"用户名和密码不能为空");
} else {
String dbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbUrl = "jdbc:sqlserver://localhost:1433;"
+ "DatabaseName=dbname;";
String password = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from xt_user where name='"
+ ""+textField.getText()+"'";
try {
Class.forName(dbClassName);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
conn = DriverManager.getConnection(dbUrl, "sa", "123");
stmt = conn.createStatement();
rs =stmt.executeQuery(sql);
while (rs.next()) {
password=rs.getString("age");
System.out.println(password);
System.out.println(passwordField.getText());
if (password.equals(passwordField.getText().trim())){
JOptionPane.showMessageDialog(button, "登录成功");
} else {
JOptionPane.showMessageDialog(null, "密码错误");
}
}
}
}
为什么输入正确的用户名和密码还是提示密码错误啊
throws HeadlessException, SQLException {
if (textField.getText().trim().equals("")
|| passwordField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null,"用户名和密码不能为空");
} else {
String dbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbUrl = "jdbc:sqlserver://localhost:1433;"
+ "DatabaseName=dbname;";
String password = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from xt_user where name='"
+ ""+textField.getText()+"'";
try {
Class.forName(dbClassName);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
conn = DriverManager.getConnection(dbUrl, "sa", "123");
stmt = conn.createStatement();
rs =stmt.executeQuery(sql);
while (rs.next()) {
password=rs.getString("age");
System.out.println(password);
System.out.println(passwordField.getText());
if (password.equals(passwordField.getText().trim())){
JOptionPane.showMessageDialog(button, "登录成功");
} else {
JOptionPane.showMessageDialog(null, "密码错误");
}
}
}
}
为什么输入正确的用户名和密码还是提示密码错误啊









