Insert, update, delete in jsp and servlet with mysql database


Database:
database name: db_school

create table tbl_student(
 id int not null auto_increment,
name varchar(255),
email varchar(255),
mobile varchar(255),
roll int,
subject varchar(255),
university varchar(255),
primary key(id)
);



index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Md. Alamgir Hossain</title>
</head>
<style>
    button {
          height: 50px;
          width: 100px;
}  
</style>
<body>
  
    <h1 align="center">Welcome to Student Management<br>
    <a href="register.jsp"><button>Register</button></a>
  
    </h1>
</body>
</html>

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration</title>
</head>
<body>
    <h1 align="center">Complete Registration with Correct Information</h1>
    <form action="RegisterServ" method="post" name="registerForm">
        <table align="center" border="1">
            <tr width="100%">
                <td>Name:</td>
                <td><input type="text" name="name" placeholder="Enter your name--"></td>
            </tr>
            <tr width="100%">
                <td>Email:</td>
                <td><input type="text" name="email" placeholder="Enter your email--"></td>
            </tr>
            <tr width="100%">
                <td>Mobile:</td>
                <td><input type="text" name="mobile" placeholder="Enter your mobile--"></td>
            </tr>
            <tr width="100%">
                <td>Roll:</td>
                <td><input type="text" name="roll" placeholder="Enter your roll---"></td>
            </tr>
            <tr width="100%">
                <td>Subject:</td>
                <td><input type="text" name="subject" placeholder="Enter your subject---"></td>
            </tr>
            <tr width="100%">
                <td>University:</td>
                <td><input type="text" name="university" placeholder="Enter your university"></td>
            </tr>
        </table>
        <table align="center">
            <tr width="100%">
                <td><b><input type="submit" value="Submit" name="registerSubmit"></b></td>
            </tr>
        </table>
    </form>
</body>
</html>

home.jsp


<%@page import="com.Util.DBConnection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>All Registered</title>
</head>
<body>
<%
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
%>
    <h1 align="center">All registered members are</h1>
    <table align="center" name="tblHome" border="1">
        <tr width="100%">
            <td align="center">Name</td>
            <td align="center">Email</td>
            <td align="center">Number</td>
            <td align="center">Roll</td>
            <td align="center">Subject</td>
            <td align="center">University</td>
            <td align="center">Actions</td>
        </tr>
        <%
            conn = DBConnection.createConnection();
            String selectQuery = "SELECT * FROM tbl_student";
            pst = conn.prepareStatement(selectQuery);
            rs = pst.executeQuery();
          
            while(rs.next()){
        %>
            <tr width="100%">
                <td align="center"><%=rs.getString("name") %></td>
                <td align="center"><%=rs.getString("email") %></td>
                <td align="center"><%=rs.getString("mobile") %></td>
                <td align="center"><%=rs.getString("roll") %></td>
                <td align="center"><%=rs.getString("subject") %></td>
                <td align="center"><%=rs.getString("university") %></td>
                <td align ="center">
                    <a href="edit.jsp?roll=<%=rs.getString("roll")%>">Edit</a>
                    <a href="delete.jsp?rol=<%=rs.getString("roll")%>">Delete</a>
                </td>
            </tr>
        <%} %>
    </table>
</body>
</html>

delete.jsp


<%@page import="java.sql.Statement"%>
<%@page import="com.Util.DBConnection"%>
<%@page import="java.sql.Connection"%>
<%
    String rolll = request.getParameter("rol");
    int roll = Integer.parseInt(rolll);
    Connection conn = DBConnection.createConnection();
    Statement st = conn.createStatement();
    st.executeUpdate("delete from tbl_student where roll = '"+roll+"'");
    //st.executeUpdate(dlteQuery);
    out.println("Deleted!!");
    response.sendRedirect("home.jsp");
%>

edit.jsp

<%@page import="com.Util.DBConnection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update Information</title>
</head>
<body>
<%
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
  
    String roll = request.getParameter("roll");
    int rolll = Integer.parseInt(roll);
%>
<h1 align="center">Update information with Correct Info</h1>
    <form action="updateServ" method="post" name="registerForm">
    <%
        conn = DBConnection.createConnection();
        String sql = "select * from tbl_student where roll='"+rolll+"'";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        while(rs.next()){
    %>
        <table align="center" border="1">
            <tr width="100%">
                <td>Name:</td>
                <td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
            </tr>
            <tr width="100%">
                <td>Email:</td>
                <td><input type="text" name="email" value="<%=rs.getString("email")%>"></td>
            </tr>
            <tr width="100%">
                <td>Mobile:</td>
                <td><input type="text" name="mobile" value="<%=rs.getString("mobile")%>"></td>
            </tr>
            <tr width="100%">
                <td>Roll:</td>
                <td><input type="text" name="roll" value="<%=rs.getString("roll")%>"></td>
            </tr>
            <tr width="100%">
                <td>Subject:</td>
                <td><input type="text" name="subject" value="<%=rs.getString("subject")%>"></td>
            </tr>
            <tr width="100%">
                <td>University:</td>
                <td><input type="text" name="university" value="<%=rs.getString("university")%>"></td>
            </tr>
        </table>
        <%} %>
        <table align="center">
            <tr width="100%">
                <td><b><input type="submit" value="Update" name="registerSubmit"></b></td>
            </tr>
        </table>
    </form>
    </body>
</html>

RegisterServ.java//com.Controller

package com.Controller;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Dao.RegisterDao;
import com.Model.Register;

/**
 * Servlet implementation class RegisterServ
 */
@WebServlet("/RegisterServ")
public class RegisterServ extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServ() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        PrintWriter out = response.getWriter();
      
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        String roll = request.getParameter("roll");
        String subject = request.getParameter("subject");
        String university = request.getParameter("university");
      
        Register register = new Register();
        register.setName(name);
        register.setEmail(email);
        register.setMobile(mobile);
        register.setRoll(roll);
        register.setSubject(subject);
        register.setUniversity(university);
      
        RegisterDao registerDao = new RegisterDao();
        String return_message;
        try {
            return_message = registerDao.Userregister(register);
            if(return_message.equals("SUCCESS")) {
                request.getRequestDispatcher("/home.jsp").forward(request, response);
            }else {
                request.setAttribute("errMessage", return_message);
                request.getRequestDispatcher("/register.jsp").forward(request, response);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
      
      
    }

}

updateServ.java//com.Controller

package com.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Util.DBConnection;

/**
 * Servlet implementation class updateServ
 */
@WebServlet("/updateServ")
public class updateServ extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public updateServ() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String number = request.getParameter("mobile");
        String rollll = request.getParameter("roll");
        String subject = request.getParameter("subject");
        String university = request.getParameter("university");
        int rr = Integer.parseInt(rollll);
        Connection conn = null;
        PreparedStatement pst = null;
      
        try {
            conn = DBConnection.createConnection();
            String updateQuery = "update tbl_student set name=?, email=?, mobile=?, subject=?, university=? where roll='"+rr+"'";
            pst = conn.prepareStatement(updateQuery);
            if(rollll!=null){
              
                String updateQuery1 = "update tbl_student set name=?, email=?, mobile=?, subject=?, university=? where roll='"+rr+"'";
                pst = conn.prepareStatement(updateQuery1);
                pst.setString(1, name);
                pst.setString(2, email);
                pst.setString(3, number);
                //pst.setString(4, rollll);
                pst.setString(4, subject);
                pst.setString(5, university);
                //pst.setString(7, rollll);
                pst.executeUpdate();
                out.println("Updated");
                //response.sendRedirect("home.jsp");
                request.getRequestDispatcher("/home.jsp").forward(request, response);
            }
          
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
    }

}

RegisterDao.java//com.Dao

package com.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.Model.Register;
import com.Util.DBConnection;

public class RegisterDao {
    public String Userregister(Register register) throws ClassNotFoundException {
        String name = register.getName();
        String email = register.getEmail();
        String mobile = register.getMobile();
        String roll = register.getRoll();
        String subject = register.getSubject();
        String university = register.getUniversity();
      
        Connection conn = null;
        PreparedStatement pSt = null;
      
        try {
            conn = DBConnection.createConnection();
            String insertQuery = "insert into tbl_student(name, email, mobile, roll, subject, university) values(?,?,?,?,?,?)";
            pSt = conn.prepareStatement(insertQuery);
            pSt.setString(1, name);
            pSt.setString(2, email);
            pSt.setString(3, mobile);
          
            int r = Integer.parseInt(roll);
            pSt.setInt(4, r);
            pSt.setString(5, subject);
            pSt.setString(6, university);
          
            int i = pSt.executeUpdate();
          
            if(i!=0) {
                return "SUCCESS";
            }
          
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
      
        return "Something Went qrong!!!";
    }
}

Register.java//com.Model

package com.Model;

public class Register {
    private String name, email, mobile, roll, subject, university;
  
    public String getName() {
        return name;
    }
    public String getEmail() {
        return email;
    }
    public String getMobile() {
        return mobile;
    }
    public String getRoll() {
        return roll;
    }
    public String getSubject() {
        return subject;
    }
    public String getUniversity() {
        return university;
    }
  
    ///Set method start
    public void setName(String name) {
        this.name = name;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public void setRoll(String roll) {
        this.roll = roll;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public void setUniversity(String university) {
        this.university = university;
    }
}

DBConnection.java//com.Util


package com.Util;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {
    public static Connection createConnection() throws ClassNotFoundException {
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/db_school";
        String user = "root";
        String password = "";
      
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected");
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}

mysql jdbc connector download link:
 

No comments:

Post a Comment