I use JDBC to read and write objects in DB.
In one of the objects (Coupon) there is a field of type CouponType (which is enum CouponType).
enum CouponType:
package db_package; public enum CouponType { RESTURANS("RESTURANS"), ELECTRICITY("ELECTRICITY"), FOOD("FOOD"), HEALTH("HEALTH"), SPORTS("SPORTS"), CAMPING("CAMPING"), TREVELLING("TREVELLING"); private String type; CouponType(String type) { this.type = type; } public String type() { return type; } } When I run the getCoupon method, I create a new coupon, fill its fields with data from the DB, and return it.
class CouponDBDAO () - getCoupon, creatCoupon methods:
public void createCoupon(Coupon coup) { Statement st; try { st = getStatment(); if(st != null){ st.execute(SQLConstantsQuery.INSERT_INTO_COUPON_VALUES + "(" + coup.getId() + ",'" + coup.getTitle() + "','" + coup.getStartDate() + "','" + coup.getEndDate() + "'," + coup.getAmount() + ",'" + coup.getType() + "','" + coup.getMessage() + "'," + coup.getPrice() + ",'" + coup.getImage() + "');"); System.out.println("Coupon " + coup.getTitle() + " added to DB"); }else{ throw new UpdateException("The Statement is null..."); } } catch (SQLException | UpdateException e) { e.printStackTrace(); } } public Coupon getCoupon(long id) { //Создание нового объекта Coupon для возврата Coupon coupon = new Coupon(); ResultSet rs; String typeFromDB; try { //Метод getStatment() возвращает Statment далее выполняем SQL запрос и получаем объект Coupon. rs = getStatment().executeQuery(SQLConstantsQuery.SELECT_COUPON_BY_ID + id ); while(rs.next()){ coupon.setId(rs.getLong(SQLConstantsQuery.COUPON_ID)); coupon.setTitle(rs.getString(SQLConstantsQuery.COUPON_TITLE)); coupon.setStartDate(rs.getDate(SQLConstantsQuery.COUPON_START_DATE)); coupon.setEndDate(rs.getDate(SQLConstantsQuery.COUPON_END_DATE)); coupon.setAmount(rs.getInt(SQLConstantsQuery.COUPON_AMOUNT)); typeFromDB = rs.getString(SQLConstantsQuery.COUPON_TYPE); CouponType ct = CouponType.valueOf(typeFromDB.toUpperCase(Locale.ENGLISH)); coupon.setType(ct); coupon.setMessage(rs.getString(SQLConstantsQuery.COUPON_MESSAGE)); coupon.setPrice(rs.getDouble(SQLConstantsQuery.COUPON_PRICE)); coupon.setImage(rs.getString(SQLConstantsQuery.COUPON_IMAGE)); } } catch (SQLException e) { e.printStackTrace(); } return coupon; } Everything would be fine, but when I do setType, the method expects a value of type CouponType from me, and I have a String. But it does not matter, I do:
CouponType ct = CouponType.valueOf(typeFromDB.toUpperCase(Locale.ENGLISH)); and pass ct to the setType method.
Now you can start Main by creating several objects before it.
Main class:
public class MainTest { public static void main(String[] args) { CouponDBDAO c = new CouponDBDAO(); Coupon coupon = new Coupon(6, "title4", "message3", "image3", 5.2, "2015-11-23", "2015-12-23", 773, CouponType.HEALTH); System.out.println(c.getCoupon(coupon.getId())); } } class Coupon:
public class Coupon { private long id; private String title, message, image; private double price; private Date startDate, endDate; private int amount; private CouponType type; protected static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd", Locale.US); public Coupon(){ } public Coupon(long id, String title, String message, String image, double price, String startDate, String endDate, int amount, CouponType type) throws ParseException { super(); this.id = id; this.title = title; this.message = message; this.image = image; this.price = price; this.startDate = formatter.parse(startDate); this.endDate = formatter.parse(endDate); this.amount = amount; this.type = type; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getStartDate() { return formatter.format(this.startDate); } public void setStartDate(Date startDate) { this.startDate = startDate; } public String getEndDate() { return formatter.format(this.endDate); } public void setEndDate(Date endDate) { this.endDate = endDate; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public CouponType getType() { return type; } public void setType(CouponType type) { this.type = type; } @Override public String toString() { return "Coupon [id=" + id + ", title=" + title + ", message=" + message + ", image=" + image + ", price=" + price + ", startDate=" + startDate + ", endDate=" + endDate + ", amount=" + amount + ", type=" + type + "]"; } } Well, now the most important thing that I want to ask is, after launch, I get an error:
Exception in thread "main" java.lang.IllegalArgumentException: No enum constant db_package.CouponType.ELECTRICITY at java.lang.Enum.valueOf(Unknown Source) at db_package.CouponType.valueOf(CouponType.java:1) at db_package.CouponDBDAO.getCoupon(CouponDBDAO.java:108) at db_package.MainTest.main(MainTest.java:54) and I can not understand for what? After all, there is such a constant. Please tell me what is wrong.