Search

Discussion in 'Java JSP/Servlet' started by peggie1990, Jul 25, 2009.

  1. peggie1990

    peggie1990 New Member

    Hey guys, can help me with the serach cause I do not know how to proceed from here.

    Thanks.

    For example in the Search engine page right, I click in 4101, the 4101 record will be shown.
    If I click 3101, It will display no reocords.

    However, I dont know how to continue:(

    This is my search engine code:
    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@ page  import="java.sql.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
    <p>
    <%
            	Class.forName("com.mysql.jdbc.Driver");
        		//1b.  Get a connection to the database
            	Connection con = DriverManager.getConnection("jdbc:mysql://localhost/peisze", "root", "");
    			//1c. Construct our SQL statement
            	PreparedStatement ps = con.prepareStatement("SELECT * FROM products where ProductId LIKE '4%' ");
    			//1d.  Execute and retrieve our result
            	ResultSet rs = ps.executeQuery();
    			
    			//2. Base on the results returned, construct a table
            %>  
            </p>      
          
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    <form action="search.jsp" target="window.open('search.jsp');">
    <table border="0" cellpadding="2" align="center">
    <tr>
    <td colspan="3" align="center" height="140" valign="top"> 
    <h2>Search Engine Selector</h2>
    
    </td></tr>
    <tr>
    <td>ProductId: </td>
    <td>Search Engine:</td>
    <td> </td>
    </tr>
    <tr>
    <td>
    <input type="text" name="productId">
    </td>
    <td>
    <select name="Details">
    <option>choose one</option>
    <option value="ProductId">ProductId</option>
    
    
    </select>
    </td>
    <td>
    <input type="submit" value="Search">
    </td>
    </tr>
    </table>
    </form>
    
    
    
    </body>
    </html>
    This is the code that will display the info:
    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@ page import="java.sql.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <p>
    <%
    				//Retrieve the id of the product selected by the user
    				//the product id is sent via the URL as a parameter
    				int ProductId = -1;
    				String strProductId = request.getParameter("ProductId");
    				if(strProductId != null) {
    					//Convert from string to int
    					ProductId = Integer.parseInt(strProductId);
    				}
    				
    				//Load the database driver
    		       	Class.forName("com.mysql.jdbc.Driver");
    				//Create a connection to our database
    		       	Connection con = DriverManager.getConnection("jdbc:mysql://localhost/peisze", "root", "");
    				//create an SQL statement
    		       	PreparedStatement ps = con.prepareStatement("SELECT * FROM products WHERE ProductId=?");
    				//set the ID to be the id above
    		       	ps.setInt(1, ProductId);
    				//Execute and retrieve our result
    		       	ResultSet rs = ps.executeQuery();
    			%>
    </p>
    <%
            	//if there is a result, rs.next() will be true
            	//else it will be false
    			if(rs.next()) {
    		%>
    		<fieldset>
    <legend>Product Information</legend>
    <table border="0">
    
    <tr>
    		<td>   
            <div style=""><img border="3"  
                src="images/Funky/<%=rs.getString("ProdImage") %>" height="150" width="150" /></div>   
            </td> 
    
    		<td>Product Name: <%=rs.getString("ProdName")+"\t" %>
    
    		<div align="left">Product Color: <%=rs.getString("ProdColor")+"\t" %></div>
    
    		<div align="left">Product Description: <%=rs.getString("ProdDesc")+"\t" %></div>
    
    		<div align="left">Product Price: <%=String.format("$%.2f",rs.getDouble("UnitPrice"))+"\t" %></div>
    
    		<div align="left">Quantity: <%=rs.getString("Quantity")+"\t" %></div>
    		
    		
    		
    	
       
    		
    	    
    
    		</td>
    	</tr>
    
    
    	
    </table>
    </fieldset>
    
    <%
    			}
    			else {
    				//if no record is found, simply display a no record message
    		%>
    No record found.
    <%
    			}
    		 %>
    <p>&nbsp;</p>
    
    
    
    </body>
    </html>

    Thanks so much for you guys help:)

Share This Page