Skip to content

Commit

Permalink
Handling MySQL Errors
Browse files Browse the repository at this point in the history
Max User Connections problem should be really fixed at this point. You
also cannot delete a location unless no devices/employees are there.
There is now a "something went wrong!" page in case someone works around
that.
  • Loading branch information
clj13001 committed Apr 12, 2017
1 parent cb9e346 commit f6a8c2e
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 131 deletions.
17 changes: 12 additions & 5 deletions WebContent/adminLogin.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ String pass;
Cookie adminCookie;
ssoNum = Integer.parseInt(request.getParameter("ssoNum2"));
pass = request.getParameter("pass");
MySQLAccess myaccess = new MySQLAccess();
Statement statement = myaccess.getStatement();
String database = "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317";
String user = "b372dfe7409692";
String password = "74f6e317";
System.getenv("VCAP_SERVICES");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(database, user, password);
Statement stmt = connection.createStatement();
ResultSet result;
result = statement.executeQuery("select * FROM admin where Admin_ID='" + ssoNum + "' AND Password='" + pass + "'");
result = stmt.executeQuery("select * FROM admin where Admin_ID='" + ssoNum + "' AND Password='" + pass + "'");
if (result.next()){
request.getSession();
session.setAttribute("ssoNum", ssoNum);
adminCookie = new Cookie("ssoNum", Integer.toString(ssoNum));
adminCookie.setMaxAge(30*60);
response.addCookie(adminCookie);
statement.close();
stmt.close();
connection.close();
response.sendRedirect("html/webpages/admin.jsp");
} else {
statement.close();
stmt.close();
connection.close();
response.sendRedirect("index.jsp");
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions WebContent/html/webpages/adminLocation.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@
<label for="modifyZip">Zip Code *</label>
<input style = "width: 100px;" name = "zip" type = "number" class="form-control" id="modifyZip" required = "true">
</div>
<button type = "submit" name = "modify" value = "Save Changes" class="btn btn-primary">Save Changes</button>
<button style = "display: inline-block;" type = "submit" name = "delete" value = "Delete Entry" class="btn btn-primary">Delete</button>
<button id = "themodifybutton" type = "submit" name = "modify" value = "Save Changes" class="btn btn-primary">Save Changes</button>
<button id = "thedeletebutton" type = "submit" name = "delete" value = "Delete Entry" class="btn btn-primary" style = "display: inline-block;" title = "hello">Delete</button>
<input type = "text" id = "modifyID" name = "id" style = "display: none;">
</form>
</div>
Expand Down Expand Up @@ -323,6 +323,15 @@ function modifyModal(){
$("#modifyState").val(locations[i].state);
$("#modifyZip").val(locations[i].zip);
$("#modifyID").val(locations[i].id);
if(locations[i].numDevices > 0 || locations[i].numEmployees > 0){
$('#thedeletebutton').attr('disabled',true);
$('#thedeletebutton').attr('title','Cannot delete because devices and/or employees are at this location!');
}
else{
$('#thedeletebutton').attr('disabled',false);
$('#thedeletebutton').removeAttr('title');
}
$("#modifyModal").show();
}
Expand Down
1 change: 0 additions & 1 deletion WebContent/html/webpages/deviceRedirect.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

<%
//make instance
MySQLAccess myaccess = new MySQLAccess();
String name = request.getParameter("name").replace("\"","\\\"");
String description = request.getParameter("description").replace("\"","\\\"");
String status = request.getParameter("status");
Expand Down
6 changes: 5 additions & 1 deletion WebContent/html/webpages/locationRedirect.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ String address = request.getParameter("address").replace("\"","\\\"");
String town = request.getParameter("town").replace("\"","\\\"");
String state = request.getParameter("state");
String zip = request.getParameter("zip");
int er = 0;
//add form was submitted
if(request.getParameter("add") != null){
Location location = new Location(name,address,town,state,zip,0);
Expand All @@ -52,11 +53,14 @@ if(request.getParameter("modify") != null){
if(request.getParameter("delete") != null){
String strID = request.getParameter("id");
int id = Integer.parseInt(strID);
LocationQueries.deleteLocation(id);
er = LocationQueries.deleteLocation(id);
}
%>
<script>
if(<%=er%> == 0)
window.location.replace("adminLocation.jsp");
else
window.location.replace("somethingwrong.jsp");
</script>
<!-- Navbar generation. -->
</body>
Expand Down
1 change: 0 additions & 1 deletion WebContent/html/webpages/profileSettingsRedirect.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

<%
//make instance
MySQLAccess myaccess = new MySQLAccess();
String name = request.getParameter("name").replace("\"","\\\"");
int id = Integer.parseInt(request.getParameter("sso"));
String phone = request.getParameter("phone");
Expand Down
40 changes: 40 additions & 0 deletions WebContent/html/webpages/somethingwrong.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">

<title>Synchrony Financial</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel = "stylesheet" type = "text/css" href = "../css/stylesheet.css">
<link rel = "shortcut icon" href = "../imgs/synchrony-financial-logo-dlpx_1.ico">
<style>
div.error{
text-align: center;
position: absolute;
top: 50%;
left: 50%;
margin-top: -126px;
margin-left: -138px;
}
</style>
</head>

<body>
<nav class="navbar navbar-inverse navbar-fixed-top" id = "navbaruniversal">
<%@ include file="navbar.jsp"%>
</nav>
<div class = "error">
<h2>Something Went Wrong!</h2>
<img src="../imgs/my-icons-collection-128px/png/wrong.png"/>
<h2>Try Again Later.</h2>
</div>
</body>
</html>
14 changes: 7 additions & 7 deletions WebContent/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
div.menuOption{
display: inline-block;
width: 250px;
height: 150px;
height: 210px;
vertical-align: top;
border: solid;
padding: 10px;
Expand All @@ -95,16 +95,16 @@
<div class = "menuOption">
<h2>User Login</h2>
<form action="userLogin.jsp" method="post">
<input type="text" name="ssoNum" placeholder="SSO Number" /><br>
<input type ="submit" value="Login" />
<input class = "form-control" type="text" name="ssoNum" placeholder="SSO Number" /><br>
<button type = "submit" value = "Add" class="btn btn-primary">Login</button>
</form>
</div>
<div class = "menuOption">
<h2>Admin Login</h2>
<form action="adminLogin.jsp" method="post">
<input type="text" name="ssoNum2" placeholder="SSO Number" />
<input type="password" name="pass" placeholder="Password" />
<input type ="submit" value="Login" />
<input class = "form-control" type="text" name="ssoNum2" placeholder="SSO Number" />
<input class = "form-control" type="password" name="pass" placeholder="Password" /><br>
<button type = "submit" value = "Add" class="btn btn-primary">Login</button>
</form>
</div>
</div>
Expand All @@ -124,7 +124,7 @@ if(usercookies != null){
if(c.getName().equals("ssoNum")){//when (and if) we get to user cookie we want to reset it
navsso = c.getValue();
c.setMaxAge(0);//delete current
newCookie = new Cookie("ssoNum",navsso);//make new one
newCookie = new Cookie("ssoNum",navsso); //make new one
newCookie.setMaxAge(30*60);
response.addCookie(newCookie);
break;
Expand Down
17 changes: 12 additions & 5 deletions WebContent/userLogin.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@
int ssoNum;
Cookie userCookie;
ssoNum = Integer.parseInt(request.getParameter("ssoNum"));
MySQLAccess myaccess = new MySQLAccess();
Statement statement = myaccess.getStatement();
System.getenv("VCAP_SERVICES");
Class.forName("com.mysql.jdbc.Driver");
String database = "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_15a989204c2ff8a?user=b372dfe7409692&password=74f6e317";
String user = "b372dfe7409692";
String password = "74f6e317";
Connection connection = DriverManager.getConnection(database, user, password);
Statement stmt = connection.createStatement();
ResultSet result;
result = statement.executeQuery("select * FROM employee where Employee_ID='" + ssoNum + "'");
result = stmt.executeQuery("select * FROM employee where Employee_ID='" + ssoNum + "'");
if (result.next()){
request.getSession();
session.setAttribute("ssoNum", ssoNum);
userCookie = new Cookie("ssoNum", Integer.toString(ssoNum));
userCookie.setMaxAge(30*60);
response.addCookie(userCookie);
statement.close();
stmt.close();
connection.close();
response.sendRedirect("html/webpages/index.jsp");
} else {
statement.close();
stmt.close();
connection.close();
response.sendRedirect("index.jsp");
}
Expand Down
43 changes: 27 additions & 16 deletions src/database/EmployeeQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ public class EmployeeQueries {

public static void initializeEmployee(int id) throws ClassNotFoundException, SQLException
{
MySQLAccess access = new MySQLAccess();
Statement statement = access.getStatement();
statement.executeUpdate("INSERT INTO employee (Employee_ID) VALUES ("+id+");");
access.closeConnection();
System.getenv("VCAP_SERVICES");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(database, user, password);
Statement stmt = connection.createStatement();
stmt.executeUpdate("INSERT INTO employee (Employee_ID) VALUES ("+id+");");
stmt.close();
connection.close();
}

public static void updateEmployee(int id, String name, String phone, int location, String email) throws ClassNotFoundException, SQLException
{
System.out.println("Updating employee "+id+": "+name+".\nPhone: "+phone+", location: "+location);
MySQLAccess access = new MySQLAccess();
Statement statement = access.getStatement();
System.getenv("VCAP_SERVICES");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(database, user, password);
Statement stmt = connection.createStatement();
String query = "";
query+="UPDATE employee SET ";
query+="Name='"+name;
Expand All @@ -31,8 +36,9 @@ public static void updateEmployee(int id, String name, String phone, int locatio
query+=", Email='"+email;
query+="' WHERE Employee_ID="+id;
System.out.println(query);
statement.executeUpdate(query);
access.closeConnection();
stmt.executeUpdate(query);
stmt.close();
connection.close();
}

public static void updateEmployee(int id, String name, int icon, String phone, String email) throws ClassNotFoundException, SQLException
Expand All @@ -57,9 +63,11 @@ public static void updateEmployee(int id, String name, int icon, String phone, S

public static User getEmployeeByID(int id) throws ClassNotFoundException, SQLException
{
MySQLAccess access = new MySQLAccess();
Statement statement = access.getStatement();
ResultSet resultSet = statement.executeQuery("Select * FROM employee WHERE Employee_ID = "+id);
System.getenv("VCAP_SERVICES");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(database, user, password);
Statement stmt = connection.createStatement();
ResultSet resultSet = stmt.executeQuery("Select * FROM employee WHERE Employee_ID = "+id);
resultSet.next();
User employee = new User(
id,
Expand All @@ -69,18 +77,20 @@ public static User getEmployeeByID(int id) throws ClassNotFoundException, SQLExc
resultSet.getString("Email"),
resultSet.getInt("Img_Index")
);
resultSet = statement.executeQuery("SELECT Address FROM location WHERE Location_ID = "+employee.getLocation());
resultSet = stmt.executeQuery("SELECT Address FROM location WHERE Location_ID = "+employee.getLocation());
resultSet.next();
employee.setLocationName(resultSet.getString("Address"));
access.closeConnection();
stmt.close();
connection.close();
return employee;
}

public static User[] getAllUsers() throws SQLException, ClassNotFoundException{
//database connect
System.getenv("VCAP_SERVICES");
MySQLAccess access = new MySQLAccess();
Statement stmt = access.getStatement();
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(database, user, password);
Statement stmt = connection.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * FROM employee");
int counter = 0;

Expand All @@ -97,7 +107,8 @@ public static User[] getAllUsers() throws SQLException, ClassNotFoundException{
counter++;
}

access.closeConnection();
stmt.close();
connection.close();
return users;
}
}
Loading

0 comments on commit f6a8c2e

Please sign in to comment.