jQuery,ajax,mysql部门表操作

jQuery,ajax,mysql部门表操作

demo8.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
</style>
<script type="text/javascript" src="js/jquery-3.2.1.js"></script>

<script>			
			$(function(){	
				/* $(document).on({
					"mouseover":function(){
						$(this).css("color","antiquewhite");
					},
					"mouseout":function(){
						$(this).css("color","black");
					}
				},"p"); */
				
				/* $(document).off("mouseout mouseover","p"); */
				
				$(document).on("click","a:contains(删除)",function(){
					if(confirm('是否删除?')){
						//$(this).parent().parent().remove();
						//发送ajax请求,给远程的服务器
						var url = "/jQuerydemo1/DeptServlet?method=del";
						var param = "deptno="+$(this).parent().parent().children().eq(0).text();
						var o = this;
						$.get(url,param,function(data){
							if(data == "1"){
								$(o).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){
								$("<input type='text' name='deptno' size='3'>").prop("readonly",true).val(text).appendTo($(this));	
							}else if(index==1){
								$("<input type='text' name='dname' size='3'>").val(text).appendTo($(this));
							}else if(index==2){
								$("<input type='text' name='loc' size='3'>").val(text).appendTo($(this));
							}							
						}
					});
				});
				
				$(document).on("click","a:contains(保存)",function(){
					var url="/jQuerydemo1/DeptServlet?method=update";
					var param = $("form:eq(1)").serialize();
					var o = this;
					$.get(url,param,function(data){
						if(data=="1"){
							$(o).text("编辑").parent().parent().children().each(function(index){
								if(index < 3){
									var text = $(this).children().eq(0).val();
									
									$(this).empty();
									
									$(this).text(text);							
								}
							});
						}else{
							alert("保存失败");
						}
					});
					
					
				});
				
				$(":button").click(function(){
					
					//发送ajax请求,给远程的服务器
					var url = "/jQuerydemo1/DeptServlet?method=add";
					var param = $("form").serialize();
					
					$.get(url,param,function(data){
						//服务器发回响应结果,然后,如果服务器添加成功,则执行下面的操作
						if(data == "1"){
							var tr = $("<tr>");
							tr.appendTo("table");
							
							$("<td>").text($("[name=deptno]").val()).appendTo(tr);
							$("<td>").text($("[name=dname]").val()).appendTo(tr);
							$("<td>").text($("[name=loc]").val()).appendTo(tr);
							
							var a = $("<a>").attr("href","#").text("删除");
							
							$("<td>").append(a).appendTo(tr);
							
							var edit = $("<a>").attr("href","#").text("编辑");
							$("<td>").append(edit).appendTo(tr);
							
							$("[name=deptno]").val("");
							$("[name=dname]").val("");
							$("[name=loc]").val("");
						}else{
							alert("添加失败!");
						}
						
					});
				});
			});
		</script>
</head>
<body>
	<form>
		<div style="text-align: center;">
			<p>添加部门</p>
			<p>
				deptno:<input type="text" name="deptno"> dname:<input
					type="text" name="dname"> loc:<input type="text" name="loc">
			</p>
			<p>
				<input type="button" value="add">
			</p>
		</div>
	</form>
	<hr>
	<form action="">
		<table border="1" width="300" align="center">
			<tr>
				<th>部门编号</th>
				<th>部门名称</th>
				<th>部门地址</th>
				<th>删除</th>
				<th>编辑</th>
			</tr>
		</table>
	</form>
</body>
</html>

服务器 DeptServlet

package com.neu.servlet;

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 com.neu.entity.Dept;
import com.neu.service.DeptService;
import com.neu.service.DeptServiceImpl;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	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("del".equals(method)) {
			doDel(request,response);
		}else if("update".equals(method)) {
			doUpdate(request,response);
		}
		
	}

	private void doUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException{	
		DeptService deptService = new DeptServiceImpl();
		
		Integer deptno = Integer.parseInt(request.getParameter("deptno"));
		String dname = request.getParameter("dname");
		String loc = request.getParameter("loc");
		
		Dept dept = new Dept(deptno, dname, loc); 
		int result;
		try {
			result = deptService.update(dept);
		} catch (Exception e) {
			result = 0;
			e.printStackTrace();
		}
		response.getWriter().print(result);
		
		
	}

	private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		DeptService deptService = new DeptServiceImpl();
		Integer deptno = Integer.parseInt(request.getParameter("deptno"));
		int result;
		try {
			result=deptService.delete(deptno);
		} catch (Exception e) {
			result=0;
			e.printStackTrace();
		}
		response.getWriter().print(result);
	}

	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		DeptService deptService = new DeptServiceImpl();
		
		Integer deptno = Integer.parseInt(request.getParameter("deptno"));
		String dname = request.getParameter("dname");
		String loc = request.getParameter("loc");
		
		Dept dept = new Dept(deptno, dname, loc); 
		int result;
		try {
			result = deptService.insert(dept);
		} catch (Exception e) {
			result = 0;
			e.printStackTrace();
		}
		
		
		response.getWriter().print(result);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 执行doGet方法
		doGet(request, response);
	}

}

DeptDao Dao表

package com.neu.dao;

import java.util.List;

import com.neu.entity.Dept;

public interface DeptDao {
	List<Dept> getAll() throws Exception;
	
	Dept getById(int deptno) throws Exception;
	//pageNum:页号,pageSize:每一页最大行数
	List<Dept> getPaged(int pageNum,int pageSize) throws Exception;
	//得到记录数(表中)
	int getCount() throws Exception;
	
	int insert(Dept dept) throws Exception;
	int delete(int deptno) throws Exception;
	int update(Dept dept) throws Exception;
	
}

DeptDaoImpl表

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 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 List<Dept> getPaged(int pageNum, int pageSize) throws Exception {
		Connection connection = JDBCUtil.getConnection();
		
		String sql = "select * from dept limit ?,?";
		
		ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {(pageNum-1)*pageSize,pageSize});
		
		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 getCount() throws Exception {
		Connection connection = JDBCUtil.getConnection();
		String sql ="select count(*) from dept";
		ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
		int count = 0;
		if(rs.next()) {
			count=rs.getInt(1);
		}
		JDBCUtil.closeConnection(connection);
		return count;
	}

	@Override
	public int insert(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 int delete(int deptno) throws Exception {
		String sql = "delete from dept where deptno =?";
		int n = JDBCUtil.executeUpdate(sql, new Object[] {deptno});
		return n;
	}

	@Override
	public int update(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;
	}

}

Dept

package com.neu.entity;

public class Dept {
	private Integer deptno;
	private String dname;
	private String 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 + "]";
	}
	public Dept() {
		super();
		// TODO Auto-generated constructor stub
	}
	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;
	}
}

service

package com.neu.service;

import java.util.List;

import com.neu.entity.Dept;

public interface DeptService {
	List<Dept> getAll() throws Exception;
	List<Dept> getPaged(int pageNum,int pageSize) throws Exception;
	int getCount() throws Exception;
	int insert(Dept dept) throws Exception;
	int delete(int deptno) throws Exception;
	int update(Dept dept) throws Exception;
}

DeptServiceImpl

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 {
		
		return deptDao.getAll();
	}
	@Override
	public List<Dept> getPaged(int pageNum, int pageSize) throws Exception {
		// TODO 自动生成的方法存根
		return deptDao.getPaged(pageNum, pageSize);
	}
	@Override
	public int getCount() throws Exception {
		// 
		return deptDao.getCount();
	}
	@Override
	public int insert(Dept dept) throws Exception {
		// 
		return deptDao.insert(dept);
	}
	@Override
	public int delete(int deptno) throws Exception {
		
		return deptDao.delete(deptno);
	}
	@Override
	public int update(Dept dept) throws Exception {
		// TODO 自动生成的方法存根
		return deptDao.update(dept);
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值