JDBC,JPA存储枚举..

实体类,
@Entity
public class H {
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private int id;
    private String name;
    private Operation op;

    public H(){}

    public H(String name, Operation op){
        this.name = name;
        this.op = op;
    }
    //getters and setters...
}

上面类中op是个枚举对象,
public enum Operation {
    PLUS("+"),
    MINUS("-"),
    TIMES("*") ,
    DIVIDE("/");
    private final String symbol;

    Operation(String symbol) {
        this.symbol = symbol;
    }

    @Override
    public String toString() {
        return symbol;
    }
}

测试代码如下,
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("NewExpensePU");

        EntityManager em1 = emf.createEntityManager();
        em1.getTransaction().begin();
        H h = new H("*", Operation.TIMES);
        em1.persist(h);
        em1.getTransaction().commit();
        em1.close();
        
        EntityManager em2 = emf.createEntityManager();
        em2.getTransaction().begin();
        H result = em2.find(H.class, h.getId()); 
        System.out.println(result.getOp());
        em2.getTransaction().commit();
        em2.close();

        emf.close();

于是发现,JPA或者说ORM框架对付枚举完全木问题,数据中存储枚举对象的形式是个smallint(6),MySql是这样的。
其实,还是源于这句话“Java的枚举本质上是int值”。

再说枚举的两个重要方法,第一个,name()——用于获取枚举对象声明时的名称,有此方法便可以放心大胆地重写toString()了~
第二个,ordinal()——返回枚举常量的序数。(这个方法十分重要,如果你不知道它,就会十分疑问ORM框架是怎么将Enum的int值存入数据库的,就像我半小时前一样)
这样,适用JDBC的代码就很好写了吧...无非写sql语句时insert枚举的ordinal()
记一下使用JDBC的代码吧,我都快忘记了....
public class DataConnect {
    private Connection con = null;
    private Statement stat = null;
    private PreparedStatement pstat = null;
    private ResultSet rs = null;

    public Connection getCon() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            StringBuilder sd = new StringBuilder("jdbc:mysql://");
            sd.append("localhost:3306");
            sd.append("/");
            sd.append("newexpense");//database name
            String url = sd.toString();
            DriverManager.setLoginTimeout(10);//设置连接超时为10秒
            con = DriverManager.getConnection(url, "root", "root");
        } catch (Exception ex) {
            Logger.getLogger(DataConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        return con;
    }

    public ResultSet query(String sql) {
        try {
            stat = con.createStatement();
            rs = stat.executeQuery(sql);
        } catch (Exception ex) {
            Logger.getLogger(DataConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public void update(String sql) {//千万别close !!!
        try {
            stat = con.createStatement();
            stat.executeUpdate(sql);
        } catch (Exception ex) {
            Logger.getLogger(DataConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void update(String sql, String[] args) {
        try {
            pstat = con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                pstat.setString(i + 1, args[i]);
            }
            pstat.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(DataConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void close() {
        //check null,then close rs,stat,pstat and con.
    }
}

这里面使用的类都是java.sql包的,
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

你可能感兴趣的:(java,sql,mysql,jdbc,jpa)