• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

JSP bean and drop down boxes

First...I'm trying to store a data into a bean...The two text boxes that are used are called lets say text1 and text2 but i don't want to store these in the bean. I need text1 and text2 to query the database which i want to store in the bean.

Please supply some sample code of how i would store the querying piece of data in my bean?

Second...I would like to use drop down boxes for some date fields in my JSP docs but i don't know how to do them. I want to query the database when the focus is on the drop down box and query for most of the recent dates in order for the user to pick one of them. How do i query the data base and user drop down boxes at the same time. I can't seem to visulize it and example would help also.

  • 3
  • 2
1 Solution
Q1, not quite understand what you mean, do you mean use text1 and 2 as condition parameter to query database and get the value back? if this is the case, then:

PreparedStatement ps = conn.prepareStatement( "select * from table1 where column1=? and column2=? );
ps.setString( 1, request.getParameter( "text1" );
ps.setString( 2, request.getParameter( "text2" );
ResultSet rs = ps.executeQuery();
if( rs.next() ) {
   bean.setXXX( rs.getString( "xxxx" );

Q2, http is a request/response model. you can access database when user click the drop down button. you can only pre-polulate it. e.g.:
<% ResultSet rs = ..... %>
<select name="xxx">
<% while( rs.next() ) { %>
   <option><%=rs.getString( "column1" )%></option>
<% } %>

STKAuthor Commented:
Trying your solution to Q1 i wrote the following code but i don't think i'm quite right. What is wrong?

the error that tomcat is giving me is that the period after mybean is "cannot resolve symbol"

sql = dbconn.prepareStatement("SELECT user_name FROM member WHERE UPPER(user_name) = ? AND UPPER(password1) = ?");
            sql.setString(1, request.getParameter("username"));
            sql.setString(2, request.getParameter("password"));
            results = sql.executeQuery();
                 if(! doneheading)
                      out.println("<table border=2>");
                      doneheading = true;

do you have setString method in your bean? what property does you bean have? maybe something like mybea.setUserName() ?
STKAuthor Commented:
all i have is what is above and this. Yes i do have a set string in my bean

private String username

public void setUsername( String name ) {
        username = name;

<%@ page import="hello.memberhandler" %>

<jsp:useBean id="mybean" scope="application"
          class="hello.memberhandler" />
<jsp:setProperty name="mybean" property="username" />
NO, you don't have setString method, you have setUsername method!
use this:

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now