Home Lookup and extract db value based on input (Java & SQL Server)
Reply: 1

Lookup and extract db value based on input (Java & SQL Server)

BigDataLouie Published in 2018-02-13 20:25:21Z

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.

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

while (rset.next())
            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.

clinomaniac Reply to 2018-02-13 22:07:12Z

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.
  • If you select it and get a string, use .equals() instead of == to compare string. Also, one = is assignment and == is comparison.
  • You can use getString to read Strings from the result set.
  • 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.
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321409 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO