Link to home
Start Free TrialLog in
Avatar of moodyahmad
moodyahmad

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial