package com.neu.entity;
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Dept() {
super();
// TODO 自动生成的构造函数存根
}
public Dept(Integer deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((deptno == null) ? 0 : deptno.hashCode());
result = prime * result + ((dname == null) ? 0 : dname.hashCode());
result = prime * result + ((loc == null) ? 0 : loc.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dept other = (Dept) obj;
if (deptno == null) {
if (other.deptno != null)
return false;
} else if (!deptno.equals(other.deptno))
return false;
if (dname == null) {
if (other.dname != null)
return false;
} else if (!dname.equals(other.dname))
return false;
if (loc == null) {
if (other.loc != null)
return false;
} else if (!loc.equals(other.loc))
return false;
return true;
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
}
#DeptDa.java
package com.neu.dao;
import java.util.List;
import com.neu.entity.Dept;
public interface DeptDao {
List<Dept> getAll() throws Exception;
int getInsert(Dept dept) throws Exception;
int getUpdate(Dept dept) throws Exception;
int getDelete(int deptno) throws Exception;
Dept getById(int deptno) throws Exception;
List<Dept> search(String dname) throws Exception;
}
#DeptDaoImpl.java
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neu.entity.Dept;
public class DeptDaoImpl implements DeptDao {
@Override
public List<Dept> getAll() throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from dept";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
List<Dept> list = new ArrayList<>();
Dept dept = null;
int deptno;
String dname;
String loc;
while(rs.next()) {
deptno=rs.getInt("deptno");
dname=rs.getString("dname");
loc=rs.getString("loc");
dept = new Dept(deptno, dname, loc);
list.add(dept);
}
JDBCUtil.closeConnection(connection);
return list;
}
@Override
public int getInsert(Dept dept) throws Exception {
String sql = "insert into dept values (?,?,?)";
int n = JDBCUtil.executeUpdate(sql, new Object[] {dept.getDeptno(),dept.getDname(),dept.getLoc()});
return n;
}
@Override
public Dept getById(int deptno) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from dept where deptno = ?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {deptno});
Dept dept = null;
String dname;
String loc;
if(rs.next()) {
dname=rs.getString("dname");
loc=rs.getString("loc");
dept = new Dept(deptno, dname, loc);
}
JDBCUtil.closeConnection(connection);
return dept;
}
@Override
public int getUpdate(Dept dept) throws Exception {
String sql = "update dept set dname = ?,loc = ? where deptno = ?";
int n = JDBCUtil.executeUpdate(sql, new Object[] {dept.getDname(),dept.getLoc(),dept.getDeptno()});
return n;
}
@Override
public int getDelete(int deptno) throws Exception {
String sql = "delete from dept where deptno = ?";
int n = JDBCUtil.executeUpdate(sql, new Object[] {deptno});
return n;
}
@Override
public List<Dept> search(String dname) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from dept where dname = ?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {dname});
List<Dept> list = new ArrayList<>();
Dept dept = null;
int deptno;
String loc;
while(rs.next()) {
deptno=rs.getInt("deptno");
loc=rs.getString("loc");
dept = new Dept(deptno, dname, loc);
list.add(dept);
}
JDBCUtil.closeConnection(connection);
return list;
}
}
#DeptService.java
package com.neu.service;
import java.util.List;
import com.neu.entity.Dept;
public interface DeptService {
List<Dept> getAll() throws Exception;
int getInsert(Dept dept) throws Exception;
Dept getById(int deptno) throws Exception;
int getUpdate(Dept dept) throws Exception;
int getDelete(int deptno) throws Exception;
List<Dept> search(String dname) throws Exception;
}
#DeptServiceImpl.java
package com.neu.service;
import java.util.List;
import com.neu.dao.DeptDao;
import com.neu.dao.DeptDaoImpl;
import com.neu.entity.Dept;
public class DeptServiceImpl implements DeptService {
private DeptDao deptDao = new DeptDaoImpl();
@Override
public List<Dept> getAll() throws Exception {
// TODO 自动生成的方法存根
return deptDao.getAll();
}
@Override
public int getInsert(Dept dept) throws Exception {
// TODO 自动生成的方法存根
return deptDao.getInsert(dept);
}
@Override
public Dept getById(int deptno) throws Exception {
// TODO 自动生成的方法存根
return deptDao.getById(deptno);
}
@Override
public int getUpdate(Dept dept) throws Exception {
// TODO 自动生成的方法存根
return deptDao.getUpdate(dept);
}
@Override
public int getDelete(int deptno) throws Exception {
// TODO 自动生成的方法存根
return deptDao.getDelete(deptno);
}
@Override
public List<Dept> search(String dname) throws Exception {
// TODO 自动生成的方法存根
return deptDao.search(dname);
}
}
#DeptServlet.java
package com.neu.servlet;
import java.io.IOException;
import java.util.List;
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 com.neu.entity.Dept;
import com.neu.service.DeptService;
import com.neu.service.DeptServiceImpl;
import net.sf.json.JSONArray;
import sun.awt.RepaintArea;
/**
* Servlet implementation class DeptServlet
*/
@WebServlet("/DeptServlet")
public class DeptServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
if ("add".equals(method)) {
doAdd(request, response);
} else if ("delete".equals(method)) {
doDel(request, response);
} else if ("update".equals(method)) {
doUpd(request, response);
} else if ("getAll".equals(method)) {
doGetAll(request, response);
} else if ("search".equals(method)) {
doSearch(request, response);
}
}
private void doSearch(HttpServletRequest request, HttpServletResponse response) {
DeptService deptService = new DeptServiceImpl();
String dname = request.getParameter("dname");
try {
List<Dept> deptList = deptService.search(dname);
String jsonArray = JSONArray.fromObject(deptList).toString();
response.getWriter().print(jsonArray);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
private void doGetAll(HttpServletRequest request, HttpServletResponse response) {
DeptService deptService = new DeptServiceImpl();
try {
List<Dept> list = deptService.getAll();
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/jsp/dept/dept.jsp").forward(request, response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
private void doUpd(HttpServletRequest request, HttpServletResponse response) throws IOException {
Integer deptno = Integer.parseInt(request.getParameter("deptno"));
String dname = request.getParameter("dname");
String loc = request.getParameter("loc");
Dept dept = new Dept(deptno, dname, loc);
DeptService deptService = new DeptServiceImpl();
int result;
try {
result = deptService.getUpdate(dept);
} catch (Exception e) {
result = 0;
e.printStackTrace();
}
response.getWriter().print(result);
}
private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
Integer deptno = Integer.parseInt(request.getParameter("deptno"));
DeptService deptService = new DeptServiceImpl();
int result;
try {
result = deptService.getDelete(deptno);
} catch (Exception e) {
result = 0;
e.printStackTrace();
}
response.getWriter().print(result);
}
private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
Integer deptno = Integer.parseInt(request.getParameter("deptno"));
String dname = request.getParameter("dname");
String loc = request.getParameter("loc");
Dept dept = new Dept(deptno, dname, loc);
DeptService deptService = new DeptServiceImpl();
int result;
try {
result = deptService.getInsert(dept);
} catch (Exception e) {
result = 0;
e.printStackTrace();
}
response.getWriter().print(result);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
#dept.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>Insert title here</title>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-3.2.1.js"></script>
<script type="text/javascript">
$(function(){
//删除部门信息
$(document).on("click","a:contains(删除)",function(){
if(confirm("是否删除?")){
var url = "${pageContext.request.contextPath }/DeptServlet?method=delete";
var param = "deptno="+$(this).data("deptno");
var t=this;
$.get(url,param,function(data){
if(data=="1"){
$(t).parent().parent().remove();
}else{
alert("删除失败");
}
});
}
});
//编辑部门信息
$(document).on("click","a:contains(编辑)",function(){
$(this).text("保存").parent().parent().children().each(function(index){
if(index<3){
var text = $(this).text();
$(this).empty();
if(index==0){
$("").attr("readonly","true").val(text).appendTo($(this));
}else if(index==1){
$("").val(text).appendTo($(this));
}else if(index==2){
$("").val(text).appendTo($(this));
}
}
});
});
//保存编辑后的信息
$(document).on("click","a:contains(保存)",function(){
var url = "${pageContext.request.contextPath }/DeptServlet?method=update";
var param = $("form:eq(1)").serialize();
var t = this;
$.get(url,param,function(data){
if(data=="1"){
$(t).text("编辑").parent().parent().children().each(function(index){
if(index<3){
var val = $(this).children().eq(0).val();
$(this).empty();
$(this).text(val);
}
});
}else{
alert("保存失败");
}
});
});
//添加新部门
$(":button[value=add]").click(function(){
var url = "/jQueryDemo3/DeptServlet?method=add";
var param = $("form").serialize();
$.get(url,param,function(data){
if(data=="1"){
var tr = $("");
tr.appendTo("#tab");
$("").text($("[name=deptno]").val()).appendTo(tr);
$(" ").text($("[name=dname]").val()).appendTo(tr);
$(" ").text($("[name=loc]").val()).appendTo(tr);
var del = $("").attr("href","#").data("deptno",$("[name=deptno]").val()).text("删除");
$(" ").append(del).appendTo(tr);
var upd = $("").attr("href","#").text("编辑");
$(" ").append(upd).appendTo(tr);
$("[name=deptno]").val("");
$("[name=dname]").val("");
$("[name=loc]").val("");
}else{
alert("添加失败");
}
});
});
//条件查询
$(":button[value=search]").click(function(){
var url = "${pageContext.request.contextPath}/DeptServlet?method=search";
var param = $("[name=dname]").eq(0).serialize();
$.post(url,param,function(data){
var jsonArray = eval("("+data+")");
//清空表格数据保留首行
$("#tab").children().children().not(":eq(0)").remove();
$(jsonArray).each(function(){
var tr = $("").appendTo("#tab");
$("").text(this.deptno).appendTo(tr);
$(" ").text(this.dname).appendTo(tr);
$(" ").text(this.loc).appendTo(tr);
var del = $("").attr("href","#").text("删除");
$(" ").append(del).appendTo(tr);
var upd = $("").attr("href","#").text("编辑");
$(" ").append(upd).appendTo(tr);
});
});
});
});
</script>
</head>
<body>
<form>
<table style="text-align: center;">
<caption>添加部门表</caption>
<tr>
<td>部门编号<input type="text" name="deptno"></td>
<td>部门名称<input type="text" name="dname"></td>
<td>部门地址<input type="text" name="loc"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="button" value="add">
<input type="button" value="search">(*根据部门名称查询)</td>
</tr>
</table>
</form>
<hr>
<form>
<table width="500" id="tab" border="1">
<tr>
<th>部门编号</th>
<th>部门名称</th>
<th>部门地址</th>
<th>删除</th>
<th>编辑</th>
</tr>
<c:forEach items="${ list }" var="dept">
<tr>
<td>${ dept.deptno }</td>
<td>${ dept.dname }</td>
<td>${ dept.loc }</td>
<td><a href="#" data-deptno="${ dept.deptno }">删除</a></td>
<!-- html5中允许添加数据属性 data-属性名 -->
<td><a href="#">编辑</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
你可能感兴趣的:(jQuery ajax 部门(dept)表操作)