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;
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