tangara goh

Ranch Hand
+ Follow
since Dec 27, 2009
I am a beginner in Java and hope to pick up the trade secrets so that I can move on to Android development
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by tangara goh

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 String[]subjectNames...

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.

    the row represent id and subjectNames which comes in 2 rows
    it is just one column for the list
    i use system.out.print ( id + rs.getInt("columnNameId") and subjectNames + rs.getString("columnNameSubj")
    sorry, what do you mean by embedded new lines ?
    i know because there are other conditions in the query that the id and subjects must satisified.

    Hope that you can clarify how to use List<String> to get the individual elements and add it to MyList.


    22 hours ago

    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.

    Hi Carey,

    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 String[]subjectNames...

    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...

    This is the part that I am seeking advice how to resolve it.

    23 hours ago

    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


    Hi Mathew,

    I think the problem lies with getting out the individual element.

    Could you let me know how to go about retrieving it ?

    Cos I am able to print out anything that is from the User model except the List<String> portion.
    1 day ago

    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?

    Hi Carey,

    It has more than one 'element' as the resultset shows in system.out.println in console

    user ID=54subjectName=LPriMath
    user ID=54subjectName=LPriEnglish

    Putting the jstl aside, how do I actually able to get the individual resultset out from the List<String> which is part of user ?
    1 day ago
    I have a resultSet which returns the elements.

    My problem is that I am not able to print out the elements of the rs.getString("columnName") in jstl.

    And I also do not know how to add the subj to MyList before setAttributes and forward it to the display page which contains the jstl c:out.

    Hope someone can give me some hints how to go about doing this part which I have been trying very hard to get it right but still can't get the elements in resultSet for subj.

    And because
    1 day ago

    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.

    So..first off.
    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?

    Hi Dave,

    Thanks so much for coming back with a reply.

    I would like to know if we want to execute a query, even if we don't set the FK or composite Key constraint, the query will still work right ?

    Kindly confirm about this part.

    Thanks again for your help.

    Really and greatly appreciate it.

    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)

    But, I didn’t ask about that one cos it was so Long ago about that comment why would I want to bring it up again?

    I think it’s ok if you are not willing to comment about my latest sql...

    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.

    Sorry Dave. Apparently I was the one that cause all the confusion. It is unique since I am using it as a Primary key.

    I presume my sql is correct since there is no comment about that part?

    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.

    Hi Dave,

    It is like a surrogate key.  Because, I was thinking if I were to use tutor_id as Primary key in my tutor table, and since it is not Unique, it will create problem later.

    So, it seems it is ok to use tutor_id as Primary key and reference my fk - tutor_subject(tutor_id, subjectName) ?

    Do you mind let me know the below sqls is it correct based on tutor_id as Primary key at tutor table?

    Alter table tutor_subject add constraint fk_constraint_t foreign key(tutor_id) references tutor_subject(tutor_id, subjectName)
    Alter table tutor add constraint fk_constraint_ts foreign key tutor_subject(tutor_id, subjectName)

    Do let me know if I have missed out anything...


    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.

    Yes.  I saw it in one of the articles.

    However, I did some more research again after reading your post and found that you are right.  

    Also, I read up and understand that there is a key(I think it is surrogate key) which runs on serial number so you don't name it as Primary Key.

    I need advice for my case where the tutor_id cannot be unique since it is a many to many relationship.  In my join table, if I were to make the tutor_id, subjectName composite key, do I make the reference foreign key as Serial Number key and not Primary key?

    Hope to have your view on this.


    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.

    I am not doubting you but I have done abit of research and I found out that one should avoid composite key as much as possible. So, what is the right way forward?

    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.

    So, how do I make tutor_id a Primary Key then? I have to use a surrogate key right, for my case?

    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.

    So, meaning tutor_id can still be made Primary key even though it contains repetitive value? Does it work in all kind of database?

    Knute Snortum wrote:So what is the primary key in the tutor_subject table?

    There is no primary key. At first, I thought it is tutor_id but since it can be more than 1 similar tutor_id, then it can’t be primary key right?

    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.

    Hi Dave,

    I just realised that I am running a problem because my tutor_id in my tutor_subject is not a primary key since it has more than one similiar tutor_id.

    Now, I need to use tutor_id to reference the tutor table tutor_id.

    How should I change the schema in this case to make the referencing work ?