Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display data from dtabase when select drop down list?

 
Rahul kumar verma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<%
    try{
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db11?" + "user=root&password=xxxx"); /**database connection*/
       Statement statement = connection.createStatement(); /**connection created*/
resultset =statement.executeQuery("select distinct time_table_class_id from time_table_details" ) ; /**Query execute from drop down list */
%>

<center>
    <h1> Drop down box or select element</h1>
        <select id="semester" onchange="loadSemester();">
        <%  while(resultset.next()){ %>
        <option value="<%= resultset.getString(1)%>"><%= resultset.getString(1)%></option><br> <br/>
        <% } %>
        </select>
      <input id="semester" type="text" value="" />
</center> /**This is a drop down list*/
<%
        }
        catch(Exception e)
        {
             out.println("wrong entry"+e);
        }
%>
==============================================
Now my question is that in the database have so many duplicate time_table_id, corresponding distinct query put in drop down list.
Now in drop down list have distinct values but I want to select any of one drop down list that time all the duplicate values corresponding display on same jsp page.Thanks in advance.
 
Tapas Chand
Ranch Hand
Posts: 600
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Business logic should not be written in JSP. JSP is for representation only.
You should move the Java code to a Servlet, process the data there and forward the necessary data to JSP.
In JSP you can populate the dropdown with the data coming from servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:You should move the Java code to a Servlet, process the data there and forward the necessary data to JSP.
In JSP you can populate the dropdown with the data coming from servlet.

Even more so, the database access should not be in a servlet controller, but in a class within the model layer.

But at minimum, getting the code out of the JSP is a good first step.
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of even more importance, I think you have the (common) misconception that javascript and java are the same thing. 
They are not.

Java/JSP runs on the server.
Javascript runs on the client in your browser.

Specifically you can not directly run a database query from JSP in response to selecting an item from a dropdown in that JSP.
The only way to run java code as a response to clicking a button/selecting an item in a dropdown is to make an HTTP request.
The standard approach has been to use an "ajax" call to make a request to a web resource, take the result and use javascript to update the page appropriately.

I'm actually surprised Bear didn't already link you to The secret life of JSP pages
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:I'm actually surprised Bear didn't already link you to The secret life of JSP pages


Thanks for making up for my omission!

I'll also take the opportunity to link to this article.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!