实体类
package com.stage3.haungzixiao.exam1.entity;
public class Player {
private Integer id; //玩家ID
private String name; //玩家名称
private Integer level; //玩家等级
private String game;//游戏名称
private String time;//注册时间
private Integer status;//账号状态 0封号 1正常
public Player() {
}
public Player(String name, Integer level, String game, String time, Integer status) {
this.name = name;
this.level = level;
this.game = game;
this.time = time;
this.status = status;
}
public Player(Integer id, String name, Integer level, String game, String time, Integer status) {
this.id = id;
this.name = name;
this.level = level;
this.game = game;
this.time = time;
this.status = status;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public String getGame() {
return game;
}
public void setGame(String game) {
this.game = game;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Player{" +
"id=" + id +
", name='" + name + '\'' +
", level=" + level +
", game='" + game + '\'' +
", time='" + time + '\'' +
", status=" + status +
'}';
}
}
package com.stage3.haungzixiao.exam1.dao;
import com.stage3.haungzixiao.exam1.entity.Player;
import com.stage3.haungzixiao.exam1.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class PlayerDao {
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
/**
* 按照添加查询
* @param game 游戏名
* @param name 玩家姓名
* @param startTime 起始时间
* @param endTime 结束时间
* @param status 状态
* @return
*/
public List<Player> getPlayerList(String game,String name,String startTime,String endTime,Integer status){
String sql = "select id,name,level,game,`time`,status from tgame where 1=1 ";
List param = new ArrayList();
if(game!=null){
sql += " and game=? ";
param.add(game);
}
if(name!=null){
sql += " and name=? ";
param.add(name);
}
if(startTime!=null){
sql += "and `time`>? ";
param.add(startTime);
}
if(endTime!=null){
sql += " and `time`<? ";
param.add(endTime);
}
if(status!=null){
sql += " and status=? ";
param.add(status);
}
try {
return queryRunner.query(sql,new BeanListHandler<>(Player.class),param.toArray());
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取所有游戏名称
* @return
*/
public List<Player> getGame(){
String sql = "select distinct game from tgame";
try {
return queryRunner.query(sql,new BeanListHandler<>(Player.class));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 添加玩家
* 时间自动获取当前系统时间
* @param player
* @return
*/
public int addPlayer(Player player){
String sql = "insert into tgame(name,level,game,`time`,status) values(?,?,?,?,?)";
try {
return queryRunner.update(sql,player.getName(),player.getLevel(),player.getGame(),player.getTime(),player.getStatus());
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
/**
* 根据id查询一个玩家
* @param id
* @return
*/
public Player getPlayerById(Integer id){
String sql = "select id,name,level,game,`time`,status from tgame where id=?";
try {
return queryRunner.query(sql,new BeanHandler<>(Player.class),id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 修改玩家
* @param player
* @return
*/
public int updatePlay(Player player){
String sql = "update tgame set name=?,level=?,game=?,`time`=?,status=? where id=?";
try {
return queryRunner.update(sql,player.getName(),player.getLevel(),player.getGame(),player.getTime(),player.getStatus(),player.getId());
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public int deletePlayer(Integer id){
String sql = "delete from tgame where id=?";
try {
return queryRunner.update(sql,id);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}
package com.stage3.haungzixiao.exam1.service;
import com.stage3.haungzixiao.exam1.dao.PlayerDao;
import com.stage3.haungzixiao.exam1.entity.Player;
import java.util.List;
public class PlayerService {
/**
* 按照条件查询
*
* @return
*/
public List<Player> getPlayerList(String game, String name, String startTime, String endTime, Integer status) {
PlayerDao playerDao = new PlayerDao();
return playerDao.getPlayerList(game, name, startTime, endTime, status);
}
public List<Player> getGame() {
PlayerDao playerDao = new PlayerDao();
return playerDao.getGame();
}
public int addPlay(Player player) {
PlayerDao playerDao = new PlayerDao();
return playerDao.addPlayer(player);
}
public Player getPlayerById(Integer id) {
PlayerDao playerDao = new PlayerDao();
return playerDao.getPlayerById(id);
}
public int updatePlay(Player player) {
PlayerDao playerDao = new PlayerDao();
return playerDao.updatePlay(player);
}
public int deletePlayer(Integer id) {
PlayerDao playerDao = new PlayerDao();
return playerDao.deletePlayer(id);
}
}
package com.stage3.haungzixiao.exam1.servlet;
import com.stage3.haungzixiao.exam1.entity.Player;
import com.stage3.haungzixiao.exam1.service.PlayerService;
import com.stage3.haungzixiao.exam1.util.ParameterUtil;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet(name = "PlayerListServlet", urlPatterns = "/playerList")
public class PlayerListServlet extends HttpServlet {
private PlayerService playerService = new PlayerService();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String game = ParameterUtil.getString(request,"game");
String name = ParameterUtil.getString(request,"name");
String startTime = ParameterUtil.getString(request,"startTime");
String endTime = ParameterUtil.getString(request,"endTime");
Integer status = ParameterUtil.getInteger(request,"status");
List<Player> playerList=null;
List<Player> gameName=null;
System.out.println(game+name+startTime+endTime+status);
playerList = playerService.getPlayerList(game,name,startTime,endTime,status);
// for (Player player : playerList) {
// System.out.println(player);
// }
gameName = playerService.getGame();
System.out.println(gameName);
request.setAttribute("playerList",playerList);
request.setAttribute("gameName",gameName);
request.getRequestDispatcher("playerList.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.stage3.haungzixiao.exam1.servlet;
import com.stage3.haungzixiao.exam1.entity.Player;
import com.stage3.haungzixiao.exam1.service.PlayerService;
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 java.io.IOException;
import java.util.List;
@WebServlet(name = "GameNameServlet", urlPatterns = "/toadd")
public class GameNameServlet extends HttpServlet {
private PlayerService playerService = new PlayerService();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Player> gameName = playerService.getGame();
request.setAttribute("gameName",gameName);
request.getRequestDispatcher("addPlayer.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.stage3.haungzixiao.exam1.servlet;
import com.stage3.haungzixiao.exam1.entity.Player;
import com.stage3.haungzixiao.exam1.service.PlayerService;
import com.stage3.haungzixiao.exam1.util.ParameterUtil;
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 java.io.IOException;
import java.util.List;
@WebServlet(name = "ToUpdatePlayerServlet", urlPatterns = "/toupdate")
public class ToUpdatePlayerServlet extends HttpServlet {
private PlayerService playerService = new PlayerService();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = ParameterUtil.getInteger(request,"id");
Player player = playerService.getPlayerById(id);
List<Player> gameName = playerService.getGame();
request.setAttribute("player",player);
request.setAttribute("gameName",gameName);
request.getRequestDispatcher("updatePlayer.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.stage3.haungzixiao.exam1.servlet;
import com.stage3.haungzixiao.exam1.entity.Player;
import com.stage3.haungzixiao.exam1.service.PlayerService;
import com.stage3.haungzixiao.exam1.util.ParameterUtil;
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 java.io.IOException;
@WebServlet(name = "UpdatePlayerServlet", urlPatterns = "/update")
public class UpdatePlayerServlet extends HttpServlet {
private PlayerService playerService = new PlayerService();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("更新");
Integer id = ParameterUtil.getInteger(request,"id");
String name = ParameterUtil.getString(request,"name");
String game = ParameterUtil.getString(request,"game");
Integer level = ParameterUtil.getInteger(request,"level");
String time = ParameterUtil.getString(request,"time");
Integer status = ParameterUtil.getInteger(request,"status");
Player player = new Player(id,name,level,game,time,status);
System.out.println(player);
int res = playerService.updatePlay(player);
response.sendRedirect("playerList");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.stage3.haungzixiao.exam1.servlet;
import com.stage3.haungzixiao.exam1.service.PlayerService;
import com.stage3.haungzixiao.exam1.util.ParameterUtil;
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 java.io.IOException;
@WebServlet(name = "DeletePlayerServlet", urlPatterns = "/delete")
public class DeletePlayerServlet extends HttpServlet {
private PlayerService playerService = new PlayerService();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = ParameterUtil.getInteger(request,"id");
int res = playerService.deletePlayer(id);
response.sendRedirect("playerList");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package com.stage3.haungzixiao.exam1.servlet;
import com.stage3.haungzixiao.exam1.entity.Player;
import com.stage3.haungzixiao.exam1.service.PlayerService;
import com.stage3.haungzixiao.exam1.util.ParameterUtil;
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 java.io.IOException;
import java.lang.reflect.Parameter;
import java.util.List;
@WebServlet(name = "AddPlayerServlet", urlPatterns = "/add")
public class AddPlayerServlet extends HttpServlet {
private PlayerService playerService = new PlayerService();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
String game = ParameterUtil.getString(request,"game");
String name = ParameterUtil.getString(request,"name");
Integer level = ParameterUtil.getInteger(request,"level");
String time = ParameterUtil.getString(request,"time");
Integer status = ParameterUtil.getInteger(request,"status");
System.out.println(name+level+game+time+status);
Player player = new Player(name,level,game,time,status);
int res = playerService.addPlay(player);
if(res>0){
response.getWriter().write("添加成功");
}else {
response.getWriter().write("添加失败");
}
response.sendRedirect("playerList");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
前端
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>玩家列表</title>
<link rel="stylesheet" href="css/bootstrap.min.css"></link>
<link rel="stylesheet" href="css/bootstrap-datetimepicker.css"></link>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="jquery-1.10.2.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="bootstrap-datetimepicker.min.js" charset="UTF-8"></script>
</head>
<body>
<div class="row">
<div class="col-lg-6">
<form action="playerList" method="post" class="form-inline" id="searchForm">
<div class="form-group">
<label>游戏</label>
<select class="form-control" name="game">
<option value="">请选择</option>
<c:forEach items="${gameName}" var="game">
<option value="${game.game}">${game.game}</option>
</c:forEach>
</select>
</div>
<div class="form-group">
<label>角色名</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group span6">
<label>注册时间</label>
<input class="form-control" size="10" type="text" id="startTime" name="startTime"> -
<input class="form-control" size="10" type="text" id="endTime" name="endTime">
</div>
<div class="form-group">
<label>状态</label>
<select class="form-control" name="status">
<option value="">请选择</option>
<option value="1">正常</option>
<option value="0">封号</option>
</select>
</div>
<div class="form-group">
<button type="submit" class="btn">查询</button> <a href="toadd"><button type="button" class="btn btn-success">+添加</button></a>
</div>
</form>
<table class="table table-striped table-bordered" style="margin-top:20px">
<tr>
<td>角色ID</td>
<td>角色名</td>
<td>角色等级</td>
<td>所属游戏</td>
<td>注册日期</td>
<td>状态</td>
<td>操作</td>
</tr>
<c:forEach items="${playerList}" var="player">
<tr>
<td>${player.id}</td>
<td>${player.name}</td>
<td>${player.level}</td>
<td>${player.game}</td>
<td>${player.time}</td>
<c:if test="${player.status==1}">
<td>正常</td>
</c:if>
<c:if test="${player.status==0}">
<td>封号</td>
</c:if>
<td><a href="toupdate?id=${player.id}">修改</a> <a href="delete?id=${player.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<ul class="pagination">
<!--不可点击的样式-->
<li class="disabled"><a href="#">首页</a></li>
<li><a href="#">上一页</a></li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<!--当前页-->
<li class="active"><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">下一页</a></li>
<li><a href="#">末页</a></li>
</ul>
</div>
</div>
<script>
$(function () {
$('#startTime').datetimepicker({
format: 'yyyy-mm-dd',
autoclose: true,
minView: 2
});
$('#endTime').datetimepicker({
format: 'yyyy-mm-dd',
autoclose: true,
minView: 2
});
})
</script>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>更新玩家</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/bootstrap.min.css"></link>
<link rel="stylesheet" href="css/bootstrap-datetimepicker.css"></link>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="jquery-1.10.2.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="bootstrap-datetimepicker.min.js" charset="UTF-8"></script>
</head>
<body>
<div class="row">
<div class="col-lg-4">
<form action="update" method="post" id="registForm" class="form-horizontal" >
<input type="hidden" name="id" value="${player.id}">
<div class="form-group">
<label class="col-sm-2 control-label">游戏:</label>
<div class="col-sm-10">
<select class="form-control" name="game">
<option value="">请选择</option>
<c:forEach items="${gameName}" var="game">
<c:if test="${player.game==game.game}">
<option value="${game.game}" selected>${game.game}</option>
</c:if>
<option value="${game.game}">${game.game}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">角色名:</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${player.name}" name="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">角色等级:</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${player.level}" name="level">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">注册日期:</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="${player.time}" readonly name="time">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">状态:</label>
<div class="col-sm-10">
<c:if test="${player.status==1}">
<label class="control-label"><input type="radio" name="status" value="1" checked>正常</label>
<label class="control-label"><input type="radio" name="status" value="0">禁用</label>
</c:if>
<c:if test="${player.status==0}">
<label class="control-label"><input type="radio" name="status" value="1">正常</label>
<label class="control-label"><input type="radio" name="status" value="0" checked>禁用</label>
</c:if>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-2">
<button type="submit" id="btn" class="btn btn-success">提交</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Calendar" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>添加玩家</title>
<link rel="stylesheet" href="css/bootstrap.min.css"></link>
<link rel="stylesheet" href="css/bootstrap-datetimepicker.css"></link>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="jquery-1.10.2.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="bootstrap-datetimepicker.min.js" charset="UTF-8"></script>
</head>
<body>
<div class="row">
<div class="col-lg-4">
<form method="post" id="registForm" class="form-horizontal" action="add">
<div class="form-group">
<label class="col-sm-2 control-label">游戏:</label>
<div class="col-sm-10">
<select class="form-control" name="game">
<option value="">请选择</option>
<c:forEach items="${gameName}" var="game">
<option value="${game.game}">${game.game}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">角色名:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">角色等级:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="level">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">注册日期:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="time" value="<%=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime())%>" readonly>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">状态:</label>
<div class="col-sm-10">
<label class="control-label"><input type="radio" name="status" value="1" checked>正常</label>
<label class="control-label"><input type="radio" name="status" value="0">禁用</label>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-2">
<button type="submit" id="btn" class="btn btn-success">提交</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>