
LOGIC SERVLET
package com.tcs.ilp.h51.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.catalina.startup.SetAllPropertiesRule;
import com.tcs.ilp.h51.DAO.Main;
import com.tcs.ilp.h51.DAO.Main.*;
import com.tcs.ilp.h51.model.Student;
/**
* Servlet implementation class LogicServlet
*/
public class LogicServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LogicServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("I am in do Post method");
//HttpSession session=request.getSession();
ArrayList<Student> al=new ArrayList<Student>();
String client=request.getParameter("check");
if(client.equals("add"))
{
System.out.println("I am in clent");
String id=request.getParameter("id");
String name=request.getParameter("name");
String college=request.getParameter("college");
String city=request.getParameter("city");
String mobile=request.getParameter("phone");
try {
al=Main.insertquery(id,name,college,city,mobile);
request.getSession().setAttribute("reports",al);
RequestDispatcher rd=request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(client.equals("delete")){
System.out.println("deleting the operation");
String id=request.getParameter("id");
try {
al=Main.deletequery(id);
RequestDispatcher rd=request.getRequestDispatcher("display1.jsp");
rd.forward(request, response);
}catch(Exception e){
}
}
else if(client.equals("viewd")){
System.out.println("Fetching the details");
String id=request.getParameter("id");
try {
al=Main.viewquery(id);
request.getSession().setAttribute("reports1",al);
RequestDispatcher rd=request.getRequestDispatcher("display2.jsp");
rd.forward(request, response);
}catch (Exception e){
}
}
else if(client.equals("update"))
{
System.out.println("I am in update");
String id=request.getParameter("id");
String name=request.getParameter("name");
try {
al=Main.updatequery(id,name);
request.getSession().setAttribute("reports2",al);
RequestDispatcher rd=request.getRequestDispatcher("display3.jsp");
rd.forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
DAOCODE package com.tcs.ilp.h51.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.tcs.ilp.h51.model.*;
public class Main {
static String name;
static String college;
static String city;
static String mobile;
static Connection conn=null;
static PreparedStatement stat=null;
static ResultSet rs=null;
public static ArrayList<Student>insertquery(String id,String name,String college,String city,String mobile) throws SQLException
{
ArrayList<Student> addlist=new ArrayList<Student>();
conn=DAOFactory.createConnection();
try
{
stat=conn.prepareStatement("insert into cred values(?,?,?,?,?)");
System.out.println("query is executing");
stat.setString(1, id);
stat.setString(2, name);
stat.setString(3, college);
stat.setString(4, city);
stat.setString(5, mobile);
int count=stat.executeUpdate();
if(count>0)
{
System.out.println("Successfully inserted");
}
else
{
System.out.println("Unsuccessful insertion");
}
stat=conn.prepareStatement("select * from cred");
rs=stat.executeQuery();
Student st=null;
while(rs.next()){
st=new Student();
st.setStudentId(rs.getString(1));
st.setStudentName(rs.getString(2));
st.setStudentCollege(rs.getString(3));
st.setCity(rs.getString(4));
st.setMobile(rs.getString(5));
addlist.add(st);
}
//return addlist;
}
catch (SQLException e) {
e.printStackTrace();
// TODO: handle exception
}
return addlist;
}
public static ArrayList<Student>deletequery(String id) throws SQLException
{
conn=DAOFactory.createConnection();
try
{
stat=conn.prepareStatement("delete from cred where id=?");
System.out.println("query is deleted");
stat.setString(1, id);
int count=stat.executeUpdate();
if(count>0)
{
System.out.println("Successfully deleted");
}
else
{
System.out.println("Unsuccessful deletion");
}
}catch(Exception e){
}
return null;
}
public static ArrayList<Student>viewquery(String id) throws SQLException
{
ArrayList<Student> addlist1=new ArrayList<Student>();
conn=DAOFactory.createConnection();
try
{
stat=conn.prepareStatement("select * from cred where id=?");
System.out.println("query is executing");
stat.setString(1, id);
rs=stat.executeQuery();
Student st=null;
while(rs.next()){
st=new Student();
st.setStudentId(rs.getString(1));
st.setStudentName(rs.getString(2));
st.setStudentCollege(rs.getString(3));
st.setCity(rs.getString(4));
st.setMobile(rs.getString(5));
addlist1.add(st);
}
//return addlist;
}
catch (SQLException e) {
e.printStackTrace();
// TODO: handle exception
}
return addlist1;
}
public static ArrayList<Student>updatequery(String id, String name) throws SQLException
{
ArrayList<Student> addlist2=new ArrayList<Student>();
conn=DAOFactory.createConnection();
try
{
stat=conn.prepareStatement("update cred set name=? where id=?");
System.out.println("query is executing");
stat.setString(1, name);
stat.setString(2, id);
int count=stat.executeUpdate();
Student st=null;
while(rs.next()){
st=new Student();
st.setStudentId(rs.getString(1));
st.setStudentName(rs.getString(2));
st.setStudentCollege(rs.getString(3));
st.setCity(rs.getString(4));
st.setMobile(rs.getString(5));
addlist2.add(st);
System.out.println("haihai");
}
}
catch (SQLException e) {
e.printStackTrace();
// TODO: handle exception
}
return addlist2;
}
}
jsp pages
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.