根据输入查找并提取db值(Java和SQL Server)

时间:2023-01-13 04:07:59

New to java.
I am attempting to write a class that will input a username, run a query on the username to find the ID, and subsequently use that ID in "where clauses" on all my other classes.

Java新手。我正在尝试编写一个将输入用户名的类,在用户名上运行查询以查找ID,然后在我所有其他类的“where子句”中使用该ID。

This is the statement that I execute (which will only ever return a recordset of a single row):

这是我执行的语句(它只会返回单行的记录集):

String sqlStatement = "SELECT AccountHolderId, Passcode from CIS4720.DBO.AccountHolder " +
"where Username = '" + logonName + "'";  

Here is my attempt at extracting the ID via the username...

这是我尝试通过用户名提取ID ...

while (rset.next())
    {
        if(rset.getInt("Username")==logonName){
            int whosOnFirst = rset.getInt("AccountHolderId");
        }

I saw another answer on the forum that says you can't assign database values to variables. If that is the case, what is a better strategy?

我在论坛上看到另一个答案,说你无法将数据库值分配给变量。如果是这样的话,什么是更好的策略?

(Also, I realize I'm not parameterizing, but I'd like to get this working before fixing that issue. This is for a course assignment so I am not worried about hack attacks).

(另外,我意识到我没有参数化,但是我想在解决这个问题之前让它工作。这是一个课程作业,所以我不担心黑客攻击)。

P. S. Thanks I fixed the double equals sign (and the extra parenthesis) in the code above.

P. S.谢谢我修复了上面代码中的双等号(以及额外的括号)。

1 个解决方案

#1


1  

Here are some comments about the code:

以下是有关代码的一些注释:

  • rset.getInt("Username") will get the column Username from the result but it also looks for an Integer column because of getInt. You are not selecting that column in the sql statement so will error out.
  • rset.getInt(“Username”)将从结果中获取列用户名,但由于getInt,它还会查找Integer列。您没有在sql语句中选择该列,因此会出错。

  • If you select it and get a string, use .equals() instead of == to compare string. Also, one = is assignment and == is comparison.
  • 如果选择它并获取字符串,请使用.equals()而不是==来比较字符串。另外,one =是赋值,==是比较。

  • You can use getString to read Strings from the result set.
  • 您可以使用getString从结果集中读取字符串。

  • You don't need to check the username and match it since your query should return exactly that user's data so I would remove the if condition entirely and just have the getInt line there.
  • 您不需要检查用户名并匹配它,因为您的查询应该完全返回该用户的数据,因此我将完全删除if条件并在那里只有getInt行。

#1


1  

Here are some comments about the code:

以下是有关代码的一些注释:

  • rset.getInt("Username") will get the column Username from the result but it also looks for an Integer column because of getInt. You are not selecting that column in the sql statement so will error out.
  • rset.getInt(“Username”)将从结果中获取列用户名,但由于getInt,它还会查找Integer列。您没有在sql语句中选择该列,因此会出错。

  • If you select it and get a string, use .equals() instead of == to compare string. Also, one = is assignment and == is comparison.
  • 如果选择它并获取字符串,请使用.equals()而不是==来比较字符串。另外,one =是赋值,==是比较。

  • You can use getString to read Strings from the result set.
  • 您可以使用getString从结果集中读取字符串。

  • You don't need to check the username and match it since your query should return exactly that user's data so I would remove the if condition entirely and just have the getInt line there.
  • 您不需要检查用户名并匹配它,因为您的查询应该完全返回该用户的数据,因此我将完全删除if条件并在那里只有getInt行。