Solved

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

Posted on 2006-11-01
1
213 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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: …

728 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

9 Experts available now in Live!

Get 1:1 Help Now