• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

get the individual element of a List<String> in a resultSet for JSTL display in JSP?

 
Ranch Hand
Posts: 545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks.
And because
 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by cleaning-up the code.  It it poorly formatted and has multiple syntax errors.
 
Ron McLeod
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote: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.


How are you using JSTL in your JSP page?
 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

https://www.javatpoint.com/jstl

 
tangara goh
Ranch Hand
Posts: 545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ?
 
tangara goh
Ranch Hand
Posts: 545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

https://www.javatpoint.com/jstl



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.
 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually when you store multiple 'elements' in a string you separate them with a delimiter (e.g. '|') that is easy to parse later.
LPriMath|LPriEnglish
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.

 
tangara goh
Ranch Hand
Posts: 545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
     
    tangara goh
    Ranch Hand
    Posts: 545
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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.

    Tks.

     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    tangara goh wrote:the row represent id and subjectNames which comes in 2 rows

    How can a row come in two rows. That's a contradiction. The only way I can see that it appears in your print is that it has an embedded new-line, as in:
    user ID=54subjectName=LPriMath\nuser ID=54subjectName=LPriEnglish
    You have an '=' as the delimiter between the field name and the data, but what separates one data from the next field name?

    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 ?

    Having the character, which in Java is '\n', inserted in the middle of the string. If you print the string the '\n' is not printed as such but causes the print to continue on the next line. You need delimiters in order to break apart the string. So far you have '=' and possibly '\n'. This is good but doesn't address the data vs field name issue. You'll have to tell us how you plan on solving that.

    What is the content of the "columnNameId"? I could see it containing a userId but that's not what the name implies. So what does it represent? Can you give an example of one of its entries?

    [edit]Ah, I see. You (or someone else) has made poor choices for column names. You don't want every column name to begin with "columName", that is useless, redundant, and confusing. Column names should clearly indicate the nature of the contents, such as: USER_ID and SUBJECTS.
     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is there only one row per userId? If it's multiple then how do you handle duplicate userId's?
     
    tangara goh
    Ranch Hand
    Posts: 545
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Is there only one row per userId? If it's multiple then how do you handle duplicate userId's?



    Yes.  One row per userId.

    I need to have duplicates because of the query condition.

    Erm...may I know if it is easier to use Stream to make the return of resultset into collection and then print ?

    I have been googling whole day but I feel that without enough experience in knowing even the basic of stream and practising it is difficult to implement the code seeing examples on line.

    Hope you could give me some hints how to do this part (not the streaming) but the part where I need to add to the resultset of columnNameSubj" to myList.

    Thanks.
     
    Carey Brown
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    tangara goh wrote:

    Carey Brown wrote:Is there only one row per userId? If it's multiple then how do you handle duplicate userId's?

    Yes. One row per userId. I need to have duplicates because of the query condition.

    One row per userId yet you have duplicate? That doesn't make sense. Duplicate what?

    You haven't sufficiently nailed down the requirements so deciding on how to implement is premature. I could not build it based on the information you've provided so far.

    I would forget about streams for now until you better understand the algorithm you will be attempting to implement. Maybe streams will be the right choice and maybe not.
     
    tangara goh
    Ranch Hand
    Posts: 545
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ron McLeod wrote:

    tangara goh wrote: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.


    How are you using JSTL in your JSP page?



    Hi Ron,

    Here's the layout I plan to get the result onto the jstl :



    Could you let me know how do I write it in order for the List of subjects to appear in rows and not just return in a straight line horizontally.

    And am I right I need to use Map ?

    Thanks.
     
    Master Rancher
    Posts: 4208
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you want a subject per row (along with all the other details) then both for-each loops need to be outside the <tr> element.
     
    tangara goh
    Ranch Hand
    Posts: 545
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:If you want a subject per row (along with all the other details) then both for-each loops need to be outside the <tr> element.



    Hi Dave,

    Thanks.  The subject is now in per row.  However, the data returned is only the last row of data and each row only contains just the last row of data repeatedly.

    What do you think is happening ?

    Is there any debugging technique in order to see the cause ?

     
    Destiny's powerful hand has made the bed of my future. And this tiny ad:
    professionally read, modify and write PDF files from Java
    https://products.aspose.com/pdf/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!