Solved

creating dropdown list in JSP querying the database

Posted on 2004-09-15
9
716 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
ID: 12068177
Can you tell me which line number of ven_vec ?
0
 

Author Comment

by:nubee
ID: 12068257
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
ID: 12068297
its pointing to the line
for(int i=0;i<ven_vec.size();i++) { as an error
0
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!

 
LVL 11

Expert Comment

by:raj3060
ID: 12068520
use this;
<option value = "<%=ven_vec(i)%>"></option>
Should work
0
 

Author Comment

by:nubee
ID: 12068794

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
ID: 12069533
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
ID: 12069769
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
ID: 12069796
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
ID: 12088726
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Direct mail marketing is the act of mailing materials straight to prospective customers. This wide form of marketing is one of the oldest methods of communicating with a geographic based demographic. So is it useful in 2017 and beyond?
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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