-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewRideOffer.jsp
158 lines (146 loc) · 5.83 KB
/
newRideOffer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*,java.util.*,java.sql.*,java.time.*,java.time.format.*"%>
<%@ 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 Offer</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 newMaxPassengers = request.getParameter("maxpassengers");
String LicensePlate = request.getParameter("licenseplate");
String checkRecurring = request.getParameter("recurring");
//Make an insert statement for the Ride Offers table:
String insert = "INSERT INTO rideoffers(Username, Date, Time, MaxPassengers, LicensePlate, 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, newMaxPassengers);
String qry = "SELECT MaxPassengers FROM carlist WHERE LicensePlate = \"" + LicensePlate + "\"";
ResultSet maxpasscheck = stmt.executeQuery(qry);
if(maxpasscheck.next()){
if(Integer.parseInt(newMaxPassengers) > maxpasscheck.getInt("MaxPassengers")){
request.setAttribute("maxpassengers", "Passengers exceeds give car capacity!");
RequestDispatcher ed = request.getRequestDispatcher("createRideOffer.jsp");
ed.forward(request, response);
}
}
ps.setString(5, LicensePlate);
if (checkRecurring == null)
{
checkRecurring = "false";
}
ps.setString(6, checkRecurring);
ps.setString(7, newStart);
ps.setString(8, 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("createRideOffer.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("createRideOffer.jsp");
ed.forward(request, response);
}
else
{
String date = newDate.substring(0, 3) + newDate.substring(5,6) + newDate.substring(8,9);
String startTime;
String test = "SELECT * FROM rideoffers r WHERE r.Username = \"" + (String)session.getAttribute("currentuser")
+ "\" AND r.Date = \"" + date + "\"AND r.Time = \"" + newTime +
"\"AND r.Origin = \"" + newStart + "\" AND r.Destination = \"" + newDestination +"\"";
ResultSet result = stmt.executeQuery(test);
if (result.next())
{
request.setAttribute("duplicate","Duplicate Offer");
error = true;
result.close();
}
if(LicensePlate == null || LicensePlate.equals("")){
request.setAttribute("insertStatus","Error: no license plate selected!");
error = true;
}
if (error)
{
RequestDispatcher ed = request.getRequestDispatcher("createRideOffer.jsp");
ed.forward(request, response);
}
else
{
ps.executeUpdate();
out.print("Insert successful! <br><br>");
out.print("From: " + newStart + "<br>" +
"To: " + newDestination + "<br>" +
"Date: " + newDate + "<br>" +
"Time: " + newTime + "<br>" +
"Max Passengers: " + newMaxPassengers + "<br>" +
"License Plate: " + LicensePlate + "<br>");
}
}
}
stmt.close();
con.close();
}
catch (Exception ex) {
ex.printStackTrace();
out.print("Insert failed!");
}
%>
<br>
<br>
[<a href="createRideOffer.jsp">Ride offers</a>] [<a href="homepage.jsp">Main page</a>] [<a href="https://github.com/NitantP/Ride-Share/blob/master/newRideOffer.jsp">GitHub Page</a>]
</body>
</html>