Guest User

Untitled

a guest
Jan 17th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. <%@page import="java.sql.Connection"%>
  2. <%@page import="databaseManagement.DBConnection"%>
  3. <%@page import="java.sql.ResultSet" %>
  4. <%@page import="java.sql.SQLException"%>
  5. <%@page import="java.sql.Connection"%>
  6. <%@page import="java.sql.PreparedStatement"%>
  7.  
  8. <?xml version="1.0" encoding="ISO-8859-1" ?>
  9. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  10. pageEncoding="ISO-8859-1"%>
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  12. <html xmlns="http://www.w3.org/1999/xhtml">
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  15. <title>Search Our Products</title>
  16. </head>
  17. <body>
  18. <form method="post">
  19. Search:<input type="text" name="Search">
  20. <input type="submit" value="Go">
  21.  
  22. <table border="2">
  23. <tr>
  24. <td>ID</td>
  25. <td>NAME</td>
  26. <td>DESCRIPTION</td>
  27. <td>PRICE</td>
  28. </tr>
  29. <%
  30.  
  31. try
  32. {
  33. DBConnection db = new DBConnection();
  34. Connection con = db.getConnection();
  35. PreparedStatement ps = con.prepareStatement("select * from products");
  36. ResultSet rs = ps.executeQuery();
  37. while(rs.next())
  38. {
  39.  
  40. %>
  41. <tr>
  42. <td><%=rs.getInt("ID") %></td>
  43. <td><%=rs.getString("NAME") %></td>
  44. <td><%=rs.getString("DESCRIPTION") %></td>
  45. <td><%=rs.getString("PRICE") %></td>
  46. </tr>
  47. <%
  48. }
  49. %>
  50. </table>
  51. <%
  52. rs.close();
  53. con.close();
  54. }
  55. catch(Exception e)
  56. {
  57. e.printStackTrace();
  58. }
  59.  
  60. %>
  61. </form>
  62. </body>
  63. </html>
  64.  
  65. import java.io.IOException;
  66. import java.sql.Connection;
  67. import java.sql.PreparedStatement;
  68. import java.sql.ResultSet;
  69. import java.sql.SQLException;
  70. import java.io.PrintWriter;
  71. import javax.servlet.RequestDispatcher;
  72. import javax.servlet.ServletException;
  73. import javax.servlet.annotation.WebServlet;
  74. import javax.servlet.http.Cookie;
  75. import javax.servlet.http.HttpServlet;
  76. import javax.servlet.http.HttpServletRequest;
  77. import javax.servlet.http.HttpServletResponse;
  78. import databaseManagement.DBConnection;
  79.  
  80. @WebServlet("/ProductSearch")
  81. public class ProductSearch extends HttpServlet {
  82. private static final long serialVersionUID = 1L;
  83.  
  84. public ProductSearch() {
  85. super();
  86. // TODO Auto-generated constructor stub
  87. }
  88.  
  89. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  90. throws ServletException, IOException {
  91. // TODO Auto-generated method stub
  92. }
  93.  
  94. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  95. throws ServletException, IOException {
  96. // TODO Auto-generated method stub
  97.  
  98. String searchTerm = request.getParameter("Search");
  99.  
  100. try {
  101. DBConnection db = new DBConnection();
  102. Connection con = db.getConnection();
  103. PreparedStatement ps = con.prepareStatement("select * from products where name like %?%");
  104. ps.setString(1, searchTerm);
  105. ResultSet rs = ps.executeQuery();
  106. return;
  107. } catch (SQLException e) {
  108. // TODO Auto-generated catch block
  109. e.printStackTrace();
  110. }
  111.  
  112. }
  113. }
  114.  
  115. package databaseManagement;
  116. import java.sql.Connection;
  117. import java.sql.DriverManager;
  118. import java.sql.SQLException;
  119.  
  120. public class DBConnection {
  121.  
  122. public Connection getConnection() {
  123. try {
  124. Class.forName("com.mysql.jdbc.Driver");
  125. // TODO: finish
  126. //CHANGE USERNAME AND PASSWORD WHEN IMPLIMENTING ON VM
  127. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/applicationdata", "root", "safepassword");
  128. return con;
  129. } catch (ClassNotFoundException e) {
  130. e.printStackTrace();
  131. return null;
  132. } catch (SQLException e) {
  133. e.printStackTrace();
  134. return null;
  135. }
  136. }
  137. }
Add Comment
Please, Sign In to add comment