Insert update delete
Published: 2023-03-14 02:04:59

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static jdk.nashorn.tools.ShellFunctions.input;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author A
 */
public class form extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       response.setContentType("text/html;Charset=UTF-8");
       PrintWriter out = response.getWriter();
        
         out.println("<form><p>id : <input type='text' name='id'></p><p> name: <input type='text' name='name'></p>"
                 + "<p>age:<input type='text' name='age'></p>");
         out.println("<form><input type='submit' value='Add' name='add'>");
         out.println("<form><input type='submit' value='Update' name='update'>");
         out.println("<form><input type='submit' value='Delete' name='delete'>");
         String id=request.getParameter("id");
         String name=request.getParameter("name");
         String age=request.getParameter("age");
         if(request.getParameter("add")!=null){
         try{
             Class.forName("com.mysql.jdbc.Driver");
             Connection Con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/javapoint","root","");
               Statement stmt=Con.createStatement();
               stmt.executeUpdate("insert into emp values('"+id+"','"+age+"','"+name+"')");
               out.println("enter successfully");
               
               
         stmt.close();
         Con.close();
          
       } catch (ClassNotFoundException ex) {
            Logger.getLogger(form.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(form.class.getName()).log(Level.SEVERE, null, ex);
        }

         
       }
         if(request.getParameter("update")!=null){
         try{
             Class.forName("com.mysql.jdbc.Driver");
             Connection Con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/javapoint","root","");
               Statement stmt=Con.createStatement();
               stmt.executeUpdate("update emp set age='"+age+"',name='"+name+"'where id='"+id+"'");
               out.println("enter successfully");
               
            
             stmt.close();
              Con.close();
          
       } catch (ClassNotFoundException ex) {
            Logger.getLogger(form.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(form.class.getName()).log(Level.SEVERE, null, ex);
        }

       }
         if(request.getParameter("delete")!=null){
         try{
             Class.forName("com.mysql.jdbc.Driver");
             Connection Con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/javapoint","root","");
               Statement stmt=Con.createStatement();
               stmt.executeUpdate("delete from emp where id='"+id+"'");
               out.println("enter successfully");
               
             stmt.close();
              Con.close();
         
          
       } catch (ClassNotFoundException ex) {
            Logger.getLogger(form.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(form.class.getName()).log(Level.SEVERE, null, ex);
        }

       } 
    }}