Java Code for Request and Response for android application from MYSQL Database
package com.sawan;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Enumeration;
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 org.json.JSONException;
import org.json.JSONObject;
/**
* Servlet implementation class AbesentData
*/
@WebServlet("/AbesentData")
public class AbesentData extends HttpServlet {
private static final long serialVersionUID = 1L;
String dayss, monss, yearss;
PreparedStatement stmt = null;
ResultSet rs;
JSONObject json = new JSONObject();
public AbesentData() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
JSONObject json = new JSONObject();
Enumeration paramNames = request.getParameterNames();
String params[] = new String[3];
int i = 0;
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
params[i] = paramValues[0];
i++;
}
if (params[1].equals("AbsentStudent")) {
String table = params[0];
table = table.substring(0, 11);
DataBase db = new DataBase();
int clsstrength = 0;
Connection con = db.getConnection();
String date = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String redate2[] = date.split("_");
char[] ch = redate2[0].toCharArray();
String date3 = ch.toString();
yearss = redate2[0].substring(0, 4);
monss = redate2[0].substring(4, 6);
dayss = redate2[0].substring(6);
String redate = dayss + "/" + dayss + "/" + dayss;
String sql = "SELECT STUDENTNAME FROM attendance." + table + "record WHERE LACTUREDAY='" + dayss
+ "' AND LACTUREMonth='" + monss + "' AND LACTUREyear='" + yearss + "'";
String sql2 = "SELECT STUDENTNAME FROM attendance." + table + "record WHERE LACTUREDAY='" + dayss
+ "' AND LACTUREMonth='" + monss + "' AND LACTUREyear='" + yearss + "' AND LactureVarification=0";
try {
stmt = con.prepareStatement(sql);
rs = stmt.executeQuery();
int totalattendance = 1;
while (rs.next()) {
totalattendance++;
}
stmt = con.prepareStatement(sql2);
rs = stmt.executeQuery();
int j = 0;
while (rs.next()) {
String sql3 = "SELECT STUDENTNAME,STUDENTBRANCH,STUDENTSECTION FROM attendance." + table
+ " WHERE STUDENTROLL='" + rs.getString("STUDENTNAME") + "'";
stmt = con.prepareStatement(sql3);
ResultSet rs3 = stmt.executeQuery();
rs3.next();
json.put("name" + j, rs3.getString("STUDENTNAME"));
json.put("branch" + j, rs3.getInt("STUDENTBRANCH") + "");
json.put("Section" + j, rs3.getString("STUDENTSECTION"));
j++;
}
json.put("absent", j + "");
json.put("totalll", totalattendance + "");
json.put("info", "success");
} catch (JSONException e1) {
try {
json.put("info", "failure");
} catch (JSONException e) {
e.printStackTrace();
}
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
} catch (Exception e) {
}
} else if (params[1].equals("AbsentStudent2")) {
String table = params[0];
table = table.substring(0, 11);
DataBase db = new DataBase();
int clsstrength = 0;
Connection con = db.getConnection();
String date = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String redate2[] = date.split("_");
char[] ch = redate2[0].toCharArray();
String date3 = ch.toString();
yearss = redate2[0].substring(0, 4);
monss = redate2[0].substring(4, 6);
dayss = redate2[0].substring(6);
String redate = dayss + "/" + dayss + "/" + dayss;
int saw = Integer.parseInt(dayss);
try {
int j = 0;
int totalattendance = 1;
for (int sa = 1; sa <= saw; sa++) {
String sql = "SELECT STUDENTNAME FROM attendance." + table + "record WHERE LACTUREDAY='" + sa
+ "' AND LACTUREMonth='" + monss + "' AND LACTUREyear='" + yearss + "'";
String sql2 = "SELECT STUDENTNAME FROM attendance." + table + "record WHERE LACTUREDAY='" + sa
+ "' AND LACTUREMonth='" + monss + "' AND LACTUREyear='" + yearss
+ "' AND LactureVarification=0";
stmt = con.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
totalattendance++;
}
stmt = con.prepareStatement(sql2);
rs = stmt.executeQuery();
while (rs.next()) {
String sql3 = "SELECT * FROM attendance." + table + " WHERE STUDENTROLL='"
+ rs.getString("STUDENTNAME") + "'";
stmt = con.prepareStatement(sql3);
ResultSet rs3 = stmt.executeQuery();
rs3.next();
json.put("name" + j, rs3.getString("STUDENTNAME"));
json.put("name2" + j, rs3.getString("STUDENTROLL"));
json.put("branch" + j, rs3.getInt("STUDENTBRANCH") + "");
json.put("Section" + j, rs3.getString("STUDENTSECTION"));
json.put("sawan1" + j, 1 + "");
json.put("sawan2" + j, 1 + " ");
json.put("sawan3" + j, 1 + "");
j++;
}
}
json.put("absent", j + "");
json.put("totalll", totalattendance + "");
json.put("info", "success");
} catch (JSONException e1) {
try {
json.put("info", "failure");
} catch (JSONException e) {
e.printStackTrace();
}
e1.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
try {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
} catch (Exception e) {
System.out.println(e);
}
}
}
}
No comments:
Post a Comment