Hi im working on a coupon based project in java,i have a class for coupons which on of its values is a category (which is an enum) and from my understanding sql server converts my enum.type to a number ?
anyway,when i try to creat a Coupon object i get this error,i have tha tables and relationships set.
I need some help to know what to check since im unsure of the problem,here is the code for the Coupon class,maybe i need to create the categories on sql first?
Here are the codes~
The main file which causes the exception~
CouponDBDAO copdb = new CouponDBDAO(); try { copdb.addCoupon(new Coupon(8, Category.Food, "Coupon1", "Desc1", new Date(19000), new Date(23000), 5, 10, "image1")); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
The error~
java.sql.SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: The INSERT statement conflicted with the FOREIGN KEY constraint "Coupons_VS_CategoriesID". The conflict occurred in database "Coupon data", table "dbo.Categories", column 'ID'.
at coupon.db.CouponDBDAO.addCoupon(CouponDBDAO.java:3 7)
at ghf.main(ghf.java:46)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The INSERT statement conflicted with the FOREIGN KEY constraint "Coupons_VS_CategoriesID". The conflict occurred in database "Coupon data", table "dbo.Categories", column 'ID'.
I need some help to know what to check since im unsure of the problem,here is the code for the Coupon class
**CTR DBDAO USE ONLY** public Coupon(int companyID, Category category, String title, String description, Date startDate, Date endDate, int amount, double price, String image) { super(); this.companyID = companyID; this.amount = amount; this.category = category; this.title = title; this.description = description; this.image = image; this.price = price; this.startDate = startDate; this.endDate = endDate; }
and the add coupon method
@Override public void addCoupon(Coupon coupon) throws SQLException { Connection con = pool.getConnection(); try { PreparedStatement st = con.prepareStatement("insert into coupons values(?, ?, ?,?, ?, ?, ?, ?, ?)"); st.setInt(1, coupon.getCompanyID()); st.setInt(2, coupon.getCategory().ordinal() + 1); st.setString(3, coupon.getTitle()); st.setString(4, coupon.getDescription()); st.setDate(5, coupon.getStartDate()); st.setDate(6, coupon.getEndDate()); st.setInt(7, coupon.getAmount()); st.setDouble(8, coupon.getPrice()); st.setString(9, coupon.getImage()); st.execute(); } catch (SQLException e) { throw new SQLException(e); } finally { pool.returnConnection(con); } }
Solved,created a new Categories on database