如何从整数中实例化枚举?

时间:2014-07-07 04:10:34

标签: java oop jdbc enums

我正在使用的枚举使用String,但数据库使用int。从State枚举中创建查找以返回整数相对简单。但是,查找只能以一种方式工作,从状态获取整数。我是否需要一个包含整数的包装类并返回State

数据库将State存储为int,我不想更改它。

package net.bounceme.dur.server.db;

import org.apache.commons.dbcp.BasicDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.bounceme.dur.data.State;
import net.bounceme.dur.data.Title;
import net.bounceme.dur.server.streams.MyProps;

public class Queries {

    private static final Logger log = Logger.getLogger(Queries.class.getName());
    private final MyProps p = new MyProps();
    private Connection connection = null;

    public void connect() throws SQLException {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(p.getDbDriver());
        dataSource.setUrl(p.getDbUrl());
        dataSource.setUsername(p.getDbUser());
        dataSource.setPassword(p.getDbPassword());
        connection = dataSource.getConnection();
    }

    public List<Title> selectTitles() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        connect();
        PreparedStatement statement = connection.prepareStatement("SELECT * FROM titles.titles");
        ResultSet resultSet = statement.executeQuery();
        Title title = null;
        List<Title> titles = new ArrayList<>();
        while (resultSet.next()) {
            title = new Title();
            title.setId(resultSet.getInt(1));
            title.setCreated(resultSet.getTimestamp(2));
            title.setPhone(resultSet.getInt(3));
            title.setTitle(resultSet.getString(4));
            int state = resultSet.getInt(5);
            State s;
            title.setState(State.a);
            title.setState(State.undefined);
            titles.add(title);
        }
        return titles;
    }

    public void updateTitle(Title clientTitle) {
        try {
            connect();
            String updateString
                    = "update titles.titles "
                    + "set STATE = ? where ID = ?";
            try (PreparedStatement ps = connection.prepareStatement(updateString)) {
                log.info(clientTitle.toString());
                ps.setInt(1, clientTitle.getState().getCode());
                ps.setInt(2, clientTitle.getId());
                ps.executeUpdate();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Queries.class.getName()).log(Level.SEVERE, null, ex);
        }
        log.info("...updated...?");
    }
}




package net.bounceme.dur.data;

public enum State {

    undefined(0), x(1), o(2), c(3), a(4), l(5), d(6);
    private int code = 0;

    State(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public State getState() {
        switch (code) {
            case 0:
                return State.undefined;
            case 1:
                return State.x;
            case 2:
                return State.o;
            case 3:
                return State.c;
            case 4:
                return State.a;
            case 5:
                return State.d;
            case 6:
                return State.l;
            default:
                return State.undefined;
        }
    }
}

类似的东西:

   State stateFromInt = StateUtils.getState(i);

然后我需要一个StateUtils课程,这看起来很麻烦。我完全意识到可以使数据库存储String数据,并将其解析回状态,但这不是我要求的解决方案。

1 个答案:

答案 0 :(得分:2)

只需向名为State的{​​{1}}添加静态方法:

fromCode(int code)

这是一种常见的工厂方法模式。在您的代码中,您可以随后调用:

public static State fromCode(int code) {
    switch (code) {
        // ...
    }
}

您甚至可以重构现有的title.setState(State.fromCode(resultSet.getInt(5))); 方法,也可以使用相同的静态方法(即使getState()方法似乎没有任何实际用途)。