?
Solved

creating dropdown list in JSP querying the database

Posted on 2004-09-15
9
Medium Priority
?
731 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 140 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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