Solved

creating dropdown list in JSP querying the database

Posted on 2004-09-15
9
694 Views
Last Modified: 2012-05-05
Hi,
I am using beans and JSP. I wont to create a drop down list querying the access database.

Here is the code I have written so far,

package projBeans;
import java.beans.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.NumberFormat;

public class SearchBean {
            private Connection conn;
            Statement st;
            ResultSet rs;
            Vector ven_vec = new Vector();
            boolean processError = false;
            String ven_base;
            String Tag = null;
            private String dbDriver="sun.jdbc.odbc.JdbcOdbcDriver";

            public SearchBean() throws ClassNotFoundException, SQLException{
      Class.forName(dbDriver);
      System.out.println("*******Driver Loaded********");
      conn=DriverManager.getConnection("jdbc:odbc:planningdb","", "");
       System.out.println("*******Database onnected*****");
            }

      public void setTag(String Value) {
            this.Tag = Value;
            }
      public String getTag() {
            return this.Tag;
      }

public void processRequest(HttpServletRequest request,HttpSession session)throws ServletException,IOException {
             try{
             st=conn.createStatement();
       String sql="Select Distinct Model from [Base Inventory Table] where Vendor='"+request.getParameter("Tag")+"'Order By Model Asc";
           rs=st.executeQuery(sql);
                while(rs.next()) {
                  ven_base = rs.getString(1);
                    ven_vec.add(ven_base);                           this.processError = false;
                     }                              System.out.println(ven_vec.toString());            }catch(SQLException e){}
}

      public boolean getProcessError () {
                return this.processError;
      }

      public String getVen_vec() {
            return this.ven_vec.toString();
      }
}

JSP:**********************************JSP

BODY>
<%@ page language="java" session="true"%>
<jsp:useBean id="SearchBean" scope="session" class="projBeans.SearchBean" />
<jsp:setProperty name="SearchBean" property="*" />
<%
      SearchBean.processRequest(request,session);
      SearchBean.getVen_vec();
      for(int i=0;i< ven_vec.size(); i++) { %>
            <select name="vendor" >
            <option value = "ven_vec(i)"></option>
            </select>
      <%}%>
</BODY>
</html>

However, I am getting an error undefined variable/class name
for:ven_vec when i try to run it

Please help me,
Nubee
0
Comment
Question by:nubee
9 Comments
 
LVL 2

Expert Comment

by:arnon81
Comment Utility
Can you tell me which line number of ven_vec ?
0
 

Author Comment

by:nubee
Comment Utility
Thanks for your response arnon81,

com.sun.jsp.JspException:compilation failed:work\%3A8080%2Fexamples\ResultPage_jsp_1.java:102

is this the information you are asking about?

Thanks,
nubee
0
 

Author Comment

by:nubee
Comment Utility
its pointing to the line
for(int i=0;i<ven_vec.size();i++) { as an error
0
 
LVL 11

Expert Comment

by:raj3060
Comment Utility
use this;
<option value = "<%=ven_vec(i)%>"></option>
Should work
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:nubee
Comment Utility

I changed  <option value = "ven_vec(i)"></option> to <option value = "<%=ven_vec(i)%>"></option>

Now its giving two errors,

com.sun.jsp.JspException: Compilation failed:work\%3A8080%2Fexamples\ResultsPage
_jsp_1.java:102: Undefined variable or class name: ven_vec
                        for(int i=0;i< ven_vec.size(); i++) {
                                       ^
work\%3A8080%2Fexamples\ResultsPage_jsp_1.java:106: Method ven_vec(int) not foun
d in class examples.jsp.proj.ResultsPage_jsp_1.
                out.print(ven_vec(i));
                                 ^
2 errors

Nubee
0
 
LVL 5

Expert Comment

by:apparition
Comment Utility
Try this,


package projBeans;
import java.beans.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.NumberFormat;

public class SearchBean {
          private Connection conn;
          Statement st;
          ResultSet rs;
          Vector ven_vec = new Vector();
          boolean processError = false;
          String ven_base;
          String Tag = null;
          private String dbDriver="sun.jdbc.odbc.JdbcOdbcDriver";

          public SearchBean() throws ClassNotFoundException, SQLException{
     Class.forName(dbDriver);
     System.out.println("*******Driver Loaded********");
     conn=DriverManager.getConnection("jdbc:odbc:planningdb","", "");
      System.out.println("*******Database onnected*****");
          }

     public void setTag(String Value) {
          this.Tag = Value;
          }
     public String getTag() {
          return this.Tag;
     }

public void processRequest(HttpServletRequest request,HttpSession session)throws ServletException,IOException {
           try{
           st=conn.createStatement();
      String sql="Select Distinct Model from [Base Inventory Table] where Vendor='"+request.getParameter("Tag")+"'Order By Model Asc";
          rs=st.executeQuery(sql);
              while(rs.next()) {
               ven_base = rs.getString(1);
                 ven_vec.add(ven_base);                        this.processError = false;
                  }                         System.out.println(ven_vec.toString());          }catch(SQLException e){}
}

     public boolean getProcessError () {
              return this.processError;
     }

     public Object getVen_vec() {
          return this.ven_vec;
     }
}

JSP:**********************************JSP

BODY>
<%@ page language="java" session="true"%>
<jsp:useBean id="SearchBean" scope="session" class="projBeans.SearchBean" />
<jsp:setProperty name="SearchBean" property="*" />
<%
     SearchBean.processRequest(request,session);
     Vector ven_vec = new Vector (SearchBean.getVen_vec());
     for(int i=0;i< ven_vec.size(); i++) { %>
          <select name="vendor" >
          <option value = "ven_vec(i)"></option>
          </select>
     <%}%>
</BODY>
</html>
0
 
LVL 5

Expert Comment

by:apparition
Comment Utility
I tried the code and got some errors.....Here is the fixed version.

package projBeans;
import java.beans.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.NumberFormat;

public class SearchBean {
          private Connection conn;
          Statement st;
          ResultSet rs;
          Vector ven_vec = new Vector();
          boolean processError = false;
          String ven_base;
          String Tag = null;
          private String dbDriver="sun.jdbc.odbc.JdbcOdbcDriver";

          public SearchBean() throws ClassNotFoundException, SQLException{
     Class.forName(dbDriver);
     System.out.println("*******Driver Loaded********");
     conn=DriverManager.getConnection("jdbc:odbc:planningdb","", "");
      System.out.println("*******Database onnected*****");
          }

     public void setTag(String Value) {
          this.Tag = Value;
          }
     public String getTag() {
          return this.Tag;
     }

public void processRequest(HttpServletRequest request,HttpSession session)throws ServletException,IOException {
           try{
           st=conn.createStatement();
      String sql="Select Distinct Model from [Base Inventory Table] where Vendor='"+request.getParameter("Tag")+"'Order By Model Asc";
          rs=st.executeQuery(sql);
              while(rs.next()) {
               ven_base = rs.getString(1);
                 ven_vec.add(ven_base);                        this.processError = false;
                  }                         System.out.println(ven_vec.toString());          }catch(SQLException e){}
}

     public boolean getProcessError () {
              return this.processError;
     }

     public Object getVen_vec() {
          return this.ven_vec;
     }
}
JSP:**********************************JSP
<BODY>
<%@ page language="java" session="true"%>
<%@ page import = "java.util.*" %>
<jsp:useBean id="SearchBean" scope="session" class="projBeans.SearchBean" />
<jsp:setProperty name="SearchBean" property="*" />
<%
     SearchBean.processRequest(request,session);
     Vector ven_vec = new Vector ((Vector) SearchBean.getVen_vec());
     ListIterator iter = ven_vec.listIterator();
%>
 <select name="vendor" >        
<%        
      while (iter.hasNext()) {
%>
          <option value = "<%=(String)iter.next()%>"><%=(String)iter.next()%></option>
<%
      }
     
%>
</select>    
</BODY>
</html>
0
 
LVL 5

Accepted Solution

by:
apparition earned 35 total points
Comment Utility
oooops....

The JSP part has to be modified

<BODY>
<%@ page language="java" session="true"%>
<%@ page import = "java.util.*" %>
<jsp:useBean id="SearchBean" scope="session" class="projBeans.SearchBean" />
<jsp:setProperty name="SearchBean" property="*" />
<%
     SearchBean.processRequest(request,session);
     Vector ven_vec = new Vector ((Vector) SearchBean.getVen_vec());
     ListIterator iter = ven_vec.listIterator();
%>
 <select name="vendor" >        
<%        
      while (iter.hasNext()) {
            String value = (String)iter.next();
%>
          <option value = "<%=value%>"><%=value%></option>
<%
      }
     
%>
</select>    
</BODY>
</html>
0
 

Author Comment

by:nubee
Comment Utility
Hello apparition,

Sorry I didnot get back to you soon. I got some emergency and I didnot check this until today.

Anyway, I used your code and its working like a charm.

Thank You so much for all your help,
Nubee
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now