Day8作业 课程信息管理

1Class文件

Course.class:

package course.model;

public class Course {
	private int id = -1;
	private String name;
	private String teacher;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTeacher() {
		return teacher;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	
}

2 jsp文件

cour_list.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import ="java.util.*,course.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>cour_list</title>
</head>
<body>
	<form action="${request.contextPath}/StuCour/CourListServlet" method="post">
	id:<input type = "text" name="id" <c:if test="${cour.id ne -1}">value="${cour.id}"</c:if>> <br>
	name:<input type="text" name="name" value="${cour.name}"><br>
	teacher:<input type = "text" name="teacher" value="${cour.teacher}"><br>
	<input type="submit" value="search"> 
	<input type="reset">
	<br>
	</form>

		<table border="1">
			<tr>
				<th>No.</th>
				<th>name</th>
				<th>teacher</th>
				<th>operation1</th>
				<th>operation2</th>
			</tr>
			<c:forEach items="${requestScope.cours}" var = "cour">
				<tr> 
					<td>${cour.id}</td>
					<td>${cour.name}</td>
					<td>${cour.teacher}</td>
					<td><a href="${request.contextPath}/StuCour/CourDelServlet?id=${cour.id}">delete</a></td>
					<td><a href="${request.contextPath}/StuCour/CourUpdServlet?id=${cour.id}">update</a></td>
				</tr>
			</c:forEach>
		</table>
		<a href="${request.contextPath}/StuCour/CourAddServlet">insert</a>
</body>
</html>

cour_add.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import ="java.util.*,course.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>cour_add</title>
</head>
<body>
		<form action="${request.contextPath}/StuCour/CourAddServlet" method="post">
		id:<input type="text" name="id"><br/>
		name:<input type="text" name="name"><br/>
		teacher:<input type="text" name="teacher"><br/>
		<input type="submit" value="submit">
		<input type="reset" value="reset">
		</form>
</body>
</html>

cour_upd.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import ="java.util.*,course.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>cour_upd</title>
</head>
<body>
		<form action="${request.contextPath}/StuCour/CourUpdServlet" method="post">
		<input type="hidden" name="id" value="${cour.id}">
		name:<input type="text" name="name" value="${cour.name}"><br/>
		teacher:<input type="text" name="teacher" value="${cour.teacher}"><br/>
		<input type="submit" value="submit">
		<input type="reset" value="reset">
		</form>
</body>
</html>

3 Servlet文件

CourListServlet:

package course.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import course.model.Course;
import course.service.CourService;
import course.service.imp.CourServiceImp;

/**
 * Servlet implementation class CourListServlet
 */
@WebServlet("/CourListServlet")
public class CourListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ArrayList <Course> cours= new ArrayList();
		CourService courService = new CourServiceImp();
		cours = (ArrayList<Course>)courService.listAll();
		request.setAttribute("cours", cours);
		request.getRequestDispatcher("course/cour_list.jsp").forward(request, response);
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		String id =  request.getParameter("id");
		String name = request.getParameter("name");
		String teacher = request.getParameter("teacher");
		Course cour = new Course();
		if(id!=null&&!id.equals(""))
		{
			cour.setId(Integer.parseInt(id));
		}
		if(name!=null&&!name.equals(""))
		{
			cour.setName(name);
		}
		if(teacher!=null&&!teacher.equals(""))
		{
			cour.setTeacher(teacher);
		}
		ArrayList <Course> cours = new ArrayList();
		CourService courService = new CourServiceImp();
		cours = courService.list(cour);
		request.setAttribute("cour", cour);
		request.setAttribute("cours", cours);
		request.getRequestDispatcher("course/cour_list.jsp").forward(request, response);
		
	}

}

CourDelServlet:

package course.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import course.service.CourService;
import course.service.imp.CourServiceImp;

/**
 * Servlet implementation class CourDelServlet
 */
@WebServlet("/CourDelServlet")
public class CourDelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		CourService courService = new CourServiceImp();
		courService.delete(Integer.parseInt(id));
		///重定向页面出错,不应该转到jsp文件去
		response.sendRedirect("CourListServlet");
		
	}

}

CourAddServlet:

package course.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import course.service.CourService;
import course.service.imp.CourServiceImp;

/**
 * Servlet implementation class CourDelServlet
 */
@WebServlet("/CourDelServlet")
public class CourDelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		CourService courService = new CourServiceImp();
		courService.delete(Integer.parseInt(id));
		///重定向页面出错,不应该转到jsp文件去
		response.sendRedirect("CourListServlet");
		
	}

}

CourUpdServlet:

package course.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import course.model.Course;
import course.service.CourService;
import course.service.imp.CourServiceImp;

/**
 * Servlet implementation class CourUpdServlet
 */
@WebServlet("/CourUpdServlet")
public class CourUpdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CourUpdServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		Course cour = new Course();
		CourService courService = new CourServiceImp();
		cour = courService.listById(Integer.parseInt(id));
		request.setAttribute("cour", cour);
		request.getRequestDispatcher("course/cour_upd.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String teacher = request.getParameter("teacher");
		Course cour = new Course();
		cour.setId(Integer.parseInt(id));
		cour.setName(name);
		cour.setTeacher(teacher);
		CourService courService = new CourServiceImp();
		courService.update(cour);
		response.sendRedirect("CourListServlet");
	}

}

4 Service文件

CourService:

package course.service;

import course.model.*;
import java.util.*;

public interface CourService {
	//查询全部课程
	public List <Course> listAll();
	//按id查询
	public Course listById(int id);
	//条件查询
	public ArrayList <Course> list(Course cour);
	//添加
	public boolean save(Course cour);
	//删除
	public boolean delete(int id);
	//修改
	public boolean update(Course cour);
}

CourServiceImp:

package course.service.imp;

import java.util.*;

import course.dao.CourDao;
import course.dao.imp.CourDaoImp;
import course.model.Course;
import course.service.CourService;

public class CourServiceImp implements CourService {

	CourDao courDao = new CourDaoImp(); 
	@Override
	public List<Course> listAll() {
		// TODO Auto-generated method stub
		return courDao.listAll();
	}

	@Override
	public Course listById(int id) {
		// TODO Auto-generated method stub
		return courDao.listById(id);
	}

	@Override
	public ArrayList<Course> list(Course cour) {
		// TODO Auto-generated method stub
		return courDao.list(cour);
	}

	@Override
	public boolean save(Course cour) {
		// TODO Auto-generated method stub
		return courDao.save(cour);
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		return courDao.delete(id);
	}

	@Override
	public boolean update(Course cour) {
		// TODO Auto-generated method stub
		return courDao.update(cour);
	}

}

5 Dao文件

CourDao:

package course.dao;

import java.util.*;

import course.model.Course;

public interface CourDao {
	//查询全部课程
	public List <Course> listAll();
	//按id查询
	public Course listById(int id);
	//条件查询
	public ArrayList <Course> list(Course cour);
	//添加
	public boolean save(Course cour);
	//删除
	public boolean delete(int id);
	//修改
	public boolean update(Course cour);
}

CourDaoImp:

package course.dao.imp;

import java.sql.*;
import java.util.*;

import org.eclipse.jdt.internal.compiler.parser.RecoveredStatement;

import course.dao.CourDao;
import course.model.Course;

public class CourDaoImp implements CourDao{

	@Override
	public List<Course> listAll(){
		ArrayList <Course> cours = new ArrayList();
		Connection con =null;
		// TODO Auto-generated method stub
		try {

		Class.forName("com.mysql.jdbc.Driver");
		con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stucour","root","");
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery("select * from course;");
		while(rs.next()) {
			Course cour = new Course();
			cour.setId(rs.getInt("id"));
			cour.setName(rs.getString("name"));
			cour.setTeacher(rs.getString("teacher"));
			cours.add(cour);
		}
		
		}catch(Exception e) {
			e.printStackTrace();
		}
		try {
		con.close();
		}catch(Exception e1)
		{
			e1.printStackTrace();
		}
		return cours;
		
	}

	@Override
	public Course listById(int id) {
		// TODO Auto-generated method stub
		Connection con = null;
		Course cour = new Course();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stucour","root","");
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from course where id="+id);
			rs.next();
			cour.setId(id);
			cour.setName(rs.getString("name"));
			cour.setTeacher(rs.getString("teacher"));
			return cour;
		}catch(Exception e) {
			e.printStackTrace();
			return cour;
		}finally {
			try {
				con.close();
			}catch(Exception e1)
			{
				e1.printStackTrace();
			}

		}
	}

	@Override
	public ArrayList<Course> list(Course cour) {
		// TODO Auto-generated method stub
		Connection con = null;
		ArrayList cours =new ArrayList();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stucour","root","");
			Statement st = con.createStatement();
			String sql = "select * from course where 1=1";
			if(cour.getId()!=-1)
			{
				sql = sql + " and id="+cour.getId();
			}
			if(cour.getName()!=null)
			{
				sql = sql + " and name like '%"+cour.getName()+"%'";
			}
			if(cour.getTeacher()!=null)
			{
				sql = sql +" and teacher like '%" +cour.getTeacher()+ "%'";
			}
			ResultSet rs = st.executeQuery(sql);
			while(rs.next())
			{
				Course cour1 = new Course();
				cour1.setId(rs.getInt("id"));
				cour1.setName(rs.getString("name"));
				cour1.setTeacher(rs.getString("teacher"));
				cours.add(cour1);
			}
			return cours;
		}catch(Exception e){
			e.printStackTrace();
			return cours;
		}finally {
			try {
				con.close();
			}catch(Exception e1)
			{
				e1.printStackTrace();
			}

		}
	}

	@Override
	public boolean save(Course cour) {
		// TODO Auto-generated method stub
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stucour","root","");
			String sql = "insert into course values(?,?,?)";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1,cour.getId());
			pst.setString(2, cour.getName());
			pst.setString(3,cour.getTeacher());
			pst.execute();
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally {
			try {
			con.close();
			}catch(Exception e1){
				e1.printStackTrace();
			}
		}
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stucour", "root", "");
			Statement st = con.createStatement();
			return st.execute("delete from course where id="+id);
		}catch(Exception e) {
			e.printStackTrace();
			return false;
			}finally
		{
				try {
					con.close();
				}catch(Exception e1) {
					e1.printStackTrace();
				}
		}
	}

	@Override
	public boolean update(Course cour) {
		// TODO Auto-generated method stub
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/stucour","root","");
			String sql = "update course set name=?,teacher=? where id =?";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1,cour.getName());
			pst.setString(2, cour.getTeacher());
			pst.setInt(3,cour.getId());
			return pst.execute();
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}finally {
			try {
				con.close();
			}catch(Exception e1)
			{
				e1.printStackTrace();
			}
		}
	}

}

6.运行结果

7.Debug注意事项

        在jsp文件中,利用El表达式时,应注意变量名大小写问题,应保持一致大写或一致小写,不可大小写同时存在,如下图的cour.id,如果在Course类中设置属性为Id,则下语句变为cour.Id,会导致接收数据失败,无论是否传入数据都会报错。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值