gpt4 book ai didi

Java 如果有效,则无效,并且没有错误消息

转载 作者:行者123 更新时间:2023-12-01 13:52:19 26 4
gpt4 key购买 nike

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

关于Java 如果有效,则无效,并且没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19874740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com