JSP面向接口的编程方式2

这篇博客介绍了如何使用JSP连接MySQL数据库并实现删除、更新和插入操作。通过接口定义方法,实现类的具体实现,包括预编译SQL语句,设置参数并执行SQL命令。同时,展示了实体类`ClassInfo`的定义,以及在Servlet中处理GET和POST请求,根据用户输入执行相应的数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JSP连接MySQL的删除、修改、添加

1.在接口定义一个方法

public int deleteClass(int parseInt);
public int update(ClassInfo c);
public int insert(ClassInfo c);

2.实现类实现方法删除方法,修改,添加


// An highlighted block
public int deleteClass(int id) {
		// TODO Auto-generated method stub
		// 1.数据库连接
		int result = 0;
		Connection conn = Dome.getconn();
		// 2.书写sql语句
		String sql = "delete from classinfo where id=?";
		// 3. 预编译sql
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			// 4.给占位符(参数)赋值
			ps.setInt(1, id);
			// 5.执行命令,删除
			result = ps.executeUpdate();
			//
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public int update(ClassInfo c) {
		// TODO Auto-generated method stub
		// 1.数据库连接
		int result = 0;
		Connection conn = Dome.getconn();
		// 2.书写sql语句
		String sql = "update  classinfo set classname=? where id=?";
		// 3. 预编译sql
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			// 4.
			ps.setString(1, c.getClassname());
			ps.setInt(2, c.getId());
			// 5.
			result = ps.executeUpdate();
			//
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
public int insert(ClassInfo c) {
		// TODO Auto-generated method stub
		// 1.数据库连接
		int result = 0;
		Connection conn = Dome.getconn();
		// 2.书写sql语句
		String sql = "insert into classinfo values(?,?)";
		// 3. 预编译sql
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			// 4.给占位符(参数)赋值
			ps.setInt(1, c.getId());
			ps.setString(2, c.getClassname());
			// 5.执行命令,删除
			result = ps.executeUpdate();
			//
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

3.创建实体层

package bao.du.entity;

public class ClassInfo {
	/*班级实体类
*/	
	private int id;
	private String classname;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getClassname() {
		return classname;
	}
	public void setClassname(String classname) {
		this.classname = classname;
	}
	public ClassInfo(int id, String classname) {
		
		this.id = id;
		this.classname = classname;
	}
	public ClassInfo() {
		
	}
	public ClassInfo(String classname) {
		
		this.classname = classname;
	}
    
}

4.显示页面逻辑

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		  request.setCharacterEncoding("UTF-8");
		  response.setContentType("text/html;charset=utf-8");
		  String id=request.getParameter("id");
		  IClassinfo icd=new ClassInfoDaoImpl();
		  int result =icd.deleteClass(Integer.parseInt(id));
		  if (result>0) {
			response.sendRedirect("ClassServlet");
		}
	}
	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		 
		 request.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=utf-8");
		 int id=Integer.parseInt(request.getParameter("id"));
		 String classname=request.getParameter("classname");
		 String h=request.getParameter("h");
		 IClassinfo icd=new ClassInfoDaoImpl();
		 int result =0;
		 if (h.equals("")) {
			 result=icd.insert(new ClassInfo(id,classname));
		} else {
			result=icd.update(new ClassInfo(id,classname));
			
		}
		 
		if (result>0) {
			response.sendRedirect("ClassServlet");
		}
	}
		 
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜小姐-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值