Results 1 to 7 of 7
  1. #1
    shyamgsk is offline Member
    Join Date
    Sep 2013
    Location
    Hyerabad,India
    Posts
    20
    Rep Power
    0

    Default how to retrieve data using <select> tag from database

    I have a "title" with a drop-down menu beside it. This drop down menu has "n" options.which are entered in the database by myself


    When I click on title select tag, and the description of the title will have to display from database

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: how to retrieve data using <select> tag from database

    OK.
    So what have you managed so far?
    Where are you stuck?
    Have you got the value selected back to the server?
    Have you got a SQL query?
    Have you got JDBC code written?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    shyamgsk is offline Member
    Join Date
    Sep 2013
    Location
    Hyerabad,India
    Posts
    20
    Rep Power
    0

    Default Re: how to retrieve data using <select> tag from database

    here is my code
    when i click on <select> tag is it displaying only top most movie from database though it has many

    here is the screen shot of outputhow to retrieve data using &lt;select&gt; tag from database-localhost-8084-access1-movies.jsp.png
    and it is defaulty displaying description of that film

    Java Code:
    <%-- 
        Document   : movies
        Created on : 19 Sep, 2013, 1:48:50 PM
        Author     : Sravan
    --%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@page language="java"%>
    <%@page language="java" import="java.sql.*"%>
    <html>
     <head>
      <title></title>
      <meta http-equiv=Content-Type content="text/html; charset=windows-1252">
     
     </head>
     <body vLink=#6f6c81 link=#486591 bgcolor="#FFCC99">
     <h1 align="center">Movies</h1>
     <p align="center">&nbsp</p>
     <form method="post" action="item_purchase.jsp">
     <table border="2" align="center" cellpadding="7" cellspacing="7">
     <tr>
     <td width="399"><strong>Select A movie</Strong></td>
     <td width ="277">
         <select name="item" id="item">
      <option value="" selected>--select--</option>
    
      <%
      try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:mydatabase");
    Statement st=con.createStatement();
    ResultSet rs= st.executeQuery("Select * from moviedesc");
    while(rs.next())
    {%>
    
    <option value="<%String item= rs.getString(1); out.print(item);%>"><% out.print(item);%></option>
          <%
            String desc = rs.getString(2);
            out.println("<tr><td>"+desc);
            out.println("</td></tr>");
            }
      }
    catch(Exception e){
    out.println("Error");
    }
    %>
    </select>
         </td>
      
    </tr>
    
    </table>
    </form>
    </body>
    </html>

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: how to retrieve data using <select> tag from database

    Java Code:
    ResultSet rs= st.executeQuery("Select * from moviedesc");
    while(rs.next())
    {%>
    
    <option value="<%String item= rs.getString(1); out.print(item);%>"><% out.print(item);%></option>
          <%
            String desc = rs.getString(2);
            out.println("<tr><td>"+desc);
            out.println("</td></tr>");
            }
      }
    catch(Exception e){
    out.println("Error");
    }
    %>
    </select>
    Have a look at the html that's produced.
    Inside your loop you are adding new rows containing the description of the film, so the code is doing just that.
    Your html will be a complete mess, with <td> and <tr> tags inside the <select> tag, mushed up with the <option> tags.
    You cannot do both the select drop down and the description fields in one loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    shyamgsk is offline Member
    Join Date
    Sep 2013
    Location
    Hyerabad,India
    Posts
    20
    Rep Power
    0

    Default Re: how to retrieve data using <select> tag from database

    yeah thats the problem!
    can any one help me in code?

    it should display only the selected film description.
    what changes should i make in my code?

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: how to retrieve data using <select> tag from database

    Well, first fix the select tag.

    Then you need to decide how you are going about showing the description.
    Ajax call would be my choice, but I have no idea what technologies you know.
    Alternatively you could fill the table with the descriptions and show/hide the relevant row.
    Another alternative is to reload the whole page on a change of the select, and so only stick the selected film description in there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: how to retrieve data using <select> tag from database

    Quote Originally Posted by Tolls View Post
    Ajax call would be my choice, but I have no idea what technologies you know.
    This would indeed be an excellent moment to learn how to apply Javascript and JQuery.

    Another alternative is to reload the whole page on a change of the select, and so only stick the selected film description in there.
    And that is the easiest way to do it (onselect = submit form). It worked just fine in the 90's and early 2k's, it will still work great now :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 0
    Last Post: 06-28-2013, 08:17 AM
  2. Replies: 0
    Last Post: 01-20-2012, 06:44 AM
  3. how to retrieve data when i select the input from the form
    By sharanya in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 03-08-2011, 10:39 AM
  4. JSP code to connect, store and retrieve from database
    By HarryJade in forum New To Java
    Replies: 1
    Last Post: 12-26-2009, 03:45 AM
  5. retrieve file containg image from database
    By rajeshwari_ib in forum Advanced Java
    Replies: 5
    Last Post: 12-15-2009, 09:10 AM

Posting Permissions

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