项目源码 :https://download.csdn.net/download/weixin_44718300/11091042

目录

MVC设计模式

前期准备:

NO01:新建一个index.jsp页面

NO02:在servlet包下创建一个Servlet

NO03:在util包下创建JDBCUtil

NO04:在been包下创建一个Student类

NO05:在dao包下创建一个StudentDao的接口

NO06:在dao.impl包下实现接口

NO07:在service包下创建一个接口

NO08:在service.impl包下实现接口

NO09:编辑StudentListServlet

NO10:新建list.jsp


MVC设计模式

JSP的开发模式

三层架构&MVC练习

前期准备:

  • 1.jar包:

  • 2.c3p0-config.xml配置:

放在src目录下

  • 3.建包

  • 4.准备数据库
  1. CREATE DATABASE stus;
  2. USE stus;
  3. 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>

MVC学生管理系统-阶段I(显示学生列表)的更多相关文章

  1. JDBC学生管理系统--处理分页显示

    分页的思想: 假设一共有104条数据,每页显示10条数据: select * from student limit 0,10; 页数是index,第index页,对应的sql语句是: select * ...

  2. MVC学生管理系统-阶段IV(修改学生信息)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架, 学生列表显示  请看阶段一文章 添加学生信息 ...

  3. MVC学生管理系统-阶段II(添加学生信息)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示    请看上一篇文章 本文是对阶段 ...

  4. MVC学生管理系统-阶段III(删除学生信息)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示  请看阶段一文章 添加学生信息   ...

  5. MVC学生管理系统-阶段V(模糊查询)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 此处省略一段话.去上一篇查看 NO01:修改list.jsp < ...

  6. python开发的学生管理系统

    python开发的学生管理系统(基础版) #定义一个函数,显示可以使用的功能列表给用户 def showInfo(): print("-"*30) print(" 学生管 ...

  7. Winform 学生管理系统增删改查

    数据库: create database adonet go use adonet go create table xue ( code ), name ), sex bit, birth datet ...

  8. python简易版学生管理系统

    #coding=utf- def showInfo(): print("**************") print(" 学生管理系统") print(&quo ...

  9. <每日一题>题目7:简单的学生管理系统V1.0

    ''' # 学生管理系统v1.0 # 添加学生的信息 # 删除学生的信息 # 修改学生的信息 # 查看学生的信息 #遍历学生的信息 #退出系统 ''' import json #1 显示操作功能 de ...

随机推荐

  1. IdentityServer4专题之三:OAuth、SSO和OpenID

    一.oauth 典型案例:如果一个用户R拥有两项服务:一项服务是图片在线存储服务A,另一个是图片在线打印服务B.由于服务A与服务B是由两家不同的服务提供商提供的,所以用户在这两家服务提供商的网站上各自 ...

  2. qrcode在手机上不显示的问题

    可以试试以下解决方案: 1.修改qrcode.min.js:里的function n()红线区域替换成这个 , 原因是这样子才能支持安卓机显示.

  3. Lesson 1 Finding fossil man

    Why are legends handed down by storytellers useful? We can read of things that happend 5000 years ag ...

  4. 【Unity】双击物体

    using UnityEngine; using System.Collections; using System; public class Click_yushe : MonoBehaviour ...

  5. Mybatis注解开发单表CRUD

    Mybatis注解开发单表CRUD mybatis注解开发和xml开发不可兼容,要么全部使用注解,要么全部使用xml,个人建议注解,简单. 当实体类属性名称和数据库表属性名称一致时:无需配置Resul ...

  6. js学习(三)对象与事件

    JavaScript 对象 1.JavaScript 对象:拥有属性和方法的数据. 2.在 JavaScript中,几乎所有的事物都是对象. 3.定义一个person对象 var person = { ...

  7. vue 线上,本地,不同变量配置

    线上的接口和本地的接口不一样,每次打包的时候要手动更改很麻烦.自动让他配置 1.修改package.json  --mode line 传参数line给配置项,编译buildline的时候,就能把li ...

  8. mysql性能优化及 Comparison method violates its general contract

    项目上嵌套结果集查询,查询的列表再根据每个id进行查询计算,嵌套的sql如下: SELECT SUM(IFNULL(t.out_rate,0)) totalOutRate, SUM(IF(IFNULL ...

  9. define可变参数,float数据传输

    define可变参数 一般在调试打印Debug信息的时候, 需要可变参数的宏. 从C99开始可以使编译器标准支持可变参数宏(variadic macros), 另外GCC也支持可变参数宏, 但是两种在 ...

  10. jenkins#安装gitlab

    通过docker安装gitlab 参考 ------------------------------ 拉docker 镜像: docker pull gitlab/gitlab-ce 创建目录存储gi ...