Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-11-01
1
Medium Priority
?
220 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Sometimes Administrators rights are not enough. These cases call for the SYSTEM account. The process in this article outlines the steps required to execute commands using the SYSTEM account.
Tech spooks aren't just for those who are tech savvy, it also happens to those of us running a business. Check out the top tech spooks for business owners.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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