-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
executable file
·241 lines (229 loc) · 7.85 KB
/
Server.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
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
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.swing.*;
public class Server extends Thread{
static ServerSocket ss;
static Socket s;
static DataOutputStream dout;
static DataInputStream dis;
static Connection con;
Object obj1=new Object();
Object obj2=new Object();
Object obj3=new Object();
Object obj4=new Object();
Object obj5=new Object();
Object obj6=new Object();
Object obj7=new Object();
public void run()
{
try
{
DataInputStream dis=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
System.out.println("Streams created");
String name=dis.readUTF();
PreparedStatement stmt=null;
if(name.equals("Registration")==true)
{
synchronized(obj1)
{
String event=dis.readUTF();
String abc=dis.readUTF();
System.out.println("Data Inputted from the client");
stmt=con.prepareStatement("select * from "+abc+" where Events='"+event+"';");
ResultSet rs=stmt.executeQuery();
if(rs.next())
{
dout.writeUTF("Already Registered");
}
else
{
stmt=con.prepareStatement("insert into "+abc+" values(?);");
stmt.setString(1,event);
stmt.execute();
dout.writeUTF("You have been successfully registered");
dout.flush();
}
}
}
else if(name.equals("Signup")==true)
{
synchronized(obj2)
{
String a=dis.readUTF();
String b=dis.readUTF();
System.out.println(a+" "+b);
stmt=con.prepareStatement("select * from login where user=?");
stmt.setString(1,a);
ResultSet res=stmt.executeQuery();
if(res.next())
{
dout.writeUTF("Already used username!!!");
}
else
{
stmt=con.prepareStatement("insert into login values(?,?)");
stmt.setString(1,a);
stmt.setString(2,b);
stmt.execute();
Statement st=con.createStatement();
st.executeUpdate("create table "+a+" ( Events varchar(20) );");
dout.writeUTF("Successfully Signed up");
dout.flush();
st.close();
}
}
}
else if(name.equals("Developer")==true)
{
synchronized(obj3)
{
Statement st=con.createStatement();
ResultSet res=st.executeQuery("select * from develop;");
System.out.println("Data Inputted from the database");
String a,b,c;
int l=0;
while(res.next())
{
l++;
}
dout.writeUTF(""+l);
res.absolute(0);
while(res.next())
{
a=res.getString("Event");
b=res.getString("Venue");
c=res.getString("Timing");
dout.writeUTF(a);
dout.writeUTF(b);
dout.writeUTF(c);
}
st.close();
res.close();
}
}
else if(name.equals("Update"))
{
synchronized(obj4)
{
String a=dis.readUTF();
String b=dis.readUTF();
String c=dis.readUTF();
stmt=con.prepareStatement("select * from develop where Event=? ;");
stmt.setString(1,a);
ResultSet res=stmt.executeQuery();
if(res.next())
{
stmt=con.prepareStatement("update develop set Venue=?, Timing=? "
+ "where Event = ?;");
stmt.setString(1,b);
stmt.setString(2,c);
stmt.setString(3,a);
stmt.execute();
dout.writeUTF("Successfully Updated current record");
}
else
{
dout.writeUTF("Insert That Event first!");
}
}
}
else if(name.equals("Insert"))
{
synchronized(obj5)
{
String a=dis.readUTF();
String b=dis.readUTF();
String c=dis.readUTF();
stmt=con.prepareStatement("select * from develop where Event=?;");
stmt.setString(1,a);
System.out.println("Statement prepared!!!!");
ResultSet res=stmt.executeQuery();
System.out.println("Query Executed!!!");
if(res.next())
{
dout.writeUTF("Already created Event");
}
else
{
stmt=con.prepareStatement("insert into develop values(?,?,?);");
System.out.println("Second statemwnt created");
stmt.setString(1, a);
stmt.setString(2, b);
stmt.setString(3, c);
System.out.println("About to execute query");
stmt.execute();
dout.writeUTF("Successfully inserted");
}
}
}
else if(name.equals("Venue"))
{
synchronized(obj6)
{
stmt=con.prepareStatement("select * from develop;");
ResultSet res=stmt.executeQuery();
int n=0;
while(res.next())
n++;
dout.writeUTF(""+n);
res.absolute(0);
while(res.next())
{
dout.writeUTF(""+res.getString(1)+" - "+res.getString(2)+" - "+res.getString(3));
}
res.close();
}
}
else if(name.equals("Combo"))
{
synchronized(obj7)
{
stmt=con.prepareStatement("select Event from develop;");
ResultSet res=stmt.executeQuery();
int n=0;
while(res.next())
{
n++;
}
dout.writeUTF(""+n);
res.absolute(0);
while(res.next())
{
dout.writeUTF(res.getString(1));
}
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
public static void main(String args[]) throws IOException, SQLException{
con=Connect_db.still_connecting();
ss=new ServerSocket(8080);
while(true)
{
try
{
System.out.println("Waiting for client requests");
s=ss.accept();
System.out.println("Connection Established");
Server t1=new Server();
t1.start();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
break;
}
}
dout.close();
dis.close();
s.close();
ss.close();
con.close();
}
}