-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewRideRequest.jsp
135 lines (126 loc) · 4.96 KB
/
newRideRequest.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<!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>Ride Share - Create Ride Request</title>
</head>
<body>
<%
try {
//Create a connection string
String url = "jdbc:mysql://cs336finalproject.cl75kudzatsx.us-east-1.rds.amazonaws.com:3306/users";
//Load JDBC driver - the interface standardizing the connection procedure. Look at WEB-INF\lib for a mysql connector jar file, otherwise it fails.
Class.forName("com.mysql.jdbc.Driver");
//Create a connection to your DB
Connection con = DriverManager.getConnection(url, "cs336project", "csteam14");
//Create a SQL statement
Statement stmt = con.createStatement();
//Get parameters from the HTML form at the createRideOffer.jsp
String newStart = request.getParameter("startinglocation");
String newDestination = request.getParameter("destination");
String newDate = request.getParameter("date");
String newTime = request.getParameter("time");
String newNumPassengers = request.getParameter("numpassengers");
String checkRecurring = request.getParameter("recurring");
//Make an insert statement for the Ride Offers table:
String insert = "INSERT INTO riderequests(Username, Date, Time, NumPassengers, Recurring, Origin, Destination)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement ps = con.prepareStatement(insert);
//Add parameters of the query. Start with 1, the 0-parameter is the INSERT statement itself
ps.setString(1, (String)session.getAttribute("currentuser"));
ps.setString(2, newDate);
ps.setString(3, newTime);
ps.setString(4, newNumPassengers);
if(checkRecurring == null){
checkRecurring = "false";
}
ps.setString(5, checkRecurring);
ps.setString(6, newStart);
ps.setString(7, newDestination);
//INSERT INPUT CHECKS HERE
boolean error = false;
boolean isDigit = true;
boolean isDigit2 = true;
if (newDate.length() != 10 || newTime.length() != 5)
{
request.setAttribute("time", "Please use the format HH:MM");
request.setAttribute("date", "Please use the format YYYY-MM-DD");
RequestDispatcher ed = request.getRequestDispatcher("createRideRequest.jsp");
ed.forward(request, response);
}
else
{
for (int i = 0; i < 10; i++)
{
if (i != 4 && i != 7)
{
if (!Character.isDigit(newDate.charAt(i)))
{
isDigit2 = false;
}
}
}
for (int i = 0; i < 5; i++)
{
if (i != 2)
{
if (!Character.isDigit(newTime.charAt(i)))
{
isDigit = false;
}
}
}
if(!isDigit || !isDigit2 || Integer.parseInt(newTime.substring(0, 2)) > 24 || Integer.parseInt(newTime.substring(3, newTime.length())) > 59 || newTime.charAt(2) != ':'
|| Integer.parseInt(newDate.substring(0, 4)) != 2017 || Integer.parseInt(newDate.substring(5, 7)) > 12
|| (Integer.parseInt(newDate.substring(5, 7)) < 4 && Integer.parseInt(newDate.substring(8, 10)) < 26) || Integer.parseInt(newDate.substring(8, 10)) > 31)
{
request.setAttribute("time", "Please use the format HH:MM");
request.setAttribute("date", "Please use the format YYYY-MM-DD");
RequestDispatcher ed = request.getRequestDispatcher("createRideRequest.jsp");
ed.forward(request, response);
}
else
{
String update = "SELECT * FROM riderequests r WHERE r.Username = \"" + (String)session.getAttribute("currentuser")
+ "\" AND r.Date = \"" + newDate + "\"AND r.Time = \"" + newTime +
"\"AND r.Origin = \"" + newStart + "\" AND r.Destination = \"" + newDestination +"\"";
ResultSet result = stmt.executeQuery(update);
if (result.next())
{
error = true;
}
if (error)
{
request.setAttribute("duplicate", "You entered a duplicate request");
RequestDispatcher ed = request.getRequestDispatcher("createRideRequest.jsp");
ed.forward(request, response);
}
else
{
//Run the query against the DB
ps.executeUpdate();
out.print("Insert successful! <br><br>");
out.print("From: " + newStart + "<br>" +
"To: " + newDestination + "<br>" +
"Date: " + newDate + "<br>" +
"Time: " + newTime + "<br>" +
"Passengers: " + newNumPassengers + "<br>");
}
con.close();
}
}
}
catch (Exception ex) {
ex.printStackTrace();
out.print("Insert failed!");
}
%>
<br>
<br>
[<a href="createRideRequest.jsp">Ride requests</a>] [<a href="homepage.jsp">Main page</a>] [<a href="https://github.com/NitantP/Ride-Share/blob/master/newRideRequest.jsp">GitHub Page</a>] [<a href="index.jsp">Logout</a>]
</body>
</html>