我无法启动 else 语句。此代码位于 GUI 中,在文本字段中输入用户名后向用户发送电子邮件。如果在 SQL 数据库中找到它,它会检索电子邮件地址,一切正常。如果输入的用户名不在数据库中,它什么也不做,我没有收到错误消息,它只是挂起。这是提交按钮的代码
jButtonSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt)
{
try
{
username = (jTextFieldUsername.getText());
connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
query = "SELECT User_userName, User_email FROM user_details WHERE " +
"User_userName = '"+username+"'";
PreparedStatement stm = connection.prepareStatement(query);
rs =stm.executeQuery();
while (rs.next())
{
String eMail = rs.getString ("User_email");
if (username.equals(rs.getString("User_userName")))
{
JOptionPane.showMessageDialog(frame, "An email with your password "
+ "has been sent \nto " + eMail);
}// end if
else
{
JOptionPane.showMessageDialog(frame, "Incorrect username. If "
+ "you are unable to login\n"
+ "with your current username, please contact us on\n"
+ "info@somewhere.com.au.");
}//end else
} //end while
close();
}//end try //catch statement follows
期待中。。。谢谢。。
谢谢大家的帮助。它现在有效,您有效地解决了我的另一个问题。这是最终的代码。再次感谢。
int count = 0;
而(rs.next()){
eMail = rs.getString ("User_email");
count++;}
if (count != 0)
{
JOptionPane.showMessageDialog(frame, "An email with your password "
+ "has been sent \nto " + eMail);
}// end if
else
{
JOptionPane.showMessageDialog(frame, "Incorrect username. If "
+ "you are unable to login\n"
+ "with your current username, please contact us on\n"
+ "info@somewhere.com.au.");
}//end else
// } //end while