MVC学生管理系统-阶段I(显示学生列表)
2024-09-22 02:46:06
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042
目录
MVC设计模式
JSP的开发模式
三层架构&MVC练习
前期准备:
- 1.jar包:
- 2.c3p0-config.xml配置:
放在src目录下
- 3.建包
- 4.准备数据库
- 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) );
NO01:新建一个index.jsp页面
<h3><a href="StudentListServlet">显示所有学生列表</a><h3>
NO02:在servlet包下创建一个Servlet
名字为StudentListServlet,接收请求, 去调用 Service , 由service去调用dao
负查询所有所有学生信息,呈现到list.jsp页面上
我们创建完先不要管他,因为我们先获取数据库中的信息
NO03:在util包下创建JDBCUtil
package com.rick.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtil {
static ComboPooledDataSource dataSource = null;
static{
dataSource = new ComboPooledDataSource();
}
public static DataSource getDataSouce() {
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;
}
}
}
NO04:在been包下创建一个Student类
package com.rick.been;
import java.util.Date;
public class Student {
private int sid;
private String sname;
private String gender;
private String phone;
private Date birthday;
private String hobby;
private String info;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
NO05:在dao包下创建一个StudentDao的接口
package com.rick.dao;
import java.sql.SQLException;
import java.util.List;
import com.rick.been.Student;
/*
* 这是针对学生表的数据访问
*/
public interface StudentDao{
//查询多有学生
List<Student> findAll() throws SQLException;
}
NO06:在dao.impl包下实现接口
package com.rick.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.rick.been.Student;
import com.rick.dao.StudentDao;
import com.rick.util.JDBCUtil;
public class StudentDaoImpl implements StudentDao{
/*
* 抛异常的时候父类没有抛子类不能直接抛,所以去父类先抛一下
*/
@Override
public List<Student> findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());
String sql = "select * from stu";
List<Student> list = runner.query(sql, new BeanListHandler<Student>(Student.class));
return list;
}
}
NO07:在service包下创建一个接口
package com.rick.sercive;
import java.sql.SQLException;
import java.util.List;
import com.rick.been.Student;
/*
* 这是学生的业务处理规范
*/
public interface StudentService {
// 查询多有学生
List<Student> findAll() throws SQLException;
}
NO08:在service.impl包下实现接口
package com.rick.sercive.impl;
import java.sql.SQLException;
import java.util.List;
import com.rick.been.Student;
import com.rick.dao.StudentDao;
import com.rick.dao.impl.StudentDaoImpl;
import com.rick.sercive.StudentService;
/*
* 这是学生的业务实现
*/
public class StudentServiceImpl implements StudentService{
public List<Student> findAll() throws SQLException {
StudentDao dao = new StudentDaoImpl();
return dao.findAll();
}
}
NO09:编辑StudentListServlet
package com.rick.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rick.been.Student;
import com.rick.sercive.StudentService;
import com.rick.sercive.impl.StudentServiceImpl;
/*
* 负查询所有所有学生信息,呈现到list.jsp页面上
*/
public class StudentListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentService service = null;
List<Student> list = null;
try {
//1查出所有学生
service = new StudentServiceImpl();
list = service.findAll();
//2.把数据传到作用于中
request.setAttribute("list", list);
//3跳转
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
NO10:新建list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生列表界面</title>
</head>
<body>
<table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>电话</td>
<td>生日</td>
<td>爱好</td>
<td>简介</td>
<td>操作</td>
</tr>
<c:forEach items = "${list}" var = "stu">
<tr>
<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="#">更新</a> <a href="#">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
最新文章
- Project中分清楚挣值项
- .net中自定义过滤器对Response内容进行处理
- 关于更改MYECLIPSE JS 代码背景颜色
- [drp 8]get和post的区别,以及乱码问题的解决
- Windows服务中用Timer和线程两种方式来执行定时任务
- UVaLive 6809 Spokes Wheel (模拟)
- 剑指 offer set 8 树的子结构
- VIM 拼写/spell check
- Flume FileChannel优化(扩展)实践指南
- RichtextBox打印
- nyoj 43 24 Point game(dfs暴力)
- skynet配置文件
- java中Log4J的使用笔记
- POJ2676 Sudoku [数独]
- NETSH WINSOCK RESET这个命令的意义和效果?
- SQL Server事务的隔离级别和锁
- 极致精简的webservice例子
- Unity服务端链接MySQL数据库
- Linux服务器---百科mediawiki
- ubuntu14.04终端分屏terminator的安装使用与配置