Solved

creating dropdown list in JSP querying the database

Posted on 2004-09-15
9
715 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Title # Comments Views Activity
getting Http 404 error in jsp forms 8 81
squirrelPlay java challenge 40 158
struts hibernate integration example 28 143
jsp login check 12 43
The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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 …

829 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