-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdmin.java
106 lines (79 loc) · 4 KB
/
Admin.java
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
package application;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
public class Admin {
public boolean login(String Email, String Password) throws SQLException {//Function to check the Email and the password of the Admin to allow login
boolean key=false;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_project","root","root");
ResultSet rs = null;
String sql = "select * from admin";
PreparedStatement p = conn.prepareStatement(sql);
rs = p.executeQuery();
while (rs.next()) {
String email = rs.getString("email");
String password = rs.getString("password");
if(email.equals(Email)&password.equals(Password)){
key=true;}}}
catch (ClassNotFoundException e){e.printStackTrace();}
return key;}
public String add_plane(int ID,String Name, String From,String To,String Date,String Time ,float Prices,int Seats,String Gate) throws SQLException{// Function that allows the Admin to add a new flight details in the flights table
try {
Name="'"+Name+"'";
Date="'"+Date+"'";
From="'"+From+"'";
To="'"+To+"'";
Time="'"+Time+"'";
Gate="'"+Gate+"'";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_project","root","root");
Statement stmt = (Statement) conn.createStatement();
String query1 = "INSERT INTO plane " + "VALUES ("+ID+", "+Name+", "+From+", "+To+", "+Date+", "+Time+", "+Prices+", "+Seats+", "+Gate+")";
stmt.executeUpdate(query1);}
catch (ClassNotFoundException e){e.printStackTrace();}
catch (SQLIntegrityConstraintViolationException e){ return "The ID is already taken"; }return"";}
public String delete_plane(int ID) throws SQLException {//Function that allows the Admin to delete any Flight and delete all the tickets associated with it
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_project","root","root");
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = null;
String sql = "select * from plane";
PreparedStatement p = conn.prepareStatement(sql);
rs = p.executeQuery();
while (rs.next()) {
int id2 = rs.getInt("id");
if (id2 == ID) {
String query1 = "delete from plane " +
"where id="+ID;
stmt.executeUpdate(query1);
return "";}
else {continue;}}}
catch (ClassNotFoundException e) {
e.printStackTrace();}return "Cannot Remove a NoneExisting Flight";}
public String delete_user(String phone_number) throws SQLException {//Function that allows the Admin to delete any User and its details from the database
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_project","root","root");
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = null;
String sql = "select * from customer";
PreparedStatement p = conn.prepareStatement(sql);
rs = p.executeQuery();
while (rs.next()) {
String pho = rs.getString("phone_number");
if (pho.equals(phone_number)) {
String query1 = "delete from customer " +
"where phone_number= "+phone_number;
stmt.executeUpdate(query1);return "";}
else {
continue;}}}
catch (ClassNotFoundException e) {
e.printStackTrace();} return "Cannot Remove a Nonexisting Customer.";}
}