Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Error on forgin key in sql

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error on forgin key in sql

    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
    Last edited by mike512; September 9th, 2019 at 07:33 AM.

Similar Threads

  1. Replies: 1
    Last Post: September 5th, 2019, 02:29 AM
  2. SQL statement is not executed! java.sql.SQLException: General error
    By ppz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 20th, 2014, 05:36 PM
  3. SQL Error
    By keletso in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 30th, 2013, 01:03 PM
  4. Error in connection sql server 2008 using netbeans 7.2
    By Indrajeet in forum JDBC & Databases
    Replies: 1
    Last Post: January 20th, 2013, 11:38 PM
  5. Replies: 6
    Last Post: August 18th, 2010, 05:41 PM