Java SQL 检查用户名是否已存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21337830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:08:03  来源:igfitidea点击:

Java SQL Check if username already exists

javasqldatabase

提问by user3211740

I am trying to check if the username already exists in the table: The database also has the field set to unique The method will return a boolean stating if the username already exists or not. The part of my code that sets the usernameExists var to true never runs even if the usernames are the same.

我正在尝试检查表中是否已存在用户名:数据库也将字段设置为唯一该方法将返回一个布尔值,说明用户名是否已存在。即使用户名相同,我的代码中将 usernameExists 变量设置为 true 的部分也永远不会运行。

 public static boolean CheckUsernameExists(String username)
{
    boolean usernameExists = false;

    try
    {

        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(urlDB, usernameDB, passwordDB);

        PreparedStatement st = connection.prepareStatement("select * from Members order by username desc");
        ResultSet r1=st.executeQuery();
        String usernameCounter;
         if(r1.next()) 
         {
           usernameCounter =  r1.getString("username");
           if(usernameCounter == username) //this part does not happen even if it should
           {
               System.out.println("It already exists");
              usernameExists = true;
           }
         }


     }

     catch (SQLException e) 
     {
        System.out.println("SQL Exception: "+ e.toString());
     } 
     catch (ClassNotFoundException cE) 
     {
        System.out.println("Class Not Found Exception: "+ cE.toString());
     }

 return usernameExists;
 }

IF you can help in anyway I would be thankful!

如果您能提供帮助,我将不胜感激!

采纳答案by crnlx

Use if(usernameCounter.equals(username)). String comparisons are tricky. This has been discussed in detail here.

使用if(usernameCounter.equals(username)). 字符串比较很棘手。这已在此处详细讨论。

回答by user3229382

Your problem is usernameCounter == username. You are comparing objects where instead I think you want to be comparing strings.

你的问题是usernameCounter == username。您正在比较对象,而不是我认为您想要比较字符串。

try usernamecounter.equals(username)

尝试usernamecounter.equals(username)

回答by EmirCalabuch

Never compare two Stringinstances using ==, use equals()(==checks for identity, not equality).

永远不要String使用==,使用equals()==检查身份,而不是相等)比较两个实例。

You could also directly use the username in the query, rather than taking the username from the query result:

您也可以直接在查询中使用用户名,而不是从查询结果中获取用户名:

PreparedStatement st = connection.prepareStatement("select * from Members where username = ?");
st.setString(1, username);
ResultSet r1=st.executeQuery();
if(r1.next()) {
  usernameExists = true;
}

回答by M21B8

As everyone has pointed out, you need to use .equals when you compare strings. however for your usage, I would recommend using .equalsIgnoreCase, as you probably want it to treat "MSmith" and "msmith" as the same.

正如每个人都指出的那样,在比较字符串时需要使用 .equals。但是对于您的使用,我建议使用 .equalsIgnoreCase,因为您可能希望它将“MSmith”和“msmith”视为相同。

Also, a much more efficient way to perform this check would be to select the row on the table

此外,执行此检查的一种更有效的方法是选择表中的行

PreparedStatement st = connection.prepareStatement("select * from Members username=?");
st.setString(1,username);

Then, if you get a row, you know the username is already used. If you don't get a row or you get a RowNotFoundException, then you can treat that as "the username has not been used". Again, you would need to be careful with the case of the input when doing the check.

然后,如果您得到一行,您就知道该用户名已被使用。如果您没有得到一行或得到 RowNotFoundException,那么您可以将其视为“用户名尚未被使用”。同样,在进行检查时,您需要小心输入的情况。