[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

creating dropdown list in JSP querying the database

Posted on 2004-09-15
9
Medium Priority
?
755 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

Independent Software Vendors: 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!

Question has a verified solution.

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

What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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