Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Basic CRUD operations
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Rishi
Basic CRUD operations
0 Answer(s)      2 years and 10 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


Advertisement
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 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
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
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. In JPA the EntityManager
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
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
basic html
basic html  what is the difference between html4 and html5
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 question
basic question  how we create a new table with the same structure and data of another table
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
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
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
basic function of computer system
basic function of computer system  What is the basic function of computer system
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 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
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
basic c/c++ questions
basic c/c++ questions  Are the Control statements like if(),while() in built functions? if no, why not as they satisfy all the properties of a function
Advertisements
 

 

 

DMCA.com