using sql result set in jsp code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cookspyder
    New Member
    • Jul 2007
    • 20

    #1

    using sql result set in jsp code

    I am havign trouble understanding how to call a string that was queried with the sql in my jsp.

    I have a result set
    [CODE=java]<%
    while (rs.next()) {
    String test = rs.getString("u ser");
    out.println(tes t);
    }
    %>
    [/CODE]
    and the string can be printed here how ever I need to call the "test" string in jsp some jsp code further down the page and it can not see the string variable is there a way to pass the queried string into the jsp? Sorry Im not a big JSP coder so Im just trying to get it working. thanks
    Last edited by acoder; Nov 16 '07, 06:34 PM. Reason: Added code tags + moved to Java forum
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by cookspyder
    I am havign trouble understanding how to call a string that was queried with the sql in my jsp.

    I have a result set
    [CODE=java]<%
    while (rs.next()) {
    String test = rs.getString("u ser");
    out.println(tes t);
    }
    %>
    [/CODE]
    and the string can be printed here how ever I need to call the "test" string in jsp some jsp code further down the page and it can not see the string variable is there a way to pass the queried string into the jsp? Sorry Im not a big JSP coder so Im just trying to get it working. thanks
    If you have
    [CODE=java] <%
    String test = null;
    while (rs.next()) {
    test = rs.getString("u ser");
    out.println(tes t);
    }
    //can use test here
    %>[/CODE]
    then you can use test after the while loop.
    Note however, a resultset may contain multiple rows so test will have the value of the last row only

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      From a design perspective no JSPs should contain database handling stuff. JSPs
      should concentrate on the view of the application. Servlets, which should act as
      the controllers should delegate to beans that should contain the backend logic,
      the model; the model should ideally encapsulate that darn SQL stuff (DAOs,
      DTOs and DVOs).

      Whenever I see sql (jdbc) code in a JSP I go "*OMG*! not again! eeeeww!"

      kind regards,

      Jos

      Comment

      Working...