繁体   English   中英

Java,如果可以,否则不没有错误消息

[英]Java if works, else doesn't with no error messages

我在启动else语句时遇到麻烦。此代码位于GUI中,在用户名在文本字段中输入后,该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;

while(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  

我认为无需在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

不是完整的代码...而是您想要的代码!

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."); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM