MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作
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>
-------------------------------------------------------------------------------------------------------
实现功能过程的截图:
页面实现一个链接:

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

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

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

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

实现功能后是:

------------------------------------------------------------------------------------------------
在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>
MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作的更多相关文章
- MVC(Model View Controller)框架
MVC框架 同义词 MVC一般指MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...
- 深入浅出Java MVC(Model View Controller) ---- (JSP + servlet + javabean实例)
在DRP中终于接触到了MVC,感触是确实这样的架构系统灵活性不少,现在感触最深的就是使用tomcat作为服务器发布比IIS好多了,起码发布很简单,使用起来方便. 首先来简单的学习一下MVC的基础知识, ...
- Model View Controller (MVC) Overview
By Rakesh Chavda on Jul 01, 2015 What is MVC?Model View Controller is a type of user interface archi ...
- Model View Controller(MVC) in PHP
The model view controller pattern is the most used pattern for today’s world web applications. It ha ...
- What is the difference between Reactjs and Rxjs?--React is the V (View) in MVC (Model/View/Controller).
This is really different, React is view library; and Rxjs is reactive programming library for javasc ...
- ubuntu 下 mysql数据库的搭建 及 数据迁移
1.mysql的安装 我是使用apt-get直接安装的 :sudo apt-get install mysql-server sudo apt-get install mysql-client 2.配 ...
- (转载)Mac系统下利用ADB命令连接android手机并进行文件操作
Mac系统下利用ADB命令连接android手机并进行文件操作 标签: Mac adb android 2016-03-14 10:09 5470人阅读 评论(1) 收藏 举报 分类: Androi ...
- 设计模式 --- 模型-视图-控制器(Model View Controller)
模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程 ...
- MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)
设计功能的实现: ----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象 ----采用MVC设计模式 ----采用到的技术 .MVC设计 ...
随机推荐
- 部署基于JDK的webservice服务类
部署服务端 两个注解(@WebService @WebMethod).一个类(Endpoint) 首先新建JAVA工程ws-server 目录结构如下 在工程里新建一个接口,申明一个方法. packa ...
- 学习OpenCV——Kalman滤波
背景: 卡尔曼滤波是一种高效率的递归滤波器(自回归滤波器), 它能够从一系列的不完全及包含噪声的测量中,估计动态系统的状态.卡尔曼滤波的一个典型实例是从一组有限的,包含噪声的,对物体位置的观察序列(可 ...
- Redis分布式
昨天公司技术大牛做了一个Redis分布式的技术分享: Redis分布式资源: http://redis.io/topics/cluster-tutorialhttp://redis.io/topics ...
- Sublime 不自动打开上次未关闭的文件 设置方法
{ "font_size": 17, "hot_exit": false, "remember_open_files": false, &q ...
- MFC主窗口架构模型
根据主窗口类型,MFC软件工程可以分为一下几种架构模型: 1.SDI(Simple Document Interface)单文档界面,一个主窗口下只编辑一份文档 2.MDI(Multiple Docu ...
- 。。。Hibernate注解配置的注意事项。。。
今天本来打算录视频的,突然遇到一个拦路虎,Hibernate注解配置,有一个注意点:要么都在属性上面注解配置,要么都在getXX()方法上面用注解配置,要不然就会报错: Caused by: org. ...
- java post请求
package com.jfbank.loan.intf.util; import java.io.IOException;import java.util.ArrayList;import java ...
- Linux centOS7 下安装mysql5.7.10
1:下载二进制安装包 http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz 2:解压到 ...
- Trace Sys
ARM片上调试和跟踪解决方案(包括CoreSight体系结构,嵌入式跟踪宏单元(ETM),程序流程跟踪(PTM),ARM调试接口(ADI), 跟踪缓冲器(ETB),嵌入式交叉触发器(CTM)) Cor ...
- Bishop的大作《模式识别与机器学习》Ready to read!
久仰Bishop的大作“Pattern Recognition and Machine Learning”已久,在我的硬盘里已经驻扎一年有余,怎奈惧其页数浩瀚,始终未敢入手.近日看文献,屡屡引用之.不 ...