0

我无法启动 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  
4

2 回答 2

1

我认为没有必要在 while 循环中检查用户名的相等性,因为您已经使用该名称进行了查询。

int count=0;
while (rs.next()) 
         {
     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
于 2013-11-09T10:57:25.873 回答
0

不是完整的代码……而是你想要的!

query = "SELECT User_userName, User_email FROM user_details WHERE User_userName = ?";

                 PreparedStatement stm = connection.prepareStatement(query);
                 stm.setString(1, username);
                 rs =stm.executeQuery();
..
..
//And to check if rs is null
if(rs!=null){
   while(rs.next()){
     ....
    }
}
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."); 
}
于 2013-11-09T10:53:47.970 回答