簡體   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