-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPServer.java
More file actions
69 lines (56 loc) · 2.22 KB
/
TCPServer.java
File metadata and controls
69 lines (56 loc) · 2.22 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
import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;
public class TCPServer {
private ServerSocket TCPSocket;
public TCPServer(int port)throws IOException{
TCPSocket = new ServerSocket(port);
TCPSocket.setSoTimeout(2147483647);
}
public void retrieveEmployee(){
while(true)
{
try
{
System.out.println("\n\nWaiting for client on port " + TCPSocket.getLocalPort() + "...");
Socket server = TCPSocket.accept();
System.out.println("\n\nJust connected to "+ server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
int x = in.readInt();
System.out.println("\n\nClient Requested for employee "+x);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/EmployeeDB","root", "pass");
PreparedStatement stmt=con.prepareStatement("SELECT *FROM EMPLOYEE WHERE Eid = ?");
stmt.setInt(1,x);
List<Employee> details = new ArrayList<Employee>();
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Employee E = new Employee(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getInt(5));
details.add(E);
}
String str = null;
for(Employee E : details){
str = "\n\n\nEMPLOYEE ID : "+Integer.toString(E.getEid())+"\nEMPLOYEE NAME : "+E.getName()+"\nPOST : "+E.getPost()+"\nDEPT ID : "+Integer.toString(E.getDid())
+"\nSALARY : "+Integer.toString(E.getSalary())+"\n\n\n";
}
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF(str);
server.close();
}
catch(Exception e){
System.out.println("Server side Retrieval problem : ");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
int port = 6666;
try{
TCPServer t = new TCPServer(port);
t.retrieveEmployee();
}catch(Exception e){
System.out.println("Server main method problem : "+e);
}
}
}