您的位置:首页 > Web前端 > JavaScript

MVC小例子--Servlet/Jdbc/Jsp交互--查询、删除学生列表

2016-08-23 01:05 387 查看


在开始学习mvc之前,我们先来写一个小例子,来系统的梳理一下逻辑关系

1、进入一个jsp页面,页面上有一个链接,是访问学生表的



2、点击链接后会出现学生表



3、最后一列可以删除每一条学生记录

我们先来看看简单的创建的数据库,学生表(students)



首先就是ListAllStudent.jsp吧,这个就是访问学生表的入库jsp,很简单只有一个链接,跳转到ListAllStudent.servlet

<a href="ListAllStudent">ListAllStudent</a>


然后是ListAllStudent,是去jdbc链接数据库,获取结果集,获取集合后,在转发给students.jsp去页面展现,我们需要setAttribute传递一个ArrayList同学的集合

ListAllStudent

package com.safly;

import java.io.IOException;
import java.util.ArrayList;

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

public class ListAllStudent extends HttpServlet {
private static final long serialVersionUID = 1L;

public ListAllStudent() {
super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentManager manager = new StudentManager();
ArrayList<Student> students = manager.getAllStudents();
System.out.println("students.size:"+students.size());
//转发
request.setAttribute("students", students);
request.getRequestDispatcher("/students.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}


然后是students.jsp获取传递过来的值,进行页面展现

<%
ArrayList<Student> students = (ArrayList<Student>)request.getAttribute("students");
%>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>FlowId</th>
<th>Type</th>
<th>IdCard</th>
<th>ExamCard</th>
<th>StudentName</th>
<th>Location</th>
<th>Grade</th>
<th>Delete</th>
</tr>
<% for(Student student: students){ %>
<tr>
<td><%= student.getFlow_id() %></td>
<td><%= student.getType() %></td>
<td><%= student.getId_card() %></td>
<td><%= student.getExam_card() %></td>
<td><%= student.getExam_card() %></td>
<td><%= student.getLocation() %></td>
<td><%= student.getGrade() %></td>
<td><a href="DeleteStudent?flowId=<%=student.getFlow_id()%>">Delete</a></td>
</tr>
<%} %>
</table>


ArrayList students = (ArrayList)request.getAttribute(“students”);通过jsp内置对象request调用getAttribute方法获取传递过来的集合,然后在拼接html字段,这里我们主要看下最后一列

href=”DeleteStudent?flowId=<%=student.getFlow_id()%>

我们删除某条学生记录,肯定是需要去数据库删除的,这里我们就默认flow_id是唯一的值,就传过去,进行数据库删除即可

在DeleteStudent进行接受传递过来的flow_id进行数据库删除,最后删除完毕后,给出一个success成功界面提示即可

DeleteStudent

package com.safly;

import java.io.IOException;

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

public class DeleteStudent extends HttpServlet {
private static final long serialVersionUID = 1L;

public DeleteStudent() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String deleFlowId = request.getParameter("flowId");
System.out.println("deleFlowId:"+deleFlowId);

StudentManager manager = new StudentManager();
int deleStuByFlowId = manager.deleStuByFlowId(deleFlowId);
if (deleStuByFlowId == 1) {
request.getRequestDispatcher("/success.jsp").forward(request, response);
}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}


success.jsp删除成功页面很简单

success!!!
<a href="ListAllStudent">List All Students</a>


最后贴出StudentManager方法,对数据库进行操作的类

package com.safly;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentManager {
java.sql.Connection connection = null;
java.sql.PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

public StudentManager() {
// 链接数据库

try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/mydatabase";
String user = "root";
String password = "root";
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public ArrayList<Student> getAllStudents() {
ArrayList<Student> students = new ArrayList<Student>();

String sql = "select flow_id, type, id_card, exam_card, stu_name, location, grade from students";
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
if (resultSet != null) {
while (resultSet.next()) {
int flowId = resultSet.getInt(1);
int type = resultSet.getInt(2);
String idCard = resultSet.getString(3);
String examCard = resultSet.getString(4);
String studentName = resultSet.getString(5);
String localtion = resultSet.getString(6);
int grade = resultSet.getInt(7);

System.out.println("flowId:"+flowId+",type:"+type+",idCard:"+idCard+",examCard:"
+examCard+",studentName:"+studentName+",localtion:"+localtion+",grade:"+grade);

Student student = new Student(flowId, type, idCard,
examCard, studentName, localtion, grade);
students.add(student);

}
}

} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}

}

return students;

}

public  int deleStuByFlowId(String deleFlowId) {

int parseFlowId = Integer.parseInt(deleFlowId);
int executeUpdate = 0;
try {
String sql = "delete from students where flow_id=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, parseFlowId);
executeUpdate = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}

}
return executeUpdate;

}

}


最后还有自动生成的web.xml

<servlet>
<servlet-name>ListAllStudent</servlet-name>
<servlet-class>com.safly.ListAllStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListAllStudent</servlet-name>
<url-pattern>/ListAllStudent</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DeleteStudent</display-name>
<servlet-name>DeleteStudent</servlet-name>
<servlet-class>com.safly.DeleteStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteStudent</servlet-name>
<url-pattern>/DeleteStudent</url-pattern>
</servlet-mapping>


以下是几步操作的截图:







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐