Skip to content
Advertisement

SQL query in Java with enum return error of Incorrect integer value:

I get this error massage: java.sql.SQLException: Incorrect integer value: ‘xACxEDx00x05~rx00…

the query is working, but if I put the category name (Category.Electricity) its return error. I call it with statement.setObject(…) meybe this is wrong..?

the main class:

CouponsDbDao coupDbDao = new CouponsDbDao();
            Coupon coupon = new Coupon(2, 1, Category.Electricity, "dhfjsf", "fdfshjd jfkds", null, null, 50, 10, null);
            coupDbDao.addCoupon(coupon);

the enum class:

public enum Category {
    Food(1), Electricity(2), Restaurant(3),Vacation(4);

    private int id;
    private Category(int i) {
        this.id = id;
    }
    public int getId() {
         return id;
    }
}

the method:

public class CouponsDbDao {
    public void addCoupon(Coupon coupon) {
        try {
        Connection connection = JdbcUtils.getConnection();
        String sqlStatement = "insert into coupons (COMPANY_ID,CATEGORY_ID,TITLE,DESCRIPTION,START_DATE,END_DATE,AMOUNT,PRICE,IMAGE) values(?,?,?,?,?,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sqlStatement);

            statement.setInt(1, coupon.getCompanyId());
            statement.setObject(2, coupon.getCategory());
            statement.setString(3, coupon.getTitle());
            statement.setString(4, coupon.getDescription());
            statement.setDate(5, coupon.getStartDate());
            statement.setDate(6, coupon.getEndDate());
            statement.setInt(7, coupon.getAmount());
            statement.setDouble(8, coupon.getPrice());
            statement.setString(9, coupon.getImage());
            statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

so what is the way to call the enum?

thank you!

Advertisement

Answer

seems like your table is expecting CATEGORY_ID
so change

        statement.setObject(2, coupon.getCategory());

to

        statement.setInt(2, coupon.getCategory().getId());
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement