Solved

How to refresh the values in the vector with new values

Posted on 2004-10-01
3
245 Views
Last Modified: 2010-04-01
Hello all,

I am using  a Bean to get the data from the database and populating that data using jsp. Vector is used to store the data from the resultset.

This is the scenario: SearchPage.jsp. User selects a vendor from listbox, Model from listbox and column from listbox and when hits enter
ResultsPage.jsp opens up and  Result is displayed in a table.

Step 2: If the user wants to search another thing, he hits back button on the form and gets to SearchPage.jsp. Hits Reset button which refreshes the form. And when the user selects the desired fields and hits Enter this time. He should only get the results from the 2nd query.

But, the code I am writing is allowing him to return the result from the 1st query and 2nd query in other words the new results are getting appended to the old values in the vector. I tried using vector.clear() method but when I used this the first result is also getting emptied nothing is showed in the result page.

This is the code I have written so far,

ResultBean.Java:

public class ResultBean {

            private Connection conn;
            Statement st;
            ResultSet rs;

            boolean processError = false;
            String ve;
            String mo;
            String co;
            String Ven=null;
            String Mod=null;
            String Col=null;
            String colval_base;
            Vector colval_vec = new Vector();
            String flag = "false";

            private String dbDriver="sun.jdbc.odbc.JdbcOdbcDriver";

      public ResultBean() throws ClassNotFoundException, SQLException{
                   Class.forName(dbDriver);
      conn=DriverManager.getConnection("jdbc:odbc:planningdb","", "");
            }

            public void setMod(String Value) {
                        this.Mod = Value;
            }

            public String getMod() {
                        return this.Mod;
            }

            public void setVen(String Value) {
                        this.Ven = Value;
            }

            public String getVen() {
                        return this.Ven;
            }

            public void setCol(String Value) {
                        this.Col = Value;
            }

            public String getCol() {
                        return this.Col;
            }
public void processRequest(HttpServletRequest request,HttpSession session)throws ServletException,IOException {
      ve = request.getParameter("Ven");
      mo = request.getParameter("Mod");
      co = request.getParameter("Col");

      try{
       st=conn.createStatement();
       rs=st.executeQuery("Select Distinct [" +request.getParameter("Col")+ "] from [Base Inventory Table] where Vendor = '"+request.getParameter("Ven")+"' and Model = '"+request.getParameter("Mod")+"'");

                   while(rs.next()) {
            colval_base = rs.getString(1);
                                                           }

                }catch(SQLException e){}

}
      public String getColval_base() {
            return this.colval_base;}
      public Object getColval_vec() {
                  return this.colval_vec; }

}


Main Page.sp: Code was written to clear the values in the vector when Reset button in the SearchPage.jsp was hit.

<!--This is the controlling jsp.It forwards requests and contrl to the appropriate JSP-->

<html>
<head>
<title>
MainPage
</TITLE>
</head>
<BODY>

<%@ page language="java" session="true" %>
<%@ page import = "java.util.*" %>

<jsp:useBean id="ResultBean" scope="session" class="projBeans.ResultBean" />
<jsp:setProperty name="ResultBean" property="*" />


 
 
<% if(request.getParameterNames().hasMoreElements() == false){ %>
      <jsp:forward page="InitialPage.jsp" />
  <%      }%>


<% if(request.getParameter ("Enter") !=null) {%>
<jsp:forward page="CheckEmpPage.jsp" />
<%      }%>


<% if(request.getParameter ("Go") !=null) {%>

<jsp:forward page="ResultsPage.jsp" />
<%      }%>


<% if(request.getParameter ("Reset") !=null) {

     ResultBean.processRequest(request,session);
     
     Vector colval_vec = new Vector ((Vector) ResultBean.getColval_vec());
          colval_vec.clear();

      }%>
 
<%  String choice = request.getParameter("choice");

if(choice.equals("search") ) { %>

<jsp:forward page="SearchPage.jsp" />

<% }

if(choice.equals("administer") ) { %>

<jsp:forward page="LoginPage.jsp" />

 <% } %>

  </BODY>
  </HTML>
   

Sorry for the length,

Please help,
Thanks
Nubee
0
Comment
Question by:nubee
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 50 total points
ID: 12210040
There might be a couple of things.

first make sure you clear the vector before you add anything to it.
second, if the clearing of the vector doesn't work you can try something else..

After you display the results
1) set the vector to null (ex. colval_vec = null;)
2) reinstanaite it. (ex colval_vec = new Vector ((Vector) ResultBean.getColval_vec());

another thing I saw...

 public Object getColval_vec() {
               return this.colval_vec; }

this is returning an empty vector..
are you sure this is what you want to be returned...
and not this one..
colval_base

Cheers,
Ghost
0
 

Author Comment

by:nubee
ID: 12219088
You are right. Why didnot I try clearing the vector before adding anything. Its such a simple thing and It didnot strike my nut head.
 Thanks a lot. It helped a lot.

0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 12219523
no problem
Josh
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

19 Experts available now in Live!

Get 1:1 Help Now