Austin
Austin

Reputation: 1085

Java enum for use across class

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;
            }
        }
    }
};

Upvotes: 2

Views: 1104

Answers (8)

bruno conde
bruno conde

Reputation: 48265

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

Upvotes: 3

zhaoyw
zhaoyw

Reputation: 31

You need to call like this: DBfields.PASSWORD

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

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

Upvotes: 6

Simulant
Simulant

Reputation: 20102

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

Upvotes: 1

amicngh
amicngh

Reputation: 7899

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

Upvotes: 0

Woot4Moo
Woot4Moo

Reputation: 24316

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

Upvotes: 1

Andy Thomas
Andy Thomas

Reputation: 86381

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;
        }
    }

Upvotes: 1

Dan W
Dan W

Reputation: 5782

You should access the enum by using DBfields.USERNAME.

See the Oracle Docs for more information.

Upvotes: 1

Related Questions