Student Portal Using Servlet

Practical : 7
Subject : Advanced Java
Aim : Create Servlet file which contains following functions:
1. Connect 2. Create Database 3. Create Table 4. Insert Records into respective table 5. Update records of particular table of database 6. Delete Records from table. 7. Delete table and also database.
index.html Code:
--------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>Java</title>
 <link rel="stylesheet" type="text/css" href="main.css">
  </head>
<body bgcolor="#222222">
<h1 align="center">Welcome To Student Portal</h1>
<div class="main">
<input type="button" class="button" name="insert_user" onclick="window.location.href='page.html'" value="Insert Student"><br>
<input type="button" class="button green" name="delete_user" onclick="window.location.href='delete.html'" value="Delete Student"><br>
<input type="button" class="button grey" name="update_user" onclick="window.location.href='update.html'" value="Update Student"><br>
<input type="button" class="button blue" name="select_user" onclick="window.location.href='select.html'" value="Select Student">
</div>
</body>
</html>
--------------------------------------------------------------------------------
delete.html Code:
--------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
    <head>
        <title>Servlet</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="main.css">
        
    </head>
    <body bgcolor="#222222">
 <form action="WebServlet" method="post" style="text-align:center;">  
<h2 style="color:white;font-family: Times; ">Student Information</h2>
<input class="side" type="text" pattern="[0-9]*" required="" name="s_enrollno" minlength="12" maxlength="12" placeholder="Enrollment No" style=" width: 370px;"><br>

<input class="b1" type="submit" value="Delete Data" name="s1">

</form>
    </body>
</html>

--------------------------------------------------------------------------------
page.html Code:
--------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
        <title>Servlet</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="main.css">

 </head>
<body bgcolor="#222222">
 <form action="WebServlet" method="post" style="text-align:center;">  


  
<h1 style="color:white;font-family: Times; ">Student Information</h1>


<input class="side" type="text"  pattern="[0-9]*" required="" name="s_enrollno" placeholder="Enrollment No" minlength="12" maxlength="12" maxlength="12"><br>
<input class="side a" type="text" pattern="[A-Za-z]+" required=""  name="s_firstname" placeholder="First Name">

<input class="side b" type="text" pattern="[A-Za-z]+" name="s_lastname" placeholder="Last Name" required><br>

<input class="side" type="text" pattern="[A-Za-z]+" name="s_branch" placeholder="Branch" required><br>

<input class="side" type="text"  pattern="[0-9]*" required="" name="s_mobileno" placeholder="Mobile No" minlength="10" maxlength="10"><br>

<input class="b1" type="submit" value="Insert Data" name="s1" >

</form>
    
</body>
</html>

--------------------------------------------------------------------------------
select.html Code:
--------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
    <head>
        <title>Servlet</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <link rel="stylesheet" type="text/css" href="main.css">
        </head>
    <body bgcolor="#222222">
 <form action="WebServlet" method="post" style="text-align:center;">  
 <h1 style="color:white;font-family: Times; ">Student Information</h1>
<input class="side" type="text" pattern="[0-9]*" required="" name="s_enrollno" placeholder="Enrollment No" minlength="12" maxlength="12" style=" width: 370px;"><br>

<input class="b1" type="submit" value="View Data" name="s1">

</form>
    </body>
</html>

--------------------------------------------------------------------------------
update.html Code:
--------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
    <head>
        <title>Servlet</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="main.css">
       </head>
    <body bgcolor="#222222">
 <form action="WebServlet" action="newupdate.html" method="post" style="text-align:center;">  

<h1 style="color:white;font-family: Times; ">Student Information</h1>
<h2 style="color:white;">Insert Enrollment No To Change Details Of Student</h2>
<input class="side" type="text" pattern="[0-9]*" required="" name="s_enrollno" placeholder="Enrollment No" minlength="12" maxlength="12"><br>
<h2 style="color:white;"">Insert New Data</h2>
<input class="side a" type="text" pattern="[A-Za-z]+" required=""  name="s_firstname" placeholder="First Name">

<input class="side b" type="text" pattern="[A-Za-z]+" name="s_lastname" placeholder="Last Name" required><br>

<input class="side" type="text" pattern="[A-Za-z]+" name="s_branch" placeholder="Branch" required><br>

<input class="side" type="text" pattern="[0-9]*" required="" name="s_mobileno" placeholder="Mobile No" minlength="10" maxlength="10" ><br>
<input class="b1"  type="submit" value="Update Data" name="s1" >

</form>
    </body>
</html>

--------------------------------------------------------------------------------
main.css Code:
--------------------------------------------------------------------------------
body
{
margin:0px;
padding:0px; 
}
.main
{
    text-align: center;
}
.button {
    background-color: #f44336; /* Green */
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    font-size: 30px;
    margin: 8px 2px;
    cursor: pointer;
    width: 400px;
    border: 0px solid black;
    transition: width 0.5s,background-color 0.5s;
}
.button:hover
{
width: 99%;
background-color: white;
color: black;
}
input
{
    margin: 50px; 
}
h1
{
color:white;
}
.green
{
background-color: #4CAF50;
}
.grey
{
background-color: #555555;
}
.blue
{
background-color: #008CBA;
}
.b1
{
    width: 395px;
    height: 40px;
    margin:10px;
    color: white;
    background-color: #008CBA;
    border: 0px solid grey;
    cursor: pointer;
}
.side
        {
        padding: 10px;
        margin: 10px; 
        width: 370px;
        }
        .a
        {
        width: 160px;
        }
        .b
        {
        width: 160px
        }
--------------------------------------------------------------------------------
WebServlet.java Code:
--------------------------------------------------------------------------------
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



import javax.servlet.*;
import javax.servlet.http.*;

 public class WebServlet  extends HttpServlet {

  
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
    try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("loaded driver succesfull");   
            Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","birju");
            Statement st=cn.createStatement();
            st.executeUpdate("create database student");
            st.executeUpdate("use student");
            st.execute("create table student2 (s_enrollno BIGINT,s_firstname varchar(20),s_lastname varchar(20),s_branch varchar(10),s_mobileno BIGINT)");
            
            ResultSet rs ;
            
        
           String s2 = request.getParameter("s1");
          
           Long eno = Long.valueOf(request.getParameter("s_enrollno"));
           
           String fname =request.getParameter("s_firstname");
           String lname = request.getParameter("s_lastname");
           String branch =request.getParameter("s_branch");
           String mobile=request.getParameter("s_mobileno");
          
           PreparedStatement sr;
           
           Statement stmt = cn.createStatement();
            ResultSet res;
                 
            // For Insert Data......
            switch (s2) {
            //For Delete Data......
                case "Insert Data":
                    String str = "insert into student2 values(?,?,?,?,?)";
                    sr = cn.prepareStatement(str);
                    sr.setLong(1,eno);
                    sr.setString(2,fname);
                    sr.setString(3,lname);
                    sr.setString(4,branch);
                    sr.setString(5,mobile);
                    int i = sr.executeUpdate();
                    out.println("Student Data inserted");
                    cn.close();
                    break;
                case "Delete Data":
                    String str1 = "select s_enrollno from student2 where s_enrollno = "+eno+" ";
                    rs =stmt.executeQuery(str1);
                    if(rs.next())
                    {
                        str1 = "delete from student2 where s_enrollno = ?";
                        sr = cn.prepareStatement(str1);
                        sr.setLong(1,eno);
                        sr.executeUpdate();
                        out.println("Student Data Deleted");
                    }
                    else{
                        out.println("Student Data Not Valid");
                    }       cn.close();
                    break;
                case "Update Data":
                    String str3 = "select * from student2 where s_enrollno = "+eno+"";
                    rs =stmt.executeQuery(str3);
                    if(rs.next())
                    {
                        str3= "update student2 set s_enrollno=? , s_firstname=?,s_lastname=?,s_branch=?,s_mobileno=? where s_enrollno = "+eno+"";
                        sr=cn.prepareStatement(str3);
                        sr.setLong(1,eno);
                        sr.setString(2,fname);
                        sr.setString(3,lname);
                        sr.setString(4,branch);
                        sr.setString(5,mobile);
                        
                        sr.executeUpdate();
                        
                        out.println("Update Student2 Data Succesfull");
                        cn.close();
                    }
                    else{
                        out.println("Student Data Not Valid");
                    }   break;
                case "View Data":
                    res = stmt.executeQuery("select * from student2 where s_enrollno="+eno+"");
                    if(res.next()){
                        Long enu= res.getLong("s_enrollno");
                        String fname1= res.getString("s_firstname");
                        String lname1= res.getString("s_lastname");
                        String branch1= res.getString("s_branch");
                        String mb1= res.getString("s_mobileno");
                        out.println("<html><body>");
                        out.println("Enrollment No:- "); out.println(enu);out.println("<br>");
                        out.println("First Name:- "); out.println(fname1);out.println("<br>");
                        out.println("Last Name:- "); out.println(lname1);out.println("<br>");
                        out.println("Branch:- "); out.println(branch1); out.println("<br>");
                out.println("Mobile No:- ");out.println(mb1);out.println("<br>");
               out.println("</html></body>");
            }
               else{
               out.println("Student Data is Not Valid");
               }    break;
            }
       }catch(ClassNotFoundException | SQLException e   )
       { System.out.println(e); }}}

--------------------------------------------------------------------------------
web.xml Code:
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
    <servlet>
        <servlet-name>WebServlet</servlet-name>
        <servlet-class>WebServlet</servlet-class>
    </servlet>
    <welecome-file-list>
        <welcome-file>MainPage.html</welcome-file>
           <welcome-file>index.html</welcome-file>
    </welecome-file-list>
   
    <servlet-mapping>
        <servlet-name>WebServlet</servlet-name>
        <url-pattern>/WebServlet</url-pattern>
    </servlet-mapping>
</web-app>

--------------------------------------------------------------------------------

Output:
index.html

page.html
result
select.html
result



delete.html
result

update.html

result





Previous
Next Post »

1 comments:

Write comments
12 June 2018 at 01:33 delete

nice
https://practicalserver.blogspot.com/search/label/java

Reply
avatar

Ads