Carey Brown wrote:
tangara goh wrote:Is it not possible to use List<String> to get the individual element ?
Because i can't see how easy it can be parsed later using StringsubjectNames...
The query is done using what you have mentioned but I am having problem with getting individual element from List<String> as I can't add it to User....
because from rs.getString("columnName") I can print it out in 2 rows but I can't add it to User which has this List...
What does a row represent? What are the column names? How are you printing multiple id/subject rows when you are only getting a single row/column from the database? Does that id/subject string have embedded new-lines? How do you know when userId ends and subject begins?
If you start with an array you can convert to a List if need be. Don't get hung up on that.
Carey Brown wrote:Usually when you store multiple 'elements' in a string you separate them with a delimiter (e.g. '|') that is easy to parse later.
Alternatively, when you have a one-to-many relationship in a database you use a second table that has a column which is a key (e.g. userId) that refers back to the first table, and the one or more columns (e.g. subject) that relate back to the first table. For example, for userId=123 you'll have only one row in the first table but zero to many rows in the second table to hold the subjects that the student is taking. This is probably the preferred route to take.
Matthew Keller wrote:jsp's and actually JEE architecture have a handfull of scopes that you can send attributes to
page scope - lives as long as the jsp is being rendered
request scope - lives as long as the request
session scope - lives as long as the user's session is active
application scope - global area that all sessions can access
The usual jsp pattern is to put a formbean into your session scope and write to it from your java. In struts you would write the java in your action class. In Spring MVC you would write this in your controller method. Both of these places are really just framework flavors of controller servletts.
The next step in the pattern is to send the thread onto a jsp (also a servlett) using a mapping. The jsp can access the formbean and display your data using a jstl tag. I love jstl because it is so damn simple.
Look for tags like c:out, c:foreach, c:if to do things in your jsp with jstl. Here is a reference. Jstl also has a $ shorthand, but you have to be careful not to confuse that with jquery's $ shorthand
Carey Brown wrote:
tangara goh wrote:My problem is that I am not able to print out the elements of the rs.getString("columnName") in jstl.
There is only one value ("element" not "elements") for columnName per row, so your inner loop doesn't make any sense.
I'm not familiar with 'jstl', what is it?
Dave Tolls wrote:Sorry, I completely missed those!
ALTER TABLE tutor_subject ADD CONSTRAINT fk_constraint_t FOREIGN KEY(tutor_id) REFERENCES tutor_subject(tutor_id, subjectName)
The foreign key is against the tutor table.
That looks like you are defining it against itself...and even then I'm not sure that would actually execute, would it, as it says the tutor_id references a tutor_id and subject_name?
ALTER TABLE tutor ADD CONSTRAINT fk_constraint_ts FOREIGN KEY tutor_subject(tutor_id, subjectName)
And this one seems to be missing anything to say what is being mapped.
The foreign key is defined on the table where the key is foreign, ie the "master" is in another table.
So tutor_subject has a foreign key in the form of the tutor_id which references the tutor_id on the tutor table.
The tutor table has no foreign key, at least in this relationship.
Finally, the tutor_subject table needs a primary key, consisting of the tutor_id and the subject_name, since I assume there is no subject table?
Dave Tolls wrote:I've already commented on the SQL, and how it should be an IN.
SELECT tutor_id, subjectName FROM tutor_subject WHERE subjectName IN (need to build up the correct number of '?' in here)
Dave Tolls wrote:tutor_id is not unique?
So more than one tutor can have the same id?
OK...then tutor_id is not the primary key on the tutor table, so you need to use whatever the primary key is.
Your naming is a bit confusing then.
Dave Tolls wrote:I'm lost now.
What's the Serial Number key?
Your join table should join the primary key of the tutor table to the primary key of the subject table.
Those two columns in the join table are that table's primary key.
Knute Snortum wrote:Did it say why you should avoid composite keys? I've been out of the workforce for several years, but why I was coding we used composite keys all the time. Maybe it's fallen out of favor.
Dave Tolls wrote:You don't make tutor_id a primary key for the tutor_subject table.
You make tutor_id and subject_id the primary key. It's a composite.
How that works for the db you are using you will have to look up.
Dave Tolls wrote:No, both columns are the primary key. It's a composite.
You can't have a primary key which is not unique.
Dave Tolls wrote:I would expect it to be (based on the name) tutor_id and subject_id.
Or maybe tutor_id and subject, if they're just a name.
Knute Snortum wrote:So what is the primary key in the tutor_subject table?
Dave Tolls wrote:Try and do it as one query.
In general it's best to get the database to do the filtering as that's the sort of thing it's designed to do.
It's likely to be better at it than code you would write.
Not that finding the tutor address is filtering.
As you said, it's joining.