Solved

filtering  in a table of data by using servlet and microsoft access

Posted on 2006-11-01
1
216 Views
Last Modified: 2012-05-05
hi expert


this page get a list of books from the database ,when you press the button search  you will get a list of books from the microsoft access databse ,
i want to add a search featuer which is make the user search by a ISBN or Title by adding two text boxs ,  in the first if the user press the search button without enter any things in this two text boxs  the page will show a list of all books , but if the user enter some ISBN in the ISBN text box the page will show the book who have this ISBN , and if the user enter a title in the title text box  the page will show the books that have this title , how i can do this ,i am new in servlet and jsp   , i want to make this by servlet , i did this



 import java.io.*;
import java.net.*;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Collections;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author amohammad
 * @version
 */
public class ShowBooks extends HttpServlet {
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
       
       
        out.write("<script language=\"JavaScript\">\n");
        out.write("    function getIsbn() { \n" +

        "     document.forms(1).submit();\n" +
        "      \n" +
        "    }");
        out.write("</script>\n");
       
       
       
       
       out.println("<html>");
       out.println("<head>");
       out.println("<title>Servlet ShowBooks</title>");
       out.println("</head>");
       out.println("<body>");
       out.print(" <input type='hidden' name='isbn'/> ");
       out.print(" <input type='hidden' name='title'/> ");
       out.print(" <input type='hidden' name='userid'/> ");
       out.print(" <input type='hidden' name='quantity'/> ");
             
         out.println(" <input type=\"submit\" name=\"srch\"  size=\"60\" value=\"Search Book\"/>");
         out.println(" Title   <input type=\"text\" name=\"titleSrc\" />");
         out.println(" ISBN   <input type=\"text\" name=\"ISBNSrc\" />");

          out.println("<TABLE BORDER=1>");
          ArrayList ar = new ArrayList();
         try {
         
         
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         
             Connection connection = DriverManager.getConnection("jdbc:odbc:books","","");
          Statement statement = connection.createStatement();
     
          ResultSet results = statement.executeQuery("select * from books;");
         

         ResultSetMetaData resultsMetaData =
          results.getMetaData();
         int columnCount = resultsMetaData.getColumnCount();
         
 
         out.println("<TR>");
         for(int i=1; i<columnCount+1; i++) {
              out.println("<TH>"  +
              resultsMetaData.getColumnName(i) ) ;
         }
          out.println("<TH> Quantity </TH>");
          out.println("<TH> BUY </TH>");
          while( results.next() ) {
       
         out.println("<TR>");
         int f = 0;
         out.println("<form name=\"fm"+f+"\" action=\"ShowBooks\" method=\"Post\">");
          for(int i=1; i<columnCount+1; i++) {
             String gg = results.getString(i);
            ar.add(gg);
             
             
           
              out.print("<TD>" + gg + "<input type=\"hidden\"  name=\"par\" value="+ gg + "/>/></TD>" );
   
             
             
         }
         

            out.print("<TD> <input type=\"text\"  name=\"par\"   /> </TD>");
            out.print("<TD> <input type=\"submit\"  name=\"buy \" size=\"60\" value=\" BUY\"    /> </TD>");

          out.println("</form>");  
           f=f+1 ;
           
       }
          out.println("<form name=\"fmo\" action=\"ShowBooks\" method=\"Post\">");
     
            out.println("</form>");
         
                 statement.close();
                 results.close();
                 connection.close();
       }
       
       catch (Exception e) {
           System.out.println(e);
           
         
       }
       
        out.println("</TABLE>");
       
        out.println("</body>");
        out.println("</html>");
       
        out.close();
    }
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       
         response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet ShowBooks</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<form name=\"fm\" action=\"ShowBooks\" method=\"Post\">\n" +
        "      <table cellspacing=\"3\" cellpadding=\"2\" border=\"1\" width=\"100%\">\n" +
        "        <tr>\n" );
       
       
         out.println(" <input type=\"submit\" name=\"srch\"  size=\"60\" value=\"Search Book\"/>");
         out.println(" Title   <input type=\"text\" name=\"titleSrc\" />");
         out.println(" ISBN   <input type=\"text\" name=\"ISBNSrc\" />");

        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
       
     
       
    }
   
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();

String[] paramValues =
request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];


}
else
{

String Title = paramValues[0] ;
String ISBN  = paramValues[1] ;
String Publisher_Id  =   paramValues[2] ;
String URL  = paramValues[3] ;
String Price  = paramValues[4] ;
String Quantity  = paramValues[5] ;
String user = (String) request.getSession().getAttribute("userName")  ;  

if (Quantity==null || Quantity.equals(""))
{
    Quantity = "0";
}



DataBaseMethod db  = new DataBaseMethod();

db.insertNewOrder(user,Integer.parseInt(Quantity) ,ISBN ) ;

 response.sendRedirect("FinalServlet");

}
           
   
}  
             

       
   processRequest(request, response);    
       
       
       
       
    }
   
    }



this servlet show a list of all books without filtering on ISBN or Title , i just  but a text box to ISBN and Title without filtering  ,
please please help me i wait from any experts who can help me by sending a code to do this .


thank you  very very very very very very very very very very  much

0
Comment
Question by:moodyahmad
1 Comment
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 500 total points
ID: 17860460
change these lines..

         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         
             Connection connection = DriverManager.getConnection("jdbc:odbc:books","","");
          Statement statement = connection.createStatement();
String ISBN = request.getParameter("ISBNSrc");      
String Title = request.getParameter("titleSrc");
String qry = "select * from books";
if(ISBN!=null && ISBN.length()>0)
qry+=" where ISBN = "+ISBN;

if(Title!=null && Title.length()>0)
qry+=" where TItle = "+Title;

          ResultSet results = statement.executeQuery(qry);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
maven webservice example issue 2 156
java operators 3 125
JavaScript parse form with colon separated values 2 78
listing all functions in JavaScript 19 227
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Building a cohesive image for your brand is vital to making an impression on consumers. When the economy is tough, brands do better than unbranded  products. This can have a huge impact on your long-term profits, as the economy goes up and down.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

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