Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2005
    Posts
    395

    JSP: JSTL SQL query

    I have an sql table with two columns (var,value), and I am looking to retrieve the value of a specific row (where the var value is "usercount") via jstl:

    Code:
    <sql:query sql="SELECT `value` FROM `database`.`table` WHERE `var`='usercount'" var="result"/>
    The "result" variable is theoretically a array, but the result of the query should only be 1x1 array. Is there a way to denote the first cell[0,0] and just grab the cell to save as a variable, rather than have to walk through the array?

  2. #2
    Join Date
    Jul 2006
    Location
    Detroit, MI
    Posts
    1,955
    Quote Originally Posted by DanCF View Post
    I have an sql table with two columns (var,value), and I am looking to retrieve the value of a specific row (where the var value is "usercount") via jstl:

    Code:
    <sql:query sql="SELECT `value` FROM `database`.`table` WHERE `var`='usercount'" var="result"/>
    The "result" variable is theoretically a array, but the result of the query should only be 1x1 array. Is there a way to denote the first cell[0,0] and just grab the cell to save as a variable, rather than have to walk through the array?
    Why isn't the 'result' varriable a resultset/rowset?

  3. #3
    Join Date
    Dec 2005
    Posts
    395
    Quote Originally Posted by utropicmedia-karl View Post
    Why isn't the 'result' varriable a resultset/rowset?
    Maybe thats what it is. I am not sure (I just started learning JSP this weekend).

    I guess my question is, is there an easy way to convert the 1x1 size of whatever format it is, to a regular variable?

  4. #4
    Join Date
    Jul 2006
    Location
    Detroit, MI
    Posts
    1,955
    Quote Originally Posted by DanCF View Post
    Maybe thats what it is. I am not sure (I just started learning JSP this weekend).

    I guess my question is, is there an easy way to convert the 1x1 size of whatever format it is, to a regular variable?
    You typically get a collection back and then operate on it as you see fit. It's not a 1x1 size unless that is what your result should be, and then still is wrapped in the collection.



    Regards,

  5. #5
    Join Date
    Dec 2005
    Posts
    395
    In this case it will always be 1x1, so is there a quick way to "unwrap it" from the collection without having to walk through the array?

  6. #6
    Join Date
    Aug 2002
    Location
    Superior, CO, USA
    Posts
    633
    Quote Originally Posted by DanCF View Post
    I have an sql table with two columns (var,value), and I am looking to retrieve the value of a specific row (where the var value is "usercount") via jstl:

    Code:
    <sql:query sql="SELECT `value` FROM `database`.`table` WHERE `var`='usercount'" var="result"/>
    The "result" variable is theoretically a array, but the result of the query should only be 1x1 array. Is there a way to denote the first cell[0,0] and just grab the cell to save as a variable, rather than have to walk through the array?
    You've got a few issues here. The first is that you should use a parameterized query, not insert a string directly. Something more like:

    Code:
    <sql:query sql="SELECT value FROM database.table WHERE var= ?" var="result">
     <sql:param value="usercount" />
    </sql:query>
    But to answer your last question, no, there isn't a generic way to do this. I'm not sure what would be wrong with the code:

    Code:
    <c:forEach var="row" items="${result.rows}"><c:out value="${row.value}"/></c:forEach>
    This just output your value. And if you're wrong and there is more than one value then you'll find a bug at the same time.
    Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.

  7. #7
    Quote Originally Posted by DanCF View Post
    In this case it will always be 1x1, so is there a quick way to "unwrap it" from the collection without having to walk through the array?
    You can set the start and end point of the result set. Example below will return the first row of the return set from the <sql:query>.

    Code:
    <c:forEach var="row" items="${result.rows}" begin="0" end="0">
      <c:out value="${row.value}"/>
    </c:forEach>
    Hope this helps you.

  8. #8

    how to store the output in variable??

    <c:forEach var="row" items="${result.rows}"><cut value="${row.value}"/></c:forEach>

    this code output the rows...but can we store this output in a string variable...
    and also i have to use these values outside the <c:foreach statement
    can you help me pls....

Posting Permissions

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