项目源码 :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. DeprecationWarning:'open()' is deprecated in mongoose>=4.11.0,use 'openUri()' instead or set the 'useMongoClient' option if using 'connect()' or 'createConnection'

    mongoose.connect('mongodb://localhost/test');报错:(node:2752) DeprecationWarning: `open()` is deprecat ...

  2. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

  3. html css3

    一.引入样式 1.行内样式表 <h1 style="color: red;font-size: 18px;">10-30</h1> 2.内部样式表(在hea ...

  4. java表单基础

    一.表单  基本语法:   <form method="表单提交方式(post/get)" action="表单提交地址">       </ ...

  5. 数据结构 c++ 广义表

    // CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  6. 第3节 sqoop:6、sqoop的数据增量导入和数据导出

    增量导入 在实际工作当中,数据的导入,很多时候都是只需要导入增量数据即可,并不需要将表中的数据全部导入到hive或者hdfs当中去,肯定会出现重复的数据的状况,所以我们一般都是选用一些字段进行增量的导 ...

  7. 014、Java中byte自动转型的操作

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  8. 012.CI4框架CodeIgniter, 加载并调用自己的Libraries库

    01. 在Libraries目录创建一个Mylib文件,内容是一个简单的类 <?php namespace App\Controllers; class Home extends BaseCon ...

  9. 0108 spring的申明式事务

    背景 互联网的金融和电商行业,最关注数据库事务. 业务核心 说明 金融行业-金融产品金额 不允许发生错误 电商行业-商品交易金额,商品库存 不允许发生错误 面临的难点: 高并发下保证: 数据一致性,高 ...

  10. mysql 视图入门