-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageCounter.java
93 lines (82 loc) · 2.78 KB
/
MessageCounter.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
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.Gson;
/**
* Servlet implementation class MessageCounter
*/
@WebServlet("/MessageCounter")
public class MessageCounter extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MessageCounter() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession ses=request.getSession();
String number=String.valueOf(request.getParameter("senderNumber"));
Map<String, Integer> map = new TreeMap<String, Integer>();
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/chats", "root", "");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SHOW TABLES;");
while (rs.next()) {
String tName=rs.getString(1);
String temp = tName;
temp=temp.replace("a","");
String arr[] = temp.split("_");
// System.out.println(temp);
// if (arr[0].equals(n)) {
statement = connection.createStatement();
if (arr[0].equals(number)) {
//out.println(tName);
int msgCount=0;
ResultSet rs1 = statement.executeQuery("select * from "+tName+" where status='unread' and who='"+arr[1]+"'");
while(rs1.next()) {
msgCount++;
}
map.put(arr[1], msgCount);
} else if (arr[1].equals(number)) {
int msgCount=0;
ResultSet rs2 = statement.executeQuery("select * from "+tName+" where status='unread' and who='"+arr[0]+"'");
while(rs2.next()) {
msgCount++;
}
map.put(arr[0], msgCount);
}
}
out.println(new Gson().toJson(map));
}catch (Exception e) {
out.println("Error "+e);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}