MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

时间:2021-01-28 01:34:20

MVC模式(Model View Controller):

Model:DAO模型

View:JSP  在页面上填写java代码实现显示

Controller:Servlet

重定向和请求的转发:

若目标的相应页面不需要从request里面读取任何信息,则可以使用重定向,可以防止表单重复提交;

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

Student.java类。里面封装了许多属性的信息;

package com.lanqiao.javatest;

public class Student {
private Integer id;
private String username;
private String password;
public Student() {
super();
}
public Student(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Student [id=" + id + ", username=" + username + ", password=" + password + "]";
}

}

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

StudentDAO.java类。里面实现数据库的连接,和实现了对数据的查询和删除,可以修改一下实现增删的功能

public class StudentDAO {

//连接数据库并实现删除的方法
public void deleteId(Integer id){

Connection connection=null;
PreparedStatement preparedStatement=null;
String sql="delete from person where id=?";
try {
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql:///test";
String user="root";
String password="lxn123";

Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();

} catch (Exception e) {

}finally {
if (preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

//连接数据库并且实现查询的方法
public List<Student> getAll(){
List<Student> list=new ArrayList<Student>();

//该方法获取连接数据库的方法
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;

String sql="select id,username,password from person";
try {
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql:///test";
String user="root";
String password="lxn123";

Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password1=resultSet.getString(3);
Student student=new Student(id,username,password1);
list.add(student);
}

} catch (Exception e) {
e.printStackTrace();
}
finally {
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
}

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

ListAllStudent.servlet类。实现了,调用上边的StudentDAO类里面的查询方法,设置属性的属性名字,和属性的值:request.setAttribute("student", students);再请求的转发到student.jsp页面,并超链接到test.jsp页面,student.jsp页面中插入java代码,实现查询的功能

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

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

StudentDAO studentDAO=new StudentDAO();

List<Student> students=studentDAO.getAll();

request.setAttribute("student", students);

//请求的转发
request.getRequestDispatcher("/students.jsp").forward(request, response);

}

}

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

student.jsp页面实现数据库数据在页面上显示:

<%@page import="com.lanqiao.javatest.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<Student> list=(List<Student>)request.getAttribute("student");
%>
<table border="1" cellspacing="0" cellpadding="10">
<tr>
<th>Id</th>
<th>userName</th>
<th>password</th>
<th>Delete</th>
</tr>
<%for(Student student:list){%>
<tr>
<td><%=student.getId()%></td>
<td><%=student.getUsername()%></td>
<td><%=student.getPassword()%></td>
<td><a href="deleteStudent?id=<%=student.getId()%>">delete</a></td>
<tr>
<%}%>
</table>
</body>
</html>

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

DeleteStudent.servlet类。通过student.jsp页面的超链接获取查询到的属性的id值,//String id=request.getParameter("id");调用StudentDAO类里面的删除的方法,已经实现的功能,在请求的转发到delete.jsp页面实现删除后的页面显示

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

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

//获取超链接<a href="deleteStudent?id=<%=student.getId()%>">delete</a>里面id的值
String id=request.getParameter("id");
StudentDAO studentDAO=new StudentDAO();

//Integer.parseInt(id)将String类型的转化为Integer类型
studentDAO.deleteId(Integer.parseInt(id));

//请求的转发
request.getRequestDispatcher("/delete.jsp").forward(request, response);

}

}

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

delete.jsp页面实现删除后显示的页面,并且超链接到开始查询的页面,查看删除后的数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>操作成功</h2>
<br><br>
<a href="listAllStudent">List All Student Page</a>
</body>
</html>

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

test.jsp页面实现查询的功能:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="listAllStudent">List All Student Page</a>
</body>
</html>

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

实现功能过程的截图:

页面实现一个链接:

MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

点击链接后显示,查询到的数据库数据:

MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

点击每一个表格后面的删除,实现删除的功能:

MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

点击链接后,查出删除后,剩余的数据

MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

数据库中数据未删除的数据时:

MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

实现功能后是:

MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

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

在lib下面的web.xml文件,里面有两个配置和映射。通过配置和映射,可实现servlet类和jsp之间的关联

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<servlet>
<description></description>
<display-name>ListAllStudent</display-name>
<servlet-name>ListAllStudent</servlet-name>
<servlet-class>com.lanqiao.javatest.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.lanqiao.javatest.DeleteStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteStudent</servlet-name>
<url-pattern>/deleteStudent</url-pattern>
</servlet-mapping>
</web-app>