2

I am having trouble declaring an enumeration for DB field types that I can use across all functions of a particular class. With the following code I get "cannot resolve USERNAME to a variable type":

public class SQL_access {

    public enum DBfields { BLANK, USERNAME, ID, PASSWORD, FIRSTNAME, LASTNAME }; 

    public boolean loginValidate( String username, String password ){

        String DBuser, DBpass;
        PreparedStatement table = connectToTable( "firstsql", "users");
        ResultSet row = table.executeQuery();;

        while(row.next()){
            DBuser = row.getString(USERNAME);
            if(DBuser.equals(username)){
                DBpass = row.getString(PASSWORD);
                break;
            }
        }
    }
};

8 Answers 8

6

You need to use DBfields.USERNAME.

UPDATE:

in order to use with the getString(String) method, you need to use the name of the enum, like: Dbfields.USERNAME.name().

if you are using the enums only for the jdbc API access, you would be better off just using a String constant:

public static final String DBFIELD_USERNAME = "USERNAME";
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, and get this error: The method getString(int) in the type ResultSet is not applicable for the arguments (SQL_access.DBfields)
getString(xxx) needs an int as argument, not a String. Did it wrong in the first place, too. But updated my post now.
@Simulant - there is both getString(int) (get by position) and getString(String) (get by column name).
3

You need to reference the enum Type: DBfields.USERNAME, or statically import the enum like:

import static mypackage.SQL_access.DBfields.*;

Also, in your case, this is not enough. You need to pass the column name -- a String -- or the column position -- an int -- to the ResultSet:

row.getString(DBfields.USERNAME.name());

Used like this, you loose the main advantage of enums which is it's static nature, but it can still be useful in other places in your code if you refer to these values as a "bag".

Comments

1

You should access the enum by using DBfields.USERNAME.

See the Oracle Docs for more information.

Comments

1

Try qualifying the enumerator with the enum type:

   while(row.next()){
        DBuser = row.getString( DBfields.USERNAME);
        if(DBuser.equals(username)){
            DBpass = row.getString( DBfields.PASSWORD);
            break;
        }
    }

Comments

1

When you reference an enumeration it is always in this form EnumName.Field. In your example you need the following:

DBUser = row.getString(DBfields.USERNAME);  
DBpass=row.getString(DBfields.PASSWORD);

Comments

1

to access your enumbs use DBfields.USERNAME, but this will not help you, bacuse row.getString() needs an int as argument. Your Enumbs are of the Type DBFields not int. better use public static final int USERNAME = the int value here; and call with row.getString(USERNAME);.

2 Comments

thanks for the answer, but that's too messy =/ I'll just use the actual ints I guess
with the final static int USERNAME = 1; declaration your accresscodes get a readable name.
0

Wrong way.You need to call the enum using DBfields.PASSWORD.

Comments

0

You need to call like this: DBfields.PASSWORD

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.