java web Servlet实现增删改查

package com.rupeng.test1;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;


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


public class PersonsServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}


	@Override
	/**
	 * 浏览器发出一次请求,服务器就会调用这个servlet的doGet一次
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// persons?action=list 显示所有成员
		// persons?action=addnew 显示新增页面
		// persons?action=edit&id=m 显示编辑id=m的页面
		// persons?action=addsubmit&name=newname&age=newage
		// persons?action=editsubmit&id=m&name=newname&age=newage
		// persons?action=delete&id=m 删除id=m的数据项
		resp.setContentType("text/html;charset=UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String action = req.getParameter("action");
		ResultSet set = null;
		PrintWriter writer = resp.getWriter();
		//显示信息
		if (action.equals("list")) {
			writer.print("<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head>");
			writer.print("<body>");
			writer.print("<p><a href='persons?action=addnew''>新增</a></p>");
			writer.print("<table>");
			writer.print("<thead><tr><td>id</td><td>姓名</td><td>年龄</td><td>修改</td><td>删除</td></tr></thead>");
			writer.print("<tbody>");
			try {
				set = JdbcCUtils.executeQurey("select * from t_persons2 ");
				while(set.next())
				{
					int id = set.getInt("id");
					String name = set.getString("name");
					int age = set.getInt("age");
					// \"是对"进行了转移
					writer.print("<tr><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+"<a href='persons?action=edit&id="+id+"'>编辑<a/>"+"</td><td>"+"<a onClick=' return confirm(\"确定要删除吗?\")'  href='persons?action=delete&id="+id+"'>删除</a>"+"</td></tr>");
					//a标签里使用onclick =“return confirm()”;如果onclick处还回false的话href超链接将不会折行
				}
			} catch (SQLException e) {
				writer.print("数据库查询出错!");
				e.printStackTrace();//用log4j工具来处理异常日志!(在项目中)
			}
			finally{
				JdbcCUtils.closeAll(set);
			}
			writer.print("</body>");
			writer.print("</table>");
			writer.print("</body></html>");
		}
		//显示新增页面
		else if (action.equals("addnew")) {
			writer.print("<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head>");
			writer.print("<body>");
			writer.print("<form action='persons' method='post'>");
			//隐藏域在界面中看不到!但是在表单提交的时候回提交到服务器!
			//name='action' value='addsubmit'就是让服务器知道我要干啥 !
			writer.print("<input type='hidden' name='action' value='addsubmit'/>");
			writer.print("<label for='name'>姓名:</label><input id='name' type='text' name='name' />");
			writer.print("<label for='age'>年龄:</label><input id='age' type='text' name='age' />");
			writer.print("<input type='submit' name='保存'/>");
			writer.print("</form>");
			writer.print("</body></html>");
		} 
		//显示编辑页面
		else if (action.equals("edit")) {
			writer.print("<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head>");
			writer.print("<body>");
			int id = Integer.parseInt(req.getParameter("id"));
			try {
				set = JdbcCUtils.executeQurey("select * from t_persons2 where id=?", id);
				if(set.next()){
					writer.print("<form action='persons' method='post'>");
					writer.print("<input type='hidden' name='action' value='editsubmit'>");
					writer.print("<input type='hidden' name='id' value='"+id+"'/>");
					String name = set.getString("name");
					int age = set.getInt("age");
					writer.print("<label for='name'>姓名:</label><input id='name' type='text' name='name' value='"+name+"'/>");
					writer.print("<label for='age'>年龄:</label><input id='age' type='text' name='age' value='"+age+"'/>");
					writer.print("<input type='submit' name='保存'/>");
					writer.print("</form>");
				}
				else{
					writer.print("不存在id="+id+"的记录");
				}
			} catch (SQLException e) {
				writer.print("编辑出错!");
				e.printStackTrace();
			}finally{
				JdbcCUtils.closeAll(set);
			}
			writer.print("</body></html>");
		} 
		//用户在新增页面中点击保存的处理逻辑
		else if (action.equals("addsubmit")) {
			String name = EncodeUtil.changeCode(req, "name");
			int age = Integer.parseInt(req.getParameter("age"));
			try {
				JdbcCUtils.executeUpdate("insert into t_persons2(name,age) values(?,?)",name,age);
				//更新成功才进行重定向!
				resp.sendRedirect("persons?action=list");//保存成功后重定向到查询页面!
			} catch (SQLException e) {
				writer.print("新增出错!");
				e.printStackTrace();//用log4j工具来处理异常日志!(在项目中)
			}
		} else if (action.equals("editsubmit")) {
			String name = EncodeUtil.changeCode(req, "name");
			int age = Integer.parseInt(req.getParameter("age"));
			int id = Integer.parseInt(req.getParameter("id"));
			try {
				JdbcCUtils.executeUpdate("update t_persons2 set name=?,age=? where id=?", name,age,id);
				//编辑成功后重定向!
				resp.sendRedirect("persons?action=list");//编辑后重定向到查询页面!
			} catch (SQLException e) {
				writer.print("修改出错!");
				e.printStackTrace();
			}
		} else if (action.equals("delete")) {
			int id = Integer.parseInt(req.getParameter("id"));
			try {
				JdbcCUtils.executeUpdate("delete  from t_persons2 where id=?",id);
				//删除成功后重定向!
				resp.sendRedirect("persons?action=list");//删除后重定向到查询页面!
			} catch (SQLException e) {
				writer.print("删除出错!");
				e.printStackTrace();
			}
		} else {
			writer.print("未知的ection");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值