Skip to content

Commit 12bf3ff

Browse files
committed
Create Handler.java
The standalone Java program
1 parent b702626 commit 12bf3ff

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Handler.java

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
6+
7+
import java.net.DatagramPacket;
8+
import java.net.DatagramSocket;
9+
import java.net.InetAddress;
10+
import java.sql.*;
11+
12+
/**
13+
*
14+
* @author Reza Nourbakhsh
15+
*/
16+
public class Contact {
17+
18+
public void ServerProcess()
19+
{
20+
try
21+
{
22+
String STUNAME="";
23+
String ADDRESS="";
24+
String POSTCODE="";
25+
String EMAIL="";
26+
27+
28+
DatagramSocket serverSocket = new DatagramSocket(9875);
29+
byte[] receiveData = new byte[1024];
30+
byte[] sendData = new byte[1024];
31+
while(true)
32+
{
33+
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
34+
serverSocket.receive(receivePacket);
35+
String sentence = new String( receivePacket.getData());
36+
System.out.println("RECEIVED:" + sentence);
37+
InetAddress IPAddress = receivePacket.getAddress();
38+
int port = receivePacket.getPort();
39+
//Code to retreive data from database
40+
boolean FindFlag = false;
41+
try
42+
{
43+
String filename = "STUCONTACT.mdb";
44+
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
45+
database+= filename.trim() ;
46+
47+
Connection con = DriverManager.getConnection(database,"","");
48+
49+
Statement stmt = con.createStatement();
50+
51+
ResultSet rs = stmt.executeQuery("SELECT STUNAME,ADDRESS,POSTCODE,EMAIL FROM Contacts where STUID='"+sentence.trim()+"'");
52+
53+
while (rs.next()) {
54+
55+
STUNAME = rs.getString("STUNAME");
56+
ADDRESS = rs.getString("ADDRESS");
57+
POSTCODE = rs.getString("POSTCODE");
58+
EMAIL = rs.getString("EMAIL");
59+
FindFlag = true;
60+
}
61+
62+
}
63+
catch(Exception e)
64+
{
65+
System.out.println("Error Ocurrs");
66+
}
67+
if(FindFlag == true)
68+
{
69+
sentence = STUNAME + ADDRESS + POSTCODE + EMAIL ;
70+
}
71+
else
72+
{
73+
sentence = "There is no student with this code";
74+
}
75+
String capitalizedSentence = sentence;
76+
77+
//************************************************
78+
sendData = capitalizedSentence.getBytes();
79+
DatagramPacket sendPacket =
80+
new DatagramPacket(sendData, sendData.length, IPAddress, port);
81+
serverSocket.send(sendPacket);
82+
//************************************************
83+
}
84+
}
85+
catch(Exception e)
86+
{
87+
88+
}
89+
90+
}
91+
92+
}

0 commit comments

Comments
 (0)