如何在以下条件下匹配用户和密码。我总是得到用户名/密码不匹配。
try
{
Conn();
st = conn.createStatement();
rs = st.executeQuery("Select UserName,PassWord from login_tbl where UserName = '"+txtUser.getText()+"' and PassWord = '"+txtPass.getPassword()+"'");
if(rs != null)
{
new CarRent().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Your Username or Password is incorrect","",JOptionPane.ERROR_MESSAGE);
}
}
您应该使用这个rs.next()
而不是这个rs != null
try
{
JOptionPane.showMessageDialog(null, "Username is : " + txtUser.getText().trim().toString() + " Password is : " + txtPass.getPassword().trim().toString(),"",JOptionPane.ERROR_MESSAGE);
Conn();
st = conn.createStatement();
rs = st.executeQuery("Select UserName,PassWord from login_tbl where UserName = '"+txtUser.getText()+"' and PassWord = '"+txtPass.getPassword()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null, "Password is valid","",JOptionPane.ERROR_MESSAGE);
new CarRent().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Your Username or Password is incorrect","",JOptionPane.ERROR_MESSAGE);
}
感谢大家,我已经解决了我的问题:)
String sql = "select * from login_tbl where usern=? and pass=?";
try{
Conn();
ps=conn.prepareStatement(sql);
ps.setString(1,txtUser.getText());
ps.setString(2,new String(txtPass.getText()));
rs=ps.executeQuery();
if (rs.next()){
//about s= new about();
//
//s.setVisible(true);
new CarRent().setVisible(true);
dispose();
}
else
{
JOptionPane.showMessageDialog(null, "Password or Username is incorrect");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}