Advanced Java Project – Student Management System using MVC ORM Part-5

Program 1

package dao;

import model.Student;
import java.sql.*;
import java.util.*;
public class StudentDAO 
{
    public static boolean checkLogin(String user,String pass)
    {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
      try
      {    
        con=mycon.Myconnection.getConnection();
        String sql;
        sql="select * from stlogin where username=? and password=?";
        ps=con.prepareStatement(sql);
        ps.setString(1, user);
        ps.setString(2, pass);
        rs=ps.executeQuery();
        if(rs.next())
            return true;
      }
      catch(Exception e)
      {
          System.out.println(e);
      }
    return false;            
   }
    public boolean insertStudent(Student S)    
    {
        Connection con=null;
        PreparedStatement ps=null;
      try
      {    
        con=mycon.Myconnection.getConnection();
        String sql;
        sql="insert into student values(?,?,?,?,?)";
        ps=con.prepareStatement(sql);
        ps.setInt(1, S.getRno());
        ps.setString(2, S.getName());
        ps.setInt(3, S.getPhy());
        ps.setInt(4, S.getChem());
        ps.setInt(5, S.getMath());
        if(ps.executeUpdate()>0)
            return true;
      }
      catch(Exception e)
      {
           System.out.println(e);
      }
      return false;         
    }   
    public boolean updateStudent(Student S)
    {
        Connection con=null;
        PreparedStatement ps=null;
      try
      {    
        con=mycon.Myconnection.getConnection();
        String sql;
        sql="update student set name=?,phy=?,chem=?,math=? where sno=?";
        ps=con.prepareStatement(sql);
        ps.setString(1, S.getName());
        ps.setInt(2, S.getPhy());
        ps.setInt(3, S.getChem());
        ps.setInt(4, S.getMath());
        ps.setInt(5, S.getRno());
        if(ps.executeUpdate()>0)
            return true;
      }  
       catch(Exception e)
      {
           System.out.println(e);
      } 
     return false;   
    }
    public Student searchByRno(int rno)
    {
     Connection con=null;
     PreparedStatement ps=null;
     ResultSet rs=null;
      Student S=new Student();
     try
     {
         con=mycon.Myconnection.getConnection();
         String sql;
         sql="select * from student where sno=?";
         ps=con.prepareStatement(sql);
         ps.setInt(1,rno);
         rs=ps.executeQuery();
         if(rs.next())
         {
              
             S.setRno(rs.getInt(1));
             S.setName(rs.getString(2));
             S.setPhy(rs.getInt(3));
             S.setChem(rs.getInt(4));
             S.setMath(rs.getInt(5));
         }
         else
             S=null;
     }
     catch(Exception e)
     {
         System.out.println(e);
     }   
     return S;   
    }
  public boolean deleteByRno(int rno)  
  {
      try
      {     
        Connection con=null;
        PreparedStatement ps=null;
        String sql=null;
        con=mycon.Myconnection.getConnection();
        sql="delete from student where sno=?";
        ps=con.prepareStatement(sql);
        ps.setInt(1,rno);
        if(ps.executeUpdate()>0)
            return true;
      }
      catch(Exception e)
      {
          System.out.println(e);
      }
     return false;   
  }
  public List<Student> searchAll()
  {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        String sql=null;
        con=mycon.Myconnection.getConnection();
        List<Student>mylist=new ArrayList<Student>();
       try
       {
           sql="select * from student";
           ps=con.prepareStatement(sql);
           rs=ps.executeQuery();
           
           while(rs.next())
           {
             Student S=new Student();
             S.setRno(rs.getInt(1));
             S.setName(rs.getString(2));
             S.setPhy(rs.getInt(3));
             S.setChem(rs.getInt(4));
             S.setMath(rs.getInt(5));
             mylist.add(S);
             
             S=null;
           }
          
       }
       catch(Exception e)
       {
           System.out.println(e);
       }
    return mylist;    
  }       
  }

Program 3

/*
 * 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.
 */

package controller;

import dao.StudentDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Student;

/**
 *
 * @author admin
 */
public class LoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LoginServlet</title>");            
            out.println("</head>");
            out.println("<body>");
             String user,pass;
             user=request.getParameter("txtuser");
             pass=request.getParameter("txtpassword");
//             out.println(user);
//             out.println(pass);
             if(StudentDAO.checkLogin(user, pass))
                   response.sendRedirect("HomePage.html");
             else
                   response.sendRedirect("index.html");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

 

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *