元数据(Meata data )
描述数据的数据 String sql , 描述这份sql字符串的数据叫做元数据
数据库元数据 DatabaseMetaData
参数元数据 ParameterMetaData
结果集元数据 ResultSetMetaData
MVC设计模式
JSP的开发模式
三层架构&MVC练习
学生信息管理系统
数据库准备
CREATE DATABASE stus;
USE stus;
CREATE TABLE stu (
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR (20),
gender VARCHAR (5),
phone VARCHAR (20),
birthday DATE,
hobby VARCHAR(50),
info VARCHAR(200)
);
查询
- 先写一个JSP 页面, 里面放一个超链接 。
<a href="StudentListServlet"> 学生列表显示</a>
-
写Servlet, 接收请求, 去调用 Service , 由service去调用dao
-
先写Dao , 做Dao实现。
public interface StudentDao {
/**
* 查询所有学生
* @return List<Student>
*/
List<Student> findAll() throws SQLException ;
}
---------------------------------------------
public class StudentDaoImpl implements StudentDao {
/**
* 查询所有学生
* @throws SQLException
*/
@Override
public List<Student> findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
return runner.query("select * from stu", new BeanListHandler<Student>(Student.class));
}
}
- 再Service , 做Service的实现。
/**
* 这是学生的业务处理规范
* @author xiaomi
*
*/
public interface StudentService {
/**
* 查询所有学生
* @return List<Student>
*/
List<Student> findAll() throws SQLException ;
}
------------------------------------------
/**
* 这是学生业务实现
* @author xiaomi
*
*/
public class StudentServiceImpl implements StudentService{
@Override
public List<Student> findAll() throws SQLException {
StudentDao dao = new StudentDaoImpl();
return dao.findAll();
}
}
- 在servlet 存储数据,并且做出页面响应。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1. 查询出来所有的学生
StudentService service = new StudentServiceImpl();
List<Student> list = service.findAll();
//2. 先把数据存储到作用域中
request.setAttribute("list", list);
//3. 跳转页面
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
-
在list.jsp上显示数据
EL + JSTL + 表格
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>学生列表界面</title>
<script type="text/javascript">
function doDelete(sid) {
/* 如果这里弹出的对话框,用户点击的是确定,就马上去请求Servlet;
如何知道用户点击的是确定
如何在js的方法中请求servlet */
var flag = confirm("是否确定删除?");
if(flag){
//表明点了确定。 访问servlet。 在当前标签页上打开 超链接,
//location.href="DeleteServlet?sid="+sid;
window.location.href="DeleteServlet?sid="+sid;
}
}
</script>
</head>
<body>
<form action="SearchStudentServlet" method="Post">
<table border="1" width="800">
<tr >
<td colspan="8">
按姓名查询:<input type="text" name="sname" >
按性别查询:
<select name="sgender">
<option value="">--请选择--</option>
<option value="男">男</option>
<option value="女">女</option>
<option value="阴阳人">阴阳人</option>
</select>
<input type="submit" value=查询>
<a href="add.jsp">添加</a>
</td>
</tr>
<tr align="center">
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>电话</td>
<td>生日</td>
<td>爱好</td>
<td>简介</td>
<td>操作</td>
</tr>
<c:forEach items="${list }" var="stu">
<tr align="center">
<td>${stu.sid }</td>
<td>${stu.sname }</td>
<td>${stu.gender }</td>
<td>${stu.phone }</td>
<td>${stu.birthday }</td>
<td>${stu.hobby }</td>
<td>${stu.info }</td>
<td><a href="EditStudentServlet?sid=${stu.sid }">更新</a> | <a href="#" onclick="doDelete(${stu.sid})">删除</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
增加
-
先跳转到增加的页面 , 编写增加的页面
-
点击添加,提交数据到AddServlet . 处理数据。
<%@ 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>
<body>
<h3>添加学生页面</h3>
<form action="AddStudentServlet" method="Post">
<table border="1" width="700">
<tr>
<td>姓名</td>
<td><input type="text" name="sname" /></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="gender" value="男" />男
<input type="radio" name="gender" value="女" />女
<input type="radio" name="gender" value="阴阳人" />阴阳人
</td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td>生日</td>
<td><input type="text" name="birthday" /></td>
</tr>
<tr>
<td>爱好</td>
<td>
<input type="checkbox" name="hobby" value="游泳" />游泳
<input type="checkbox" name="hobby" value="篮球" />篮球
<input type="checkbox" name="hobby" value="足球" />足球
<input type="checkbox" name="hobby" value="看书" />看书
<input type="checkbox" name="hobby" value="写字" />写字
</td>
</tr>
<tr>
<td>简介</td>
<td><textarea name="info" rows="3" cols="20" ></textarea></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="添加" /> </td>
</tr>
</table>
</form>
</body>
</html>
- 调用service
package service.Inte;
import java.sql.SQLException;
import java.util.List;
import entity.PageBean;
import entity.Student;
/**
* @author wanghaichaun
*这是学生的业务处理规范
*/
public interface StudentServiceInte {
/**
* 查询所有学生
* @return List<Student>
*/
List<Student> findAll() throws SQLException;
/**
* 模糊查询(姓名 性别)
* @return List<Student>
*/
List<Student> searchStudent(String sname,String sgender) throws SQLException;
/**
* 根据ID查询单个学生对象
* @param sid
* @return
* @throws SQLException
*/
Student findStudentById(int sid) throws SQLException ;
/**
* 添加学生
* @param student 需要添加到数据库的学生对象
* @throws SQLException
*/
void insert(Student student) throws SQLException;
/**
* 根据id删除学生
* @param student 需要添加到数据库的学生对象
* @throws SQLException
*/
void Detele(int sid) throws SQLException;
/**
* 更新学生信息
* @throws SQLException
*/
void Update(Student student) throws SQLException;
/**
* 查询当页的学生数据
* @throws SQLException
*/
PageBean findStudentByPage(int currentPage) throws SQLException;
}
调用service的实现
package service.Impl;
import java.sql.SQLException;
import java.util.List;
import com.wangshi.dao.Impl.StudentDaoImpl;
import com.wangshi.dao.Inte.StudentDao;
import entity.PageBean;
import entity.Student;
import service.Inte.StudentServiceInte;
/**
* 这是学生业务实现
* @author wanghaichuan
*
*/
public class StudentServiceImpl implements StudentServiceInte {
StudentDao sd = new StudentDaoImpl();
/**
*查找
*/
public List<Student> findAll() throws SQLException {
List<Student> list = sd.findAll();
return list;
}
/**
* 添加
*/
public void insert(Student student) throws SQLException {
sd.insert(student);
}
public void Detele(int sid) throws SQLException {
sd.Detele(sid);
}
/*通过id查找数据*/
public Student findStudentById(int sid) throws SQLException {
Student student = sd.findStudentById(sid);
return student;
}
//修改
public void Update(Student student) throws SQLException {
sd.Update(student);
}
/**
* 模糊查询
* @throws SQLException
*/
public List<Student> searchStudent(String sname, String sgender) throws SQLException {
List<Student> list = sd.searchStudent(sname, sgender);
return list;
}
/**
* 查询当页的学生数据
* @throws SQLException
*/
public PageBean findStudentByPage(int currentPage) throws SQLException {
//封装分页的该页数据
PageBean<Student> pageBean = new PageBean<Student>();
//该页的 学生数据
List<Student> list = sd.findStudentByPage(currentPage);
pageBean.setList(list);
//每页的记录数
int pageSize =StudentDao.PAGE_SIZE;
pageBean.setPageSize(pageSize);
//设置当前页
pageBean.setCurrentPage(currentPage);
//总的记录数, 总的页数。
int count = sd.findCount();
pageBean.setTotalSize(count);
//总的页数
//200 , 10 ==20 201 , 10 = 21 201 % 10 == 0 ?201 / 10 :201 % 10 + 1
// 总的页数 % 每页的记录数 ==0 ? 总的页数 / 每页的记录数 : 总的页数 /每页的记录数+1
pageBean.setTotalPage(count%pageSize==0 ? count/pageSize : (count/pageSize)+1);
return pageBean;
}
}
}
- 调用dao, 完成数据持久化。
package com.wangshi.dao.Inte;
import java.sql.SQLException;
import java.util.List;
import entity.Student;
/**
* @author wanghaichaun
*这是针对学生表的数据访问
*/
public interface StudentDao {
//一页显示多少条记录
int PAGE_SIZE=5;
/**
* 查询所有学生
* @return List<Student>
*/
List <Student>findAll() throws SQLException;
/**
* 模糊查询(姓名 性别)
* @return List<Student>
*/
List<Student> searchStudent(String sname,String sgender) throws SQLException;
/**
* 根据ID查询单个学生对象
* @param sid
* @return
* @throws SQLException
*/
Student findStudentById(int sid) throws SQLException ;
/**
* 添加学生
* @param student 需要添加到数据库的学生对象
* @throws SQLException
*/
void insert(Student student) throws SQLException;
/**
* 根据id删除学生
* @throws SQLException
*/
void Detele(int sid) throws SQLException;
/**
* 更新学生信息
* @throws SQLException
*/
void Update(Student student) throws SQLException;
/**
* 查询当页的学生数据
* @throws SQLException
*/
List<Student> findStudentByPage(int currentPage) throws SQLException;
/**
* 查询总的学生记录数
* @return
* @throws SQLException
*/
int findCount()throws SQLException;
}
dao层的实现
package com.wangshi.dao.Impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.wangshi.dao.Inte.StudentDao;
import entity.Student;
import util.JDBCUtil;
import util.TestUtil;
/**
* @author wanghaichuan
*这是StudentDao的实现。 针对前面定义的规范,做出具体的实现。
*/
public class StudentDaoImpl implements StudentDao{
/**
* 查询所有学生
* @return List<Student>
* @throws SQLException
*/
public List<Student> findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
List<Student> list = runner.query("select * from stu", new BeanListHandler<Student>(Student.class));
return list;
}
/**
* 添加
*/
public void insert(Student student) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
runner.update("insert into stu values(null,?,?,?,?,?,?)",student.getSname(),
student.getGender(),student.getPhone(),
student.getBirthday(),student.getHobby(),student.getInfo());
}
/*删除*/
public void Detele(int sid) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
runner.update("delete from stu where sid=?",sid);
}
/**
* 根据id查单个学生的信息
*/
public Student findStudentById(int sid) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
Student student = runner.query("select * from stu where sid = ?",new BeanHandler<Student>(Student.class) ,sid);
return student;
}
public void Update(Student student) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
runner.update("update stu set sname = ?,gender=?,phone=?,birthday=?,hobby = ?,info=? where sid = ?",student.getSname(),
student.getGender(),student.getPhone(),
student.getBirthday(),student.getHobby(),student.getInfo(),student.getSid());
}
/**
* 模糊查询(根据学生姓名 性别)
*/
public List<Student> searchStudent(String sname, String sgender) throws SQLException {
System.out.println("现在要执行模糊查询了,收到的name ="+sname + "==genser=="+sgender);
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
//String sql = "select * from stu where sname=? or sgender=?";
/*
* 这里要分析一下:
* 如果只有姓名 ,select * from stu where sname like ? ;
* 如果只有性别 , select * from stu where gender = ?
*
* 如果两个都有 select * from stu where sname like ? and gender=?
*
* 如果两个都没有就查询所有。
*
*/
String sql = "select * from stu where 1=1 ";
List<String> list = new ArrayList<String>();
//判断有没有姓名, 如果有,就组拼到sql语句里面
if(!TestUtil.isEmpty(sname)){
sql = sql + " and sname like ?";
list.add("%"+sname+"%");
}
//判断有没有性别,有的话,就组拼到sql语句里面。
if(!TestUtil.isEmpty(sgender)){
sql = sql + " and gender = ?";
list.add(sgender);
}
//集合变数组
List<Student> list1 = runner.query(sql, new BeanListHandler<Student>(Student.class),list.toArray());
return list1;
}
/**
* 查询当页的学生数据
* @throws SQLException
*/
public List<Student> findStudentByPage(int currentPage) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
//第一个问号,代表一页返回多少条记录 , 第二个问号, 跳过前面的多少条记录。
//5 0 --- 第一页 (1-1)*5
//5 5 --- 第二页 (2-1)*5
//5 10 --- 第三页 (当前页-1)*每页的记录数
return runner.query("select * from stu limit ? offset ?", new BeanListHandler<Student>(Student.class),PAGE_SIZE, (currentPage-1)*PAGE_SIZE);
}
/**
* 查询总的学生记录数
* @return
* @throws SQLException
*/
public int findCount() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
//new ScalarHandler() 用于处理 平均值 、 总的个数。
Long result = (Long) runner.query("select count(*) from stu", new ScalarHandler());
return result.intValue();
}
}
- 完成了这些存储工作后,需要跳转到列表页面。 这里不能直接跳转到列表页面,否则没有什么内容显示。 应该先跳转到查询所有学生信息的那个Servlet, 由那个Servlet再去跳转到列表页面。
package com.wangshi.servlet;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Student;
import service.Impl.StudentServiceImpl;
import service.Inte.StudentServiceInte;
/**
* 用于处理学生的添加请求
* @author wanghaichuan
*/
public class AddStudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编码
request.setCharacterEncoding("utf-8");
try {
System.out.println("123");
//1. 获取客户端提交上来的数据
String sname = request.getParameter("sname");
System.out.println("456456"+sname);
String gender = request.getParameter("gender");
String phone = request.getParameter("phone");
String birthday = request.getParameter("birthday");
//只能显示一个
//String hobby = request.getParameter("hobby");
String[] h = request.getParameterValues("hobby");
//[篮球,足球,写字] --- 篮球,足球,写字
String hobby = Arrays.toString(h);
hobby = hobby.substring(1,hobby.length()-1);
String info = request.getParameter("info");
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
System.out.println("456456");
//2.添加到数据库
//string -- date
Student student = new Student(sname, gender, phone, hobby, info, date);
StudentServiceInte ss = new StudentServiceImpl();
ss.insert(student);
System.out.println("789789");
//3. 跳转到列表页
//再查一次数据库,然后再装到作用域中 ,然后再跳转。
//这里是直接跳转到页面上, 那么这个页面会重新翻译一次,上面的那个request的请求存放的数据是没有了。
//request.getRequestDispatcher("list.jsp").forward(request, response);
//servlet除了能跳jsp之外。 还能跳servlet
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编码
request.setCharacterEncoding("utf-8");
try {
System.out.println("123");
//1. 获取客户端提交上来的数据
String sname = request.getParameter("sname");
System.out.println("456456"+sname);
String gender = request.getParameter("gender");
String phone = request.getParameter("phone");
String birthday = request.getParameter("birthday");
//只能显示一个
//String hobby = request.getParameter("hobby");
String[] h = request.getParameterValues("hobby");
//[篮球,足球,写字] --- 篮球,足球,写字
String hobby = Arrays.toString(h);
hobby = hobby.substring(1,hobby.length()-1);
String info = request.getParameter("info");
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
System.out.println("456456");
//2.添加到数据库
//string -- date
Student student = new Student(sname, gender, phone, hobby, info, date);
StudentServiceInte ss = new StudentServiceImpl();
ss.insert(student);
System.out.println("789789");
//3. 跳转到列表页
//再查一次数据库,然后再装到作用域中 ,然后再跳转。
//这里是直接跳转到页面上, 那么这个页面会重新翻译一次,上面的那个request的请求存放的数据是没有了。
//request.getRequestDispatcher("list.jsp").forward(request, response);
//servlet除了能跳jsp之外。 还能跳servlet
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 爱好的value 值有多个。
request.getParameter(“hobby”);
String[] hobby = request.getParameterValues(“hobby”) —> String[]
String value = Arrays.toString(hobby): // [爱好, 篮球, 足球]
删除
-
点击超链接,弹出一个询问是否删除的对话框,如果点击了确定,那么就真的删除。
<a href="#" onclick="doDelete(${stu.sid})">删除</a>
-
让超链接,执行一个js方法
<script type="text/javascript">
function doDelete(sid) {
/* 如果这里弹出的对话框,用户点击的是确定,就马上去请求Servlet。
如何知道用户点击的是确定。
如何在js的方法中请求servlet。 */
var flag = confirm("是否确定删除?");
if(flag){
//表明点了确定。 访问servlet。 在当前标签页上打开 超链接,
//window.location.href="DeleteServlet?sid="+sid;
location.href="DeleteServlet?sid="+sid;
}
}
</script>
- 在js访问里面判断点击的选项,然后跳转到servlet。
package com.wangshi.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.Impl.StudentServiceImpl;
import service.Inte.StudentServiceInte;
/**删除
* @author wanghaichaun
*
*/
public class DeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//获取id
int sid = Integer.parseInt(request.getParameter("sid"));
System.out.println("sid="+sid);
//2.执行删除
StudentServiceInte ss = new StudentServiceImpl();
ss.Detele(sid);
//3.跳转界面
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- servlet收到了请求,然后去调用service , service去调用dao
更新
- 点击列表上的更新, 先跳转到一个EditServlet
在这个Servlet里面,先根据ID 去查询这个学生的所有信息出来。
package com.wangshi.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Student;
import service.Impl.StudentServiceImpl;
import service.Inte.StudentServiceInte;
/**更新
* @author wanghaichuan
*处理单个学生的更新,查询一个学生的信息,然后跳转到更新界面
*/
public class EditStudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1. 接收sid
int sid = Integer.parseInt(request.getParameter("sid"));
System.out.println("sid="+sid);
//2. 查询学生数据
StudentServiceInte ss = new StudentServiceImpl();
Student student = ss.findStudentById(sid);
System.out.println("student:"+student);
//3. 显示数据
//存数据
request.setAttribute("stu", student);
//跳转
request.getRequestDispatcher("edit.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- 跳转到更新的页面。 ,然后在页面上显示数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!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>更新学生页面</title>
</head>
<body>
<h3>更新学生页面</h3>
<form method="Post" action="UpdateStudentServlet">
<input type="hidden" name="sid" value="${stu.sid }">
<table border="1" width="600">
<tr>
<td>姓名</td>
<td><input type="text" name="sname" value="${stu.sname }"></td>
</tr>
<tr>
<td>性别</td>
<td>
<!-- 如果性别是男的, 可以在男的性别 input标签里面, 出现checked ,
如果性别是男的, 可以在女的性别 input标签里面,出现checked -->
<input type="radio" name="gender" value="男" <c:if test="${stu.gender == '男'}">checked</c:if>>男
<input type="radio" name="gender" value="女" <c:if test="${stu.gender == '女'}">checked</c:if>>女
<input type="radio" name="gender" value="女" <c:if test="${stu.gender == '阴阳人'}">checked</c:if>>阴阳人
</td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone" value="${stu.phone }"></td>
</tr>
<tr>
<td>生日</td>
<td><input type="text" name="birthday" value="${stu.birthday }"></td>
</tr>
<tr>
<td>爱好</td>
<td>
<!-- 爱好: 篮球 , 足球 , 看书
因为爱好有很多个, 里面存在包含的关系 <c:if test="${fn:contains('元字符串','对应的字符串') }"></c:if>-->
<input type="checkbox" name="hobby" value="游泳" <c:if test="${fn:contains(stu.hobby,'游泳') }">checked</c:if>>游泳
<input type="checkbox" name="hobby" value="篮球" <c:if test="${fn:contains(stu.hobby,'篮球') }">checked</c:if>>篮球
<input type="checkbox" name="hobby" value="足球" <c:if test="${fn:contains(stu.hobby,'足球') }">checked</c:if>>足球
<input type="checkbox" name="hobby" value="看书" <c:if test="${fn:contains(stu.hobby,'看书') }">checked</c:if>>看书
<input type="checkbox" name="hobby" value="写字" <c:if test="${fn:contains(stu.hobby,'写字') }">checked</c:if>>写字
</td>
</tr>
<tr>
<td>简介</td>
<td><textarea name="info" rows="3" cols="20">${stu.info }</textarea></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="更新"> </td>
</tr>
</table>
</form>
</body>
</html>
- 修改完毕后,提交数据到UpdateServlet
提交上来的数据是没有带id的,所以我们要手动创建一个隐藏的输入框, 在这里面给定id的值, 以便提交表单,带上id。
<form method="post" action="UpdateServlet">
<input type="hidden" name="sid" value="${stu.sid }">
...
</form>
- 获取数据,调用service, 调用dao.【具体代码请看上述】
分页功能
-
物理分页 (真分页)
来数据库查询的时候,只查一页的数据就返回了。优点: 内存中的数据量不会太大
缺点:对数据库的访问频繁了一点。
SELECT * FROM stu LIMIT 5 OFFSET 2 -
逻辑分页 (假分页)
一口气把所有的数据全部查询出来,然后放置在内存中。优点: 访问速度快。
缺点: 数据库量过大,内存溢出。
--------------------------------------------------------代码都在文章中☻☻☻