1、 c3p0-0.9.1.2.jar // 第三方数据库连接池
2、commons-dbutils-1.4.jar // 第三方数据库操作方法封装
3、jstl.jar // jsp中jstl语法引入
4、mysql-connector-java-5.1.7-bin // sql 数据库
public class Student {
private int sid;
private String sname;
private String gender;
private String phone;
private String hobby;
private String info;
private Date birthday;
}
// 分页的学生数据列表
public class PageBean {
private int currentPage; //当前页
private int totalPage;//总页数
private int pageSize;//每页的记录数
private int totalSize; //总的记录数
private List list; //当前页的学生集合
}
public class JDBCUtil {
static ComboPooledDataSource dataSource = null;
static{
dataSource = new ComboPooledDataSource();}
public static DataSource getDataSource(){
return dataSource;}
/**
* 获取连接对象
* @return
* @throws SQLException
*/
public static Connection getConn() throws SQLException{
return dataSource.getConnection();}
/**
* 释放资源
* @param conn
* @param st
* @param rs
*/
public static void release(Connection conn , Statement st , ResultSet rs){
closeRs(rs);
closeSt(st);
closeConn(conn);}
public static void release(Connection conn , Statement st){
closeSt(st);
closeConn(conn);}
private static void closeRs(ResultSet rs){
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}}
private static void closeSt(Statement st){
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null;
}
}
private static void closeConn(Connection conn){
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
/**
* 这是StudentDao的实现。 针对前面定义的规范,做出具体的实现。
* @author xiaomi
*/
public class StudentDaoImpl implements StudentDao {
/**
* 查询所有学生
* @throws SQLException
*/
@Override
public List findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
List query = runner.query("select * from stu", new BeanListHandler(Student.class));
return runner.query("select * from stu", new BeanListHandler(Student.class)); }
@Override
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()
);}
@Override
public void delete(int sid) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
runner.update("delete from stu where sid=?" ,sid);
}
@Override
public Student findStudentById(int sid) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
return runner.query("select * from stu where sid = ?", new BeanHandler(Student.class) ,sid);}
@Override
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());}
@Override
public List 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 list = new ArrayList ();
//判断有没有姓名, 如果有,就组拼到sql语句里面
if(!TextUtils.isEmpty(sname)){
sql = sql + " and sname like ?";
list.add("%"+sname+"%");
}
//判断有没有性别,有的话,就组拼到sql语句里面。
if(!TextUtils.isEmpty(sgender)){
sql = sql + " and gender = ?";
list.add(sgender);
}
Object[] objects = list.toArray();
return runner.query(sql , new BeanListHandler(Student.class) ,list.toArray() );
}
@Override
public List findStudentByPage(int currentPage) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
//第一个问号,代表一页返回多少条记录 , 第二个问号, 跳过前面的多少条记录。
//5 0 --- 第一页 (1-1)*5
//5 5 --- 第二页 (2-1)*5
//5 10 --- 第三页
return runner.query("select * from stu limit ? offset ?",
new BeanListHandler(Student.class) , PAGE_SIZE , (currentPage-1)*PAGE_SIZE);
}
@Override
public int findCount() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
//用于处理 平均值 、 总的个数。
Long result = (Long) runner.query("SELECT COUNT(*) FROM stu" , new ScalarHandler() );
return result.intValue();
}
}
注意与DAO层进行区分
/**
* 这是学生业务实现
* @author xiaomi
*
*/
public class StudentServiceImpl implements StudentService {
@Override
public List findAll() throws SQLException {
StudentDao dao = new StudentDaoImpl();
List all = dao.findAll();
return dao.findAll();}
@Override
public void insert(com.itheima.domain.Student student) throws SQLException {
StudentDao dao = new StudentDaoImpl();
dao.insert(student);}
@Override
public void delete(int sid) throws SQLException {
StudentDao dao = new StudentDaoImpl();
dao.delete(sid);}
@Override
public com.itheima.domain.Student findStudentById(int sid) throws SQLException {
StudentDao dao = new StudentDaoImpl();
return dao.findStudentById(sid);}
@Override
public void update(com.itheima.domain.Student student) throws SQLException {
StudentDao dao = new StudentDaoImpl();
dao.update(student);}
@Override
public List searchStudent(String sname, String sgender) throws SQLException {
return new StudentDaoImpl().searchStudent(sname, sgender);}
@Override
public PageBean findStudentByPage(int currentPage) throws SQLException {
//封装分页的该页数据
PageBean pageBean = new PageBean();
int pageSize = StudentDao.PAGE_SIZE ;
pageBean.setCurrentPage(currentPage); //设置当前页
pageBean.setPageSize(pageSize); //设置每页显示多少记录
StudentDao dao = new StudentDaoImpl() ;
List list =dao.findStudentByPage(currentPage);
pageBean.setList(list); //设置这一页的学生数据
//总的记录数, 总的页数。
int count = dao.findCount();
pageBean.setTotalSize(count); //设置总的记录数
//200 , 10 ==20 201 , 10 = 21 201 % 10 == 0 ?201 / 10 :201 % 10 + 1
pageBean.setTotalPage(count % pageSize==0 ? count / pageSize : (count / pageSize) + 1); //总页数
return pageBean;
}
}
和android的页面注册文件非常类似,对于页面的url需要提前注册。
和
需要一一对应
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
StudentListServlet
StudentListServlet
com.hhh.servlet.StudentListServlet
StudentListServlet
/StudentListServlet
AddServlet
AddServlet
com.hhh.servlet.AddServlet
AddServlet
/AddServlet
DeleteServlet
DeleteServlet
com.hhh.servlet.DeleteServlet
DeleteServlet
/DeleteServlet
EditServlet
EditServlet
com.hhh.servlet.EditServlet
EditServlet
/EditServlet
UpdateServlet
UpdateServlet
com.hhh.servlet.UpdateServlet
UpdateServlet
/UpdateServlet
SearchStudentServlet
SearchStudentServlet
com.hhh.servlet.SearchStudentServlet
SearchStudentServlet
/SearchStudentServlet
StudentListPageServlet
StudentListPageServlet
com.hhh.servlet.StudentListPageServlet
StudentListPageServlet
/StudentListPageServlet
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
显示所有学生列表
分页显示所有学生
对于跳转到jsp进行展示的数据,我们通过request设置属性所对应的对象进行数据传递。
/**
* 这是用于分页显示学生列表的servlet
* @author xiaomi
*/
public class StudentListPageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1. 获取需要显示的页码数
int currentPage =Integer.parseInt( request.getParameter("currentPage"));
//2. 根据指定的页数,去获取该页的数据回来
//List --- list.jsp
StudentService service = new StudentServiceImpl();
PageBean pageBean= service.findStudentByPage(currentPage);
request.setAttribute("pageBean", pageBean);
//3. 跳转界面。
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
对于直接跳转的页面直接通过a标签带参跳转,jsp中我们一般进行少量数据的传递。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: huhanghao
Date: 2019-10-31
Time: 22:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
/**
* 处理单个学生的更新, 查询一个学生的信息,然后跳转到更新页面
* @author xiaomi
*
*/
public class EditServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1. 接收id
int sid = Integer.parseInt(request.getParameter("sid"));
//2. 查询学生数据
StudentService service = new StudentServiceImpl();
Student stu = service.findStudentById(sid);
//3. 显示数据
//存数据
request.setAttribute("stu", stu);
//跳转
request.getRequestDispatcher("edit.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
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" %>
更新学生页面
更新学生页面