Link to home
Start Free TrialLog in
Avatar of STK
STK

asked on

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.

Avatar of kennethxu
kennethxu

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>
<% } %>
</select>


Avatar of STK

ASKER

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();
           
            while(results.next())
            {
                 if(! doneheading)
                 {
                      out.println("<table border=2>");
                      doneheading = true;
                     
                 }
               
                 mybean.setString(results.getString("user_name"));

do you have setString method in your bean? what property does you bean have? maybe something like mybea.setUserName() ?
Avatar of STK

ASKER

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" />
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial