Advertisement
Guest User

Untitled

a guest
Jun 8th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1" import="java.sql.Connection,java.sql.ResultSet,java.sql.DriverManager,project1.components.DBConnector, project1.components.Filter"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  7. <title>Product</title>
  8. <style type="text/css">
  9. <%@include file="bootstrap/css/bootstrap.css" %>
  10. <%@include file="bootstrap/css/bootstrap-theme.css" %>
  11. </style>
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <script src="bootstrap/js/jquery.js"></script>
  14. <script src="bootstrap/js/bootstrap.js"></script>
  15. </head>
  16. <body>
  17. <%
  18. DBConnector dbc = new DBConnector();
  19. Connection conn = dbc.connect();
  20. String query = "select * from product";
  21. %>
  22. </body>
  23.  
  24. <form method="post">
  25.  
  26. <div class="col-xs-2 col-xs-offset-5">
  27. <input type="text" class="form-control" name="value" value=<%=request.getParameter("firstinput")%>>
  28. <br><br>
  29. <input type="submit" class="form-control" name="submit" value="Filter">
  30. <%
  31. if(!request.getParameter("value").isEmpty()){
  32. String val = request.getParameter("value");
  33. Filter ft = new Filter();
  34. query = ft.filterByValue(val);
  35. }
  36. %>
  37. </div>
  38. <br></br>
  39. <table class = 'table table-hover'>
  40. <tr>
  41. <td>Id</td>
  42. <td>Name</td>
  43. <td>Product</td>
  44. </tr>
  45. <%
  46. try
  47. {
  48. ResultSet rs = dbc.obtainTable(query);
  49.  
  50. while(rs.next())
  51. {
  52.  
  53. %><tr>
  54. <td><%=rs.getInt("ID") %></td>
  55. <td><%=rs.getString("NAME") %></td>
  56. <td><%=rs.getFloat("PRICE") %></td>
  57. </tr> <%
  58.  
  59. }
  60. %>
  61. </table>
  62. <%
  63. rs.close();
  64. conn.close();
  65. }
  66. catch(Exception e)
  67. {
  68. e.printStackTrace();
  69. }
  70.  
  71.  
  72.  
  73.  
  74. %>
  75.  
  76. </form>
  77.  
  78. </html>
  79.  
  80. package project1.components;
  81.  
  82. import java.sql.DriverManager;
  83. import java.sql.ResultSet;
  84. import java.sql.SQLException;
  85.  
  86. import com.mysql.jdbc.Connection;
  87. import com.mysql.jdbc.Statement;
  88.  
  89. public class DBConnector {
  90.  
  91. String url = "jdbc:mysql://localhost:3306/products?autoReconnect=true&useSSL=false";
  92. String user = "root";
  93. String pass = "root";
  94.  
  95. public Connection connect() throws ClassNotFoundException, SQLException{
  96. Class.forName("com.mysql.jdbc.Driver");
  97. return (Connection) DriverManager.getConnection(url,user,pass);
  98. }
  99.  
  100. public ResultSet obtainTable(String query) throws ClassNotFoundException, SQLException{
  101. Connection cn = this.connect();
  102. Statement stmt= (Statement) cn.createStatement();
  103. return stmt.executeQuery(query);
  104. }
  105. }
  106.  
  107. package project1.components;
  108.  
  109. import java.sql.ResultSet;
  110. import java.sql.SQLException;
  111.  
  112. public class Filter {
  113.  
  114. public String filterByValue(String val) throws ClassNotFoundException, SQLException{
  115. DBConnector dbc = new DBConnector();
  116. int num = Integer.parseInt(val);
  117. String query = null;
  118. if(num >= 0){
  119. query = "select * from product where price >" + val;
  120. }
  121. else{
  122. query = "select * from product";
  123. }
  124. return query;
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement