Swastik
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Swastik Dey wrote:This itself returns an array of String.
So you need to do something like this
Swastik
Swastik
Swastik Dey wrote:You don't need any ArrayList here.
The following code snippet is fair enough to retrieved the check box elements from html page.
As an example if 2 check boxes are checked, size will be 2, if 4 check boxes are checked, size will be 4. The ArrayList thing you are doing is a wrong way. getParameterValues always returns a String array.
Swastik
Swastik Dey wrote:Can you provide us some more input here? Like what is table structure. How are saving the subjects into table etc. It might help us to dig into it little more.
Swastik
Swastik Dey wrote:Fair enough. But it's still not clear what is your database table design. How many fields does it have. Are you saving the subjects row wise, or in a single column using some delimiters.
Swastik
Swastik
tangara goh wrote:So, if later I'm going to search functionality, if this tutor has say Chemistry and Biology in her portfolio, her name and other people names that has ticked Chemistry and Biology will appear in the Search List.
Paul Clapham wrote:
tangara goh wrote:So, if later I'm going to search functionality, if this tutor has say Chemistry and Biology in her portfolio, her name and other people names that has ticked Chemistry and Biology will appear in the Search List.
You should stop asking about the code you want to write. Several people have asked you about your database design and instead of answering, you just ask about writing code again. Nobody can help you that way.
So let me ask this: if the "subject" parameter returns you an array containing "Chemistry" and "Biology", should this create two database records, one containing "Chemistry" and the second containing "Biology"? Or did you choose the un-normalized design where you have several columns in one table for subjects? It's impossible for anybody to discuss code to update your database without knowing at least that.
Paul Clapham wrote:I saw that but I was astonished that you planned to put more than one subject in a column named "subject". That's why I wasn't convinced I could understand you.
So did I understand that correctly? In a normalized database design, if a tutor had three subjects then you would put those in three separate database rows, so that you could readily find tutors for a particular subject. But you're really planning to put all of the subjects in one field in one row?
Okay. If so, how do you plan to get them all in? You need to decide that before you write code and I'm pretty sure you haven't mentioned what your plan is.
tangara goh wrote:Please find attached, you can see all the subject in the same column.
So, how do you set the Array in Prepared Statement ?
Dave Tolls wrote:You have Subjects, Tutors and Students.
To me that's 3 tables...possibly 2 if Tutors and Students are close in content, but I'd start with 3.
Tutors and Students both have Many-to-many relationship with Subjects, so there'll be a mapping table for each.
Trying to squeeze a Many-to-Many into a single database column as you are is not good database design.
tangara goh wrote:
It seems like nobody has the answer and I am kind of surprise because this should be considered still as a beginner topic .........
Swastik
Dave Tolls wrote:OK, so no mapping tables, but each Subject table has a foreign key to the Tutor table.
Subject: id, name (varchar), tutor_id (int)
Tutor: id, name, etc etc
is that the table structure you have?
Swastik
Swastik
Swastik Dey wrote:Even if it works, may be subjects are delimited with comma or something else, but of course not a correct design.
Swastik
Swastik
Dave Tolls wrote:Your checkboxes can be driven by the data in the Subject table, whether as ids or the names used in the table.
Either way, you'll have an array of values that you can then:
UPDATE Subject
SET tutorId = ?
WHERE name IN (....comma seperated list of values)
Dave Tolls wrote:Well, I used 'name' since I was guessing you might be sending the subject name (which I would assume was unique).
You could always send a subject id. That's up to you.
For the IN, the most secure way is to build the list of '?' placeholders and the do a load of setString (or setInt if using ids) on the PreparedStatement.
The initial answer on this SO thread is the usual way to build up the IN, using String.join and Collections.nCopies...it's a lot easier than it used to be!
Swastik
Swastik Dey wrote:
The above statement is completely wrong. What are you trying to do? Create a new record or update existing records?
The above statement is wrong as well. I believe by m1 you are referring to subject class. The setSubject method is expecting a List of type Subject but you are passing a String array. So you need to convert this String array to a list of type subject before you pass it to the setSubject method.
Last but not the least, naming convention. Ideally class names in java start with upper case characters.
Dave Tolls wrote:I think there's a misunderstanding here.
What does your SUBJECT table look like?
My thinking was that there would be a fixed list of subjects in the table.
You would then assign a tutor to each subject based on what has been selected by the check boxes.
But it sounds like there are no subjects in the table, and they are only added when a tutor is set up?
Consider Paul's rocket mass heater. |