Adding Student View
Adding Student View
Adding Student View
Create a database
https://netbeans.org/kb/docs/web/quickstart-webapps-spring.html
<%--
Document : studentForm
Author : Bossman
--%>
<%@page import="com.kuda.model.Student"%>
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
<%
if(student==null){
student.setStudentId(0);
student.setStudentName("");
student.setStudentAddress("");
}
%>
<h1>Student Form</h1>
</form>
</body>
</html>
………..StudentList View………………
<%--
Document : studentList.jsp
Author : Bossman
--%>
<%@page import="java.util.*"%>
<%@page import="com.kuda.model.Student"%>
<html>
<head>
</head>
<body>
<h1>Student List</h1>
<%
%>
<table>
<tr>
<td>Id</td>
<td>Student Name</td>
<td>Student Address</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<tr>
<td><%= student.getStudentId()%></td>
<td><%= student.getStudentName()%></td>
<td><%= student.getStudentAddress()%></td>
<td><a href="StudentServlet?studentIdForEdit=<%=student.getStudentId()
%>">Edit</a></td>
<td><a href="StudentServlet?studentIdForDelete=<%=student.getStudentId()
%>">Delete</a></td>
</tr>
<%} %>
</table>
</body>
</html>
>……………………………………………………Controller Class…………………………>>>>>>>>>>>>>>>>>>>
package com.kuda.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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.kuda.dao.StudentDao;
import com.kuda.model.Student;
@WebServlet("/StudentServlet")
public StudentServlet() {
super();
doPost(request, response);
try {
if(request.getParameter("studentId")!=null)
student.setStudentId(Integer.parseInt(request.getParameter("studentId")));
student.setStudentName(request.getParameter("studentName"));
student.setStudentAddress(request.getParameter("studentAddress"));
if (request.getParameter("add") != null) {
sd.insertStudent(student);
request.setAttribute("studentList", sd.getStudentList());
sd.updateStudent(student);
request.setAttribute("studentList", sd.getStudentList());
} else if (request.getParameter("studentIdForDelete") != null) {
sd.deleteStudent(Integer.parseInt(request
.getParameter("studentIdForDelete")));
request.setAttribute("studentList", sd.getStudentList());
student = sd.getStudent(Integer.parseInt(request
.getParameter("studentIdForEdit")));
request.setAttribute("student", student);
RequestDispatcher rs = request
.getRequestDispatcher("studentForm.jsp");
rs.forward(request, response);
return;
RequestDispatcher rs = request.getRequestDispatcher("studentList.jsp");
rs.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
>>>…………………………………………………………………………………..>>>>>>>>>>>>>>>>>
* @author Bossman
*/
package com.kuda.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.kuda.model.Student;
import com.kuda.util.DBConnection;
Connection con;
public StudentDao(){
con = DBConnection.getConnection();
try {
pst.setString(1, student.getStudentName());
pst.setString(2, student.getStudentAddress());
pst.execute();
} catch (Exception e) {
e.printStackTrace();
try {
ResultSet rs = pst.executeQuery();
while (rs.next()) {
student.setStudentId(rs.getInt("studentId"));
student.setStudentName(rs.getString("studentName"));
student.setStudentAddress(rs.getString("studentAddress"));
studentList.add(student);
return studentList;
} catch (Exception e) {
e.printStackTrace();
return null;
try {
pst.setInt(1, studentId);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
student.setStudentId(rs.getInt("studentId"));
student.setStudentName(rs.getString("studentName"));
student.setStudentAddress(rs.getString("studentAddress"));
return student;
} catch (Exception e) {
e.printStackTrace();
return null;
try {
pst.setInt(1, studentId);
pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
try {
pst.setString(1, student.getStudentName());
pst.setString(2, student.getStudentAddress());
pst.setInt(3, student.getStudentId());
pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
………………………………………………….Model …………………………………………….
/**
* @author Bossman
*/
package com.kuda.model;
return studentId;
this.studentId = studentId;
return studentName;
this.studentName = studentName;
return studentAddress;
this.studentAddress = studentAddress;
package com.kuda.util;
/**
* @author Bossman
*/
import java.sql.Connection;
import java.sql.DriverManager;
try{
Class.forName("com.mysql.jdbc.Driver");
return con;
}catch(Exception e){
e.printStackTrace();
return null;
-----------------------util. …………………………………………………….
package com.kuda.util;
/**
* @author Bossman
*/
import java.sql.Connection;
import java.sql.DriverManager;
try{
Class.forName("com.mysql.jdbc.Driver");
return con;
}catch(Exception e){
e.printStackTrace();
}
return null;