Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1" import="java.sql.Connection,java.sql.ResultSet,java.sql.DriverManager,project1.components.DBConnector, project1.components.Filter"%>
- <!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>Product</title>
- <style type="text/css">
- <%@include file="bootstrap/css/bootstrap.css" %>
- <%@include file="bootstrap/css/bootstrap-theme.css" %>
- </style>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <script src="bootstrap/js/jquery.js"></script>
- <script src="bootstrap/js/bootstrap.js"></script>
- </head>
- <body>
- <%
- DBConnector dbc = new DBConnector();
- Connection conn = dbc.connect();
- String query = "select * from product";
- %>
- </body>
- <form method="post">
- <div class="col-xs-2 col-xs-offset-5">
- <input type="text" class="form-control" name="value" value=<%=request.getParameter("firstinput")%>>
- <br><br>
- <input type="submit" class="form-control" name="submit" value="Filter">
- <%
- if(!request.getParameter("value").isEmpty()){
- String val = request.getParameter("value");
- Filter ft = new Filter();
- query = ft.filterByValue(val);
- }
- %>
- </div>
- <br></br>
- <table class = 'table table-hover'>
- <tr>
- <td>Id</td>
- <td>Name</td>
- <td>Product</td>
- </tr>
- <%
- try
- {
- ResultSet rs = dbc.obtainTable(query);
- while(rs.next())
- {
- %><tr>
- <td><%=rs.getInt("ID") %></td>
- <td><%=rs.getString("NAME") %></td>
- <td><%=rs.getFloat("PRICE") %></td>
- </tr> <%
- }
- %>
- </table>
- <%
- rs.close();
- conn.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- %>
- </form>
- </html>
- package project1.components;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.Statement;
- public class DBConnector {
- String url = "jdbc:mysql://localhost:3306/products?autoReconnect=true&useSSL=false";
- String user = "root";
- String pass = "root";
- public Connection connect() throws ClassNotFoundException, SQLException{
- Class.forName("com.mysql.jdbc.Driver");
- return (Connection) DriverManager.getConnection(url,user,pass);
- }
- public ResultSet obtainTable(String query) throws ClassNotFoundException, SQLException{
- Connection cn = this.connect();
- Statement stmt= (Statement) cn.createStatement();
- return stmt.executeQuery(query);
- }
- }
- package project1.components;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class Filter {
- public String filterByValue(String val) throws ClassNotFoundException, SQLException{
- DBConnector dbc = new DBConnector();
- int num = Integer.parseInt(val);
- String query = null;
- if(num >= 0){
- query = "select * from product where price >" + val;
- }
- else{
- query = "select * from product";
- }
- return query;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement