-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminHandler.java
More file actions
261 lines (244 loc) · 10.1 KB
/
adminHandler.java
File metadata and controls
261 lines (244 loc) · 10.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import java.net.*;
import java.sql.*;
import java.io.*;
import java.util.ArrayList;
public class adminHandler implements Runnable{
final ObjectOutputStream op;
final ObjectInputStream ip;
final Socket s;
final Connection c;
boolean flag = false;
propclientOwner p;
prop po;
static int clientID = 1;
static int ownerID = 1;
static int propID = 1;
String query;
PreparedStatement st;
Statement cst;
ResultSet rs;
ArrayList<propclientOwner> arrclientowner;
ArrayList<prop> arrprop;
ArrayList<Object> msg;
String m;
String cname;
int poid;
public adminHandler (Socket ss, ObjectInputStream oip, ObjectOutputStream oop, Connection conn){
this.s = ss;
this.op = oop;
this.ip = oip;
this.c = conn;
}
@Override
public void run() {
do {
int ch = 0;
try {
ch = (int)ip.readObject();
} catch (IOException | ClassNotFoundException e) {
break;
}
switch (ch){
case 1:
try {
p = (propclientOwner) ip.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
query = "insert into client values (?,?,?,?)";
try {
st = c.prepareStatement(query);
st.setInt(1,clientID);
st.setString(2,p.name);
st.setString(3,p.phoneNo);
st.setString(4,p.address);
st.executeUpdate();
System.out.println("New client Added...");
clientID++;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
break;
case 2:
try {
p = (propclientOwner) ip.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
query = "insert into owner values (?,?,?,?)";
try {
st = c.prepareStatement(query);
st.setInt(1,ownerID);
st.setString(2,p.name);
st.setString(3,p.phoneNo);
st.setString(4,p.address);
st.executeUpdate();
System.out.println("New Owner Added...");
ownerID++;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
break;
case 3:
po = new prop();
try {
po = (prop) ip.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
query = "insert into prop values (?,?,?,?,?,?,?,?,?)";
try {
st = c.prepareStatement(query);
st.setInt(1,propID);
st.setInt(2,po.ownerId);
st.setString(3,po.type);
st.setLong(4,po.sqrfeet);
st.setLong(5,po.price);
st.setInt(6,po.age);
st.setString(7,po.address);
st.setString(8,po.area);
st.setString(9,po.status);
st.executeUpdate();
System.out.println("New Property Added...");
propID++;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
break;
case 4:
try {
po = (prop) ip.readObject();
query = "update prop set ownerid=?,proptype=?,propsqrfeet=?,propprice=?,propage=?,propadd=?,proparea=?,propstatus=? where propid=?";
st = c.prepareStatement(query);
st.setInt(1,po.ownerId);
st.setString(2,po.type);
st.setLong(3,po.sqrfeet);
st.setLong(4,po.price);
st.setInt(5,po.age);
st.setString(6,po.address);
st.setString(7,po.area);
st.setString(8,po.status);
st.setInt(9,po.id);
st.executeUpdate();
} catch (IOException | ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
System.out.println("Property "+ poid + " is updated");
break;
case 5:
try {
String n = (String) ip.readObject();
query = "select msg,status from messages where cli='"+n+"'";
cst = c.createStatement();
rs = cst.executeQuery(query);
msg = new ArrayList<>();
while (rs.next()){
msg.add(rs.getInt(2));
msg.add(rs.getString(1));
}
op.reset();
op.writeObject(msg);
} catch (SQLException | IOException | ClassNotFoundException throwables) {
throwables.printStackTrace();
}
break;
case 6:
query = "select * from client";
try {
cst = c.createStatement();
rs = cst.executeQuery(query);
arrclientowner = new ArrayList<>();
while (rs.next()) {
p = new propclientOwner();
p.id = rs.getInt(1);
p.name = rs.getString(2);
p.phoneNo = rs.getString(3);
p.address = rs.getString(4);
arrclientowner.add(p);
}
op.reset();
op.writeObject(arrclientowner);
} catch (SQLException | IOException throwables) {
throwables.printStackTrace();
}
break;
case 7:
query = "select * from owner";
try {
cst = c.createStatement();
rs = cst.executeQuery(query);
arrclientowner = new ArrayList<>();
while (rs.next()) {
p = new propclientOwner();
p.id = rs.getInt(1);
p.name = rs.getString(2);
p.phoneNo = rs.getString(3);
p.address = rs.getString(4);
arrclientowner.add(p);
}
op.reset();
op.writeObject(arrclientowner);
} catch (SQLException | IOException throwables) {
throwables.printStackTrace();
}
break;
case 8:
query = "select * from prop";
try {
cst = c.createStatement();
rs = cst.executeQuery(query);
arrprop = new ArrayList<>();
while (rs.next()) {
po = new prop();
po.id = rs.getInt(1);
po.ownerId =rs.getInt(2);
po.type = rs.getString(3);
po.sqrfeet = rs.getLong(4);
po.price = rs.getLong(5);
po.age = rs.getInt(6);
po.address = rs.getString(7);
po.area = rs.getString(8);
po.status = rs.getString(9);
arrprop.add(po);
}
op.reset();
op.writeObject(arrprop);
} catch (SQLException | IOException throwables) {
throwables.printStackTrace();
}
break;
case 9:
try {
m = (String) ip.readObject();
cname = (String) ip.readObject();
System.out.println(m + " "+ cname);
query = "insert into messages values (?,?,?)";
st = c.prepareStatement(query);
st.setString(1, cname);
st.setString(2, m);
st.setInt(3, 0);
st.executeUpdate();
System.out.println("Message sent from admin to "+cname);
} catch (IOException | ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
break;
case 10:
try {
s.close();
ip.close();
op.close();
c.close();
System.out.println("Closing connection");
} catch (IOException | SQLException e) {
break;
}
flag = true;
break;
default:
break;
}
}while (!flag);
System.out.println("Code End");
}
}