Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Not allowing user to insert duplicates in DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not allowing user to insert duplicates in DB" Watch "Not allowing user to insert duplicates in DB" New topic

Not allowing user to insert duplicates in DB

Kyupa Supa
Ranch Hand

Joined: Jun 23, 2012
Posts: 30
I want to restrict the user from entering duplicate assignments into DB, actually in the bridge table.

I have 2 tables: professors and courses an a bridge table containing prof_id and course_id as FKs.
In a jsp file user assigns professors to courses: 1 prof. to 1 or many courses.

I have:

In my method I receive and pname(professor's name) as parameters from the form in the JSP file

Knowing pname's id, the FK, I want to insert now into DB but to make sure user didn't choose 1 or more courses that the professor is already assigned to.

then I

where pname is a String(professor name), titles is a String Array(titles[] containing the courses I have to assign)

I tried with Set, Collections but I managed to enter the current courses+ again the already assigned courses
I also read about OUTER JOINS because I thought it might help.
I would kindly ask a reasonable way to solve it because I am intermediate...

Thank you.
caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

if i got your question right, you get a list of courses from the JSP which you want to assign to the prof but you don't want to assign him courses that he has already been assigned. this is what i would do.
convert the courses obtained from the JSP to a List object say an arrayList
then get a Collection of all courses from the database that the prof is already assigned to.
then do a List.removAll(Collection<?> c) which will remove all the courses that are contained in the collection from your List. then persist the remaining list of courses in the normal way you are using.


this is an attempt i am also new to java. you might want to consider other options

Kyupa Supa
Ranch Hand

Joined: Jun 23, 2012
Posts: 30
I did as so now and it works Thank you

It is sorta covered in the JavaRanch Style Guide.
subject: Not allowing user to insert duplicates in DB
Similar Threads
Add Strings to String Array in Java?
2 lookup tables and 1 bridge table
Exception String array in Java?
Persistence Example
Problems with in Hibernate