?
Solved

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

Posted on 2006-11-01
1
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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