import DBcon.DataCon;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class EventMaster extends HttpServlet
{

    public EventMaster()
    {
    }

    public void service(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws ServletException, IOException
    {
        String s = "";
        Connection connection = null;
        try
        {
            connection = (new DataCon()).getConnection(getServletContext());
            int i = 0;
            String s1 = httpservletrequest.getParameter("TYPE");
            if(s1.equalsIgnoreCase("INSERT"))
            {
                Statement statement = connection.createStatement();
                ResultSet resultset = statement.executeQuery("select max(evtid) from event_master");
                if(resultset.next())
                    i = resultset.getInt(1);
                PreparedStatement preparedstatement2 = connection.prepareStatement("INSERT INTO Event_master values(?,?,?,?,?,?,?,?)");
                preparedstatement2.setInt(1, i + 1);
                preparedstatement2.setString(2, httpservletrequest.getParameter("TEvtName"));
                preparedstatement2.setString(3, httpservletrequest.getParameter("TVenue"));
                preparedstatement2.setString(4, httpservletrequest.getParameter("TDate"));
                preparedstatement2.setString(5, httpservletrequest.getParameter("TAddress"));
                preparedstatement2.setString(6, httpservletrequest.getParameter("TLocation"));
                preparedstatement2.setString(7, httpservletrequest.getParameter("TCity"));
                preparedstatement2.setLong(8, Long.parseLong(httpservletrequest.getParameter("TPhno")));
                preparedstatement2.execute();
                connection.commit();
                System.out.println("Record Inserted");
                s = "/inssuc.jsp";
            } else
            if(s1.equalsIgnoreCase("DELETE"))
            {
                PreparedStatement preparedstatement = connection.prepareStatement("DELETE From Event_master where evtid=?");
                preparedstatement.setInt(1, Integer.parseInt(httpservletrequest.getParameter("TEvtId")));
                preparedstatement.execute();
                connection.commit();
                System.out.println("Record Deleted");
                s = "/delsuc.jsp";
            } else
            if(s1.equalsIgnoreCase("MODIFY"))
            {
                PreparedStatement preparedstatement1 = connection.prepareStatement("Update Event_master set EVTNAME=?,VENUE=?,CURDATE=?,ADDRESS=?,LOCATION=?,CITY=?,PHNO=? where EVTID=?");
                preparedstatement1.setString(1, httpservletrequest.getParameter("TEvtName"));
                preparedstatement1.setString(2, httpservletrequest.getParameter("TVenue"));
                preparedstatement1.setString(3, httpservletrequest.getParameter("TDate"));
                preparedstatement1.setString(4, httpservletrequest.getParameter("TAddress"));
                preparedstatement1.setString(5, httpservletrequest.getParameter("TLocation"));
                preparedstatement1.setString(6, httpservletrequest.getParameter("TCity"));
                System.out.println((new StringBuilder()).append(">>>>>>>>>>>>").append(httpservletrequest.getParameter("TPhno").trim()).append("*********>>>>>>>>>>>").toString());
                preparedstatement1.setString(7, httpservletrequest.getParameter("TPhno").trim());
                System.out.println((new StringBuilder()).append(">>>>>>>>>>>>after").append(httpservletrequest.getParameter("TEvtId")).append("*********>>>>>>>>>>>").toString());
                preparedstatement1.setInt(8, Integer.parseInt(httpservletrequest.getParameter("TEvtId")));
                preparedstatement1.execute();
                connection.commit();
                System.out.println("Record Updated");
                s = "/Updsuc.jsp";
            }
        }
        catch(Exception exception)
        {
            System.out.println((new StringBuilder()).append(exception.toString()).append(">>>>>>>>>>>").append(exception.getMessage()).toString());
            exception.printStackTrace();
            if(connection == null)
                s = "/Netfail.jsp";
            else
                s = "/opefail.jsp";
        }
        RequestDispatcher requestdispatcher = httpservletrequest.getRequestDispatcher(s);
        requestdispatcher.forward(httpservletrequest, httpservletresponse);
    }
}