Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
5.1 kB
2
Indexable
Never
package biegi;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.xml.crypto.Data;

@WebServlet("/servlet1")
public class Servlet1 extends HttpServlet {

    private static final String CONTENT_TYPE = "text/html; charset=windows-1250";
    private String login = "sa";
    private String pass = "12345";
    private String dbBiegi = "jdbc:sqlserver://localhost\\MYSSQLSERVER:1434;databaseName=Biegi22";
    private String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private String htmlStart = "<head><meta http-equiv='Content-Type' content='text/html; charset=windows-1250'/><title>Biegi</title><meta charset='UTF-8'><link rel='stylesheet' href='resources/css/style.css'></head><body><div id='container'><div id='title'><h1>Table of Biegi</h1></div><div id='content'><form name='filters' id='filters' action='/Project1/servlet1' method='post'><div id='filterTitle'><h2>Filters</h2></div><div class='filter' id='price'><input class='price' type='number' name='minPrice' placeholder='minPrice'><input class='price' type='number' name='maxPrice' placeholder='maxPrice'></div><div class='filter' id='date'><input type='date' id='start' name='fromDate' value='1900-01-01'min=' 1900-01-01' max='2300-12-31'><input type='date' id='end' name='toDate' value='2100-12-31' min='1900-01-01' max='2300-12-31'></div><div class='filter' id='city'><input name='stringFilter' type='search' id='citySearch' placeholder='cityName'></div><div class='filter' id='distance'><input class='distance' type='number' name='minDistance' placeholder='minDistance' ><input class='distance' type='number' name='maxDistance' placeholder='maxDistance'></div><button class='filter' id='buttonSubmit' type='submit'>Submit</button></form><div id='result'>";
    private String htmlEnd = "</div></div></div></body></html>";

    private String myQuery = 
        "select B.Nazwa, M.NazwaMiasta, [Dystans[m]]], Data, Cena from dbo.Biegi as B join dbo.Miasta as M on B.MiastoID = M.MiastoID ";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
        
        String minPrice = request.getParameter("minPrice");
        String maxPrice = request.getParameter("maxPrice");
        String fromDate = request.getParameter("fromDate");
        String toDate = request.getParameter("toDate");
        String cityName = request.getParameter("stringFilter");
        String minDistance = request.getParameter("minDistance");
        String maxDistance = request.getParameter("maxDistance");

        String dateCondition = "where data > '" + fromDate + "' and data < '" + toDate +  "'";
        myQuery += dateCondition;

        if(!minPrice.isEmpty())
        {
            myQuery += " and cena > " + minPrice;
        }
        if(!maxPrice.isEmpty())
        {
            myQuery += " and cena < " + maxPrice;
        }
        if(!minDistance.isEmpty())
        {
            myQuery += " and [Dystans[m]]] > " + minDistance;
        }
        if(!maxDistance.isEmpty())
        {
            myQuery += " and [Dystans[m]]] < " + maxDistance;
        }

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        Connection con = null;
        try 
        {
            Class.forName(driverName);
            con = DriverManager.getConnection(dbBiegi, login, pass);
            Statement statement = con.createStatement();

            ResultSet rs = statement.executeQuery(myQuery);
            out.println(htmlStart);
            out.println("<table style = 'width:100%; color: #fcfcfc '>");
            out.println("<tr><th>Nazwa</th><th>Miasto</th><th>Dystans</th><th>Data</th><th>Cena</th></tr>");
            while (rs.next()) 
                {
                    out.println("<tr><td>");
                    out.println(rs.getString("Nazwa"));
                    out.println("</td><td>" );
                    out.println(rs.getString("NazwaMiasta"));
                    out.println("</td><td>" );
                    out.println(rs.getString("Dystans[m]"));
                    out.println("</td><td>" );
                    out.println(rs.getString("Data"));
                    out.println("</td><td>" );
                    out.println(rs.getString("Cena"));
                    out.println("</td></tr>");
                }
            out.println("</table>");
            out.println(htmlEnd);
            statement.close();
            rs.close();
            con.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        out.close();
   }
    
}