Results 1 to 4 of 4
  1. #1
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default Loop through an Array and invoke SQL command

    Hi Guys,

    I've got a big problem with these JSP/JSPX pages. I'm sure you people can help me.

    Basically I am passing an array of strings from a Java servlet to a JSP page where I am trying to go through the array using 'forEach' loop and for each element, invoke a MYSQL command.

    The array is called "Websites".

    Here is my code:

    <c:forEach var="e" begin="0" step="1" end=${Websites.length}>

    <sql:query var="table">
    select SUM(usability) as number from ${Websites[e]};
    </sql:query>
    .......

    I have two main problems, firstly in the ForEach Tag, "Website.length" is not working, it returns SQL error at run time.
    secondly in the MySql command, "${Website[e]}" is not working too, it returns SQL error at run time as well.

    PLease Please help because I have been spending so much time on this.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Posting exceptions might help.
    I only ask because if Websites is simply an array there is no way it'll throw a SQLException in the for-each loop.

    I would also suggest that you do the SQL in the servlet and simply pass all the necessary data into the JSP page. It's be a lot easier to debug, if nothing else. Data gathering sholdn't really be the responsibility of the JSP.

  3. #3
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default

    Thanks for reply, but Toll, your answer does not make sense to me (sorry i am not very good with jsp), this is my exact problem that I don't know whether my forEach loop is correct or not, that was actually my question, is my forEach loop correct?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Well, the problem is you say it's throwing a SQL exception...which can't be the case from a for-each, unless Website is doing something odd.

    Posting the exceptions helps us to understand your problem.

Similar Threads

  1. Finding a the max value of the array using a for loop
    By soccer_kid_6 in forum New To Java
    Replies: 1
    Last Post: 04-11-2010, 11:25 PM
  2. Replies: 22
    Last Post: 01-05-2009, 01:03 AM
  3. How do I create an array with every cycle of a loop?
    By blackhole8746 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 07:49 AM
  4. Replies: 2
    Last Post: 05-05-2008, 01:56 PM
  5. Timing array loop performance
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •