Basic CRUD operations

Basic CRUD operations

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

View Answers









Related Tutorials/Questions & Answers:
Basic CRUD operations
Basic CRUD operations  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
Basic CRUD operations
Basic CRUD operations  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
Advertisements
Basic CRUD operations
Basic CRUD operations  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
CRUD operations in spring mvc 2.5
CRUD operations in spring mvc 2.5  Hi, I want to do CRUD operations on jquery datatable such as edit delete select and add row. I am using spring mvc 2.5. How can I do this. Thanks in advance
crud operations with foreign key constraint.
crud operations with foreign key constraint.  I neeed to know how to perform crud operations in mysql with foreign key constraint using mvc architecture i.e using servlet jsp and bean. please provide all the code so that i can
ModuleNotFoundError: No module named 'basic-crud'
ModuleNotFoundError: No module named 'basic-crud'  Hi, My Python... 'basic-crud' How to remove the ModuleNotFoundError: No module named 'basic-crud' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'django-basic-crud'
ModuleNotFoundError: No module named 'django-basic-crud'  Hi, My... named 'django-basic-crud' How to remove the ModuleNotFoundError: No module named 'django-basic-crud' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-basic-crud'
ModuleNotFoundError: No module named 'django-basic-crud'  Hi, My... named 'django-basic-crud' How to remove the ModuleNotFoundError: No module named 'django-basic-crud' error? Thanks   Hi, In your
How to perform CRUD operations using gwt on Restlet server 2.0
How to perform CRUD operations using gwt on Restlet server 2.0  I want to perform CRUD operation using gwt on restlet server 2.0. The CRUD operations are like create, read, update, delete operations.Any generic code
JPA 2.1 CRUD examples
will learn how to create example program that forms CRUD operations against... a beginner should learn first. What is CRUD operations in JPA? The CRUD stands... CRUD operations on the Entity objects.ADS_TO_REPLACE_1 In JPA
Operations on string
Operations on string  Apply undo,redo operations on String pool in java
Operations with Calendar
Operations with Calendar  I have the following as my known parameter Effective Date : 21-08-2012 Cut off day : Thursday I want my first cycle calculation done from 21-08-2012 till wednesday(22-08-2012) and from thursday my
CRUD SERVLET
CRUD SERVLET  How to create a crud servlet?   Helper Class package com.nsep.user.helper; import java.sql.*; import java.util.logging.Logger; public class DataBaseUtil { Logger logger=Logger.getLogger("Logging
CRUD DAO
CRUD DAO  how to create dao for create,read,update and delete?   /* *ConnectionManager * * *Version:1.0 * *Date:25-Nov-2011 * */ package com.student.dao; import java.sql.*; import org.apache.log4j.Logger
ModuleNotFoundError: No module named 'crud'
ModuleNotFoundError: No module named 'crud'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'crud' How to remove the ModuleNotFoundError: No module named 'crud' error
For CRUD application - Spring
For CRUD application  Hi, Can i have Crud(create ,edit,update,delete the data in database ) code & search the eployee using "id or name" using Spring ,Hibernate and Mysql Thanks Raghavendra
ModuleNotFoundError: No module named 'Operations'
ModuleNotFoundError: No module named 'Operations'  Hi, My Python... 'Operations' How to remove the ModuleNotFoundError: No module named 'Operations' error? Thanks   Hi, In your python environment you
Version of com.atsid>play-crud dependency
List of Version of com.atsid>play-crud dependency
Version of com.jeromeloisel>elasticsearch-crud dependency
List of Version of com.jeromeloisel>elasticsearch-crud dependency
ModuleNotFoundError: No module named 'crud-factory'
ModuleNotFoundError: No module named 'crud-factory'  Hi, My Python... 'crud-factory' How to remove the ModuleNotFoundError: No module named 'crud-factory' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'pyramid_crud'
ModuleNotFoundError: No module named 'pyramid_crud'  Hi, My Python... 'pyramid_crud' How to remove the ModuleNotFoundError: No module named 'pyramid_crud' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'smorest-crud'
ModuleNotFoundError: No module named 'smorest-crud'  Hi, My Python... 'smorest-crud' How to remove the ModuleNotFoundError: No module named 'smorest-crud' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'crud_filters'
ModuleNotFoundError: No module named 'crud_filters'  Hi, My Python... 'crud_filters' How to remove the ModuleNotFoundError: No module named 'crud_filters' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'generic-crud'
ModuleNotFoundError: No module named 'generic-crud'  Hi, My Python... 'generic-crud' How to remove the ModuleNotFoundError: No module named 'generic-crud' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'crud-factory'
ModuleNotFoundError: No module named 'crud-factory'  Hi, My Python... 'crud-factory' How to remove the ModuleNotFoundError: No module named 'crud-factory' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'crud_filters'
ModuleNotFoundError: No module named 'crud_filters'  Hi, My Python... 'crud_filters' How to remove the ModuleNotFoundError: No module named 'crud_filters' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'crud_filters'
ModuleNotFoundError: No module named 'crud_filters'  Hi, My Python... 'crud_filters' How to remove the ModuleNotFoundError: No module named 'crud_filters' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Flask-Crud'
ModuleNotFoundError: No module named 'Flask-Crud'  Hi, My Python... 'Flask-Crud' How to remove the ModuleNotFoundError: No module named 'Flask-Crud' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'ps_crud'
ModuleNotFoundError: No module named 'ps_crud'  Hi, My Python..._crud' How to remove the ModuleNotFoundError: No module named 'ps_crud... to install padas library. You can install ps_crud python with following command
ModuleNotFoundError: No module named 'qnc-crud'
ModuleNotFoundError: No module named 'qnc-crud'  Hi, My Python...-crud' How to remove the ModuleNotFoundError: No module named 'qnc-crud... to install padas library. You can install qnc-crud python with following
ModuleNotFoundError: No module named 'qnc-crud'
ModuleNotFoundError: No module named 'qnc-crud'  Hi, My Python...-crud' How to remove the ModuleNotFoundError: No module named 'qnc-crud... to install padas library. You can install qnc-crud python with following
ModuleNotFoundError: No module named 'sanic-crud'
ModuleNotFoundError: No module named 'sanic-crud'  Hi, My Python... 'sanic-crud' How to remove the ModuleNotFoundError: No module named 'sanic-crud' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'apistar-crud'
ModuleNotFoundError: No module named 'apistar-crud'  Hi, My Python... 'apistar-crud' How to remove the ModuleNotFoundError: No module named 'apistar-crud' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'crud-factory'
ModuleNotFoundError: No module named 'crud-factory'  Hi, My Python... 'crud-factory' How to remove the ModuleNotFoundError: No module named 'crud-factory' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-crud'
ModuleNotFoundError: No module named 'django-crud'  Hi, My Python... 'django-crud' How to remove the ModuleNotFoundError: No module named 'django-crud' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'Flask-Crud'
ModuleNotFoundError: No module named 'Flask-Crud'  Hi, My Python... 'Flask-Crud' How to remove the ModuleNotFoundError: No module named 'Flask-Crud' error? Thanks   Hi, In your python environment you
basic html
basic html  what is the difference between html4 and html5
ModuleNotFoundError: No module named 'list_operations'
ModuleNotFoundError: No module named 'list_operations'  Hi, My... named 'list_operations' How to remove the ModuleNotFoundError: No module named 'list_operations' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'logion-operations'
ModuleNotFoundError: No module named 'logion-operations'  Hi, My... named 'logion-operations' How to remove the ModuleNotFoundError: No module named 'logion-operations' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'make-operations'
ModuleNotFoundError: No module named 'make-operations'  Hi, My... named 'make-operations' How to remove the ModuleNotFoundError: No module named 'make-operations' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'matrix-operations'
ModuleNotFoundError: No module named 'matrix-operations'  Hi, My... named 'matrix-operations' How to remove the ModuleNotFoundError: No module named 'matrix-operations' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'text_operations'
ModuleNotFoundError: No module named 'text_operations'  Hi, My... named 'text_operations' How to remove the ModuleNotFoundError: No module named 'text_operations' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'dist-operations'
ModuleNotFoundError: No module named 'dist-operations'  Hi, My... named 'dist-operations' How to remove the ModuleNotFoundError: No module named 'dist-operations' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'logion-operations'
ModuleNotFoundError: No module named 'logion-operations'  Hi, My... named 'logion-operations' How to remove the ModuleNotFoundError: No module named 'logion-operations' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'make-operations'
ModuleNotFoundError: No module named 'make-operations'  Hi, My... named 'make-operations' How to remove the ModuleNotFoundError: No module named 'make-operations' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'rpaframework-operations'
ModuleNotFoundError: No module named 'rpaframework-operations'  Hi...: No module named 'rpaframework-operations' How to remove the ModuleNotFoundError: No module named 'rpaframework-operations' error? Thanks   
ModuleNotFoundError: No module named 'Complex-Operations'
ModuleNotFoundError: No module named 'Complex-Operations'  Hi, My... named 'Complex-Operations' How to remove the ModuleNotFoundError: No module named 'Complex-Operations' error? Thanks   Hi, In your
basic question
basic question  how we create a new table with the same structure and data of another table
CRUD application in hibernate annotation
CRUD application in hibernate annotation       In this section, you will learn how to develop a CRUD application... CHARSET=latin1  Follows the following steps for developing the CRUD
JPA Crud
JPA Crud       In this section we will show you how to develop simple CRUD application using JPA... the application. This simple JPA CRUD application can be the base for your future

Ads