简体   繁体   中英

How can I retrieve two fields of a table in Mysql with Java?

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?

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.

将查询更改为:

SELECT `userName`, `password` FROM `ClientsTable`

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`");  

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`

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

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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM