Home Answers Viewqa Java-Beginners Basic CRUD operations

 
 


Rishi
Basic CRUD operations
0 Answer(s)      a year and 5 months ago
Posted in : Java Beginners

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 Pages:
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
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
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
JSP CRUD Application
, and the JDBC. We will understand here the basic purpose of creating a CRUD...Create JSP CRUD Application and run on Tomcat 7 In this section we will discuss about how to create a simple crud application in JSP using Eclipse IDE. We
Hibernate Performing the usual DB operations
HIBERNATE-BASICS Hibernate Performing the usual DB operations... outlines the basic features of the Hibernate environment and the code for performing the usual DB operations. Hybernate2 can be downloaded from http
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
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
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 application in hibernate annotation
CRUD application in hibernate annotation       In this section, you will learn how to develop a CRUD application...  Follows the following steps for developing the CRUD application
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
Basic Steps to Outsourcing Success, Steps to Success in Outsourcing
Basic Steps to Outsourcing Success Introduction There are a few fundamental steps to ensure the best results for your outsourcing venture. These strategies... to the vendor and finalize the best practices. This is where the operations begin
How to make a CRUD without using SQL Server? by just using your GUI? (CRUD = Creating, Register, Update, Delete)
How to make a CRUD without using SQL Server? by just using your GUI? (CRUD = Creating, Register, Update, Delete)  well I got a Program that can... a CRUD again without using SQL Server. how to make it? please help
basic html
basic html  what is the difference between html4 and html5
basic question
basic question  how we create a new table with the same structure and data of another table
CRUD application in JPA
CRUD application in JPA      ... a CRUD application in JPA (Java Persistence APIs).  Table name... the CRUD application in JPA : Step 1: The hibernate.cfg.xml
basic doubts
basic doubts  can i write two beans within a same xml file..? Also can i write two xml files within a same package
basic program
basic program  hii actully i am goin for technical interview on java as fresher so please tell me what these two basic lines means : public static void main(String arg) and System.out.print(); please give me meaning of each
A basic program
A basic program  Write a program that will print out the first 10 numbers, their squares and their cubes. Write a program that displays the first 100 terms of the triangular sequence. This is the sequence that goes
Creating and running the JPA CRUD application
Creating and running the JPA CRUD application   ... different project artifacts for JPA CRUD demo example code. After completing... the CRUD demo application. Following the following steps to create the JPA
basic function of computer system
basic function of computer system  What is the basic function of computer system
Basic Validators provided by the framework.
Basic Validators provided by the framework.  What are Validators? and What are Basic Validators provided by the framework
Java JMenuItem to perform file operations
Java JMenuItem to perform file operations In this tutorial, you will learn how to use JMenuItem class to perform file operations. Java Swing introduced... example in java swing through which you can perform open and save operations
Summary - Basic Elements
Java: Summary - Basic Elements In this section we will see how to comment your Java code. We will also see the Identifiers and Keyword of Java API. Properly commenting the Java code is very important. Commenting the programming code
Summary - Basic Elements
Java: Summary - Basic Elements In this section we will learn about commenting in Java. We will learn following things: a) Identifiers in Java. b) Object types in Java. c) Code commenting in Java d) Integer types in Java e) Keywords
binary addition,subtraction and modulus operations - Java Beginners
operations on these two binary numbers and finally dipaly the result in decimal... operations as urgent as possible on my email. i would be highly obliged. 
Basic Java Tutorial
Basic Java Tutorial  Hi, Where is the url of Basic Java Tutorials on roseindia.net? Thanks   Hi, You can Learn Java in a Day also? Thanks
java and visual basic
java and visual basic  Hello, Please how does java and visual basic interact or talk

Ask Questions?

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.