1

I have a ClientsTable like that :

firstName lastName address idNumber userName password
         |        |       |        |        |
david      bowie    here    123     mick      jagger 
chuck      norris   there   456     steven    seagal

I want to get both the userName and the password of each row and verify it with the given parameters of my method :

public Person verifyClientExists(String username,String password) throws SQLException
{

    ResultSet results = this.m_statement.executeQuery("SELECT `userName` FROM `ClientsTable`");  
    while (results.next() == true)
    {
        // here I want to compare the retrieved data from 
                    // mysql with the "username" and "password" given in the prototype
    }



    return null;

} 

But with my current query I can only get one field .

How can I retrieve two fields?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jack cole
  • 447
  • 2
  • 8
  • 24

7 Answers7

3

Why not use

select username, password from clientstable

? And then your ResultSet interrogation is:

String username = results.getString("username");
String password = results.getString("password");

See the JDBC tutorial page on ResultSets. Note that I ask for the results by column name. It's fractionally more robust than by asking for them by index (number). If I change the SQL statement (reorder query parameters) then the correct columns are still returned.

I hope you're not storing cleartext passwords in those tables, btw! See here for more info on hashing and salts.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
2

Change your query to:

SELECT `userName`, `password` FROM `ClientsTable`
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

First, I would suggest using the APIs to build queries, and not execute raw queries (take a look here).

But to answer your question - if you want to get also the password use:

ResultSet results = this.m_statement.executeQuery("SELECT `userName`, `password` FROM `ClientsTable`");  
MByD
  • 135,866
  • 28
  • 264
  • 277
1

alternatively, you can check the number of records from the result:

SELECT COUNT(*) 
FROM   `ClientsTable`
WHERE  userName = 'userHere' AND
       password = 'passHere'

the result here would be 0 and 1 only :) because I assume that the username is unique.

but if you want to retrieve the username and the password, just select the two columns in your query.

SELECT `userName`, `password` 
FROM `ClientsTable`
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

You can ask for more fields like this: SELECT userName, password FROM ClientsTable

Then use resultSet.getString(1) for userName and resultSet.getString(2) for password

Paaske
  • 4,345
  • 1
  • 21
  • 33
1

Change your sql to "SELECT userName ,password FROM ClientsTable

Now you can use the resultset object and fetch the values
results.getString(1)
results.getString(2)

or you can retrieve using the fieldName results.getString("userName"); results.getString("password");

Durgadas Kamath
  • 400
  • 2
  • 12
1

Your query is wrong. Your query should be like :-

SELECT EMP_NAME,EMP_PASSWORD FROM EMP_DETAILS TABLE;

Its quite simple to retrieve not only 2 field but also any number of field.

JDGuide
  • 6,239
  • 12
  • 46
  • 64