Search the Blog

Saturday, July 27, 2019

Code for Server Response with SQL Update Programming Logic and Code with Syntax

Code for Server  Response with SQL Update  Programming Logic and Code with Syntax 


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 Academic
 */
@WebServlet("/Academic")
public class Academic extends HttpServlet {
private static final long serialVersionUID = 1L;
     
String dayss, monss, yearss;
PreparedStatement stmt = null;
ResultSet rs;
JSONObject json = new JSONObject();
    public Academic() {
        super();
        // TODO Auto-generated constructor stub
    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
JSONObject json = new JSONObject();
Enumeration paramNames = request.getParameterNames();
String params[] = new String[1];
int i = 0;
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
params[i] = paramValues[0];
i++;
}

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 monthsss=Integer.parseInt(monss);
int daysss=Integer.parseInt(dayss);
monss=monthsss+"";
dayss=daysss+"";
String sql = "SELECT * FROM attendance." + table + "cal  WHERE datee='" + dayss
+ "' AND monthh='" + monss + "' AND yearr='" + yearss + "'";
try {
stmt = con.prepareStatement(sql);
rs = stmt.executeQuery();
int totalattendance = 1;
rs.next();
int noo1=rs.getInt("noo");
String sql2 = "SELECT * FROM attendance." + table + "cal  WHERE datee='30' AND monthh='03' AND yearr='" + (Integer.parseInt(yearss)+1)+"" + "'";
stmt = con.prepareStatement(sql2);
rs = stmt.executeQuery();
rs.next();
int j = 0;
String sql3 = "SELECT * FROM attendance." + table + "cal  WHERE noo >= "+noo1+" and accss=1 ";
stmt = con.prepareStatement(sql3);
rs = stmt.executeQuery();
while (rs.next()) {
String datee=rs.getString("datee");
String monthh=rs.getString("monthh");
String yearrr=rs.getString("yearr");
json.put("name" + j, datee+"-"+monthh+"-"+yearrr);
json.put("branch" + j, rs.getString("content"));
j++;
}
json.put("absent", j + "");
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);
}

}

}





Java Code for Request and Response for android application from MYSQL Database  Programming Logic and Code with Syntax 

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

Translate