Advertisement
Guest User

searchproduct

a guest
Apr 2nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. <%--
  2. Document : query
  3. Created on : Apr 03, 2016, 10:27:52 AM
  4. Author : mazlan
  5. --%>
  6.  
  7. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>NorthWind Search Product by Category</title>
  12. </head>
  13. <body>
  14. <h1>Find Product</h1>
  15. <h3>Enter Category :</h3>
  16. <form method="get">
  17. <input type="text" name="cateName" value="" />
  18. <input type="submit" value="Search">
  19. </form>
  20.  
  21. <%!
  22. //String[] lName = request.getParameterValues("lastName");
  23. String cateName = "";
  24. %>
  25. <%
  26. cateName = request.getParameter("cateName");
  27. if (cateName != null) {
  28. %>
  29. <%@ page import = "java.sql.*" %>
  30. <%
  31. Connection conn = DriverManager.getConnection(
  32. "jdbc:mysql://localhost:3306/northwind", "user", "1234"); // <== Check!
  33.  
  34. // Connection conn =
  35. // DriverManager.getConnection("jdbc:odbc:eshopODBC"); // Access
  36. Statement stmt = conn.createStatement();
  37. //a.ProductID,a.ProductName,b.CategoryName
  38. String sqlStr = "SELECT * FROM products a, categories b WHERE b.CategoryName like '%"+ cateName + "%' AND b.CategoryID=a.CategoryID ORDER BY ProductName ASC";
  39.  
  40. // for debugging
  41. System.out.println("Query statement is " + sqlStr);
  42. ResultSet rset = stmt.executeQuery(sqlStr);
  43. %>
  44. Query statement is <%= sqlStr %><br/>
  45. <h3>List of Product under category: <%= cateName %></h3>
  46. <hr>
  47. <table border=1 cellpadding=5>
  48. <tr>
  49. <th>Product ID</th>
  50. <th>Product Name</th>
  51. <th>Category</th>
  52. </tr>
  53. <%
  54. while (rset.next()) {
  55. int id = rset.getInt("ProductID");
  56. %>
  57. <tr>
  58. <td><%= rset.getInt("ProductID") %></td>
  59. <td><%= rset.getString("ProductName") %></td>
  60. <td><%= rset.getString("CategoryName") %></td>
  61. </tr>
  62. <%
  63. }
  64. %>
  65. </table>
  66. <br>
  67. <%
  68. rset.close();
  69. stmt.close();
  70. conn.close();
  71. }
  72. %>
  73. </body>
  74. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement