easyui06:数据的删改

easyui06:数据的删改,第1张

基于上篇博客,在各个包类增加新的方法

com.zking.dao

package com.zking.dao;

import java.util.List;

import com.zking.entity.Book;

public interface IBookDao {
	
	/**
	 * 带模糊查询的分页
	 * @param pageIndex  页数
	 * @param pageSize 每页几条数据
	 * @param str  名称的关键字
	 * @return  结果集合
	 */
	public List getAllByPage(int pageIndex,int pageSize,String str,String col);
	
	/**
	 * 获得总行数
	 * @param str  表名等
	 * @return 总行数
	 */
	public int getRows(String str);
	
	/**
	 * 增加
	 * @param b
	 * @return
	 */
	public int addts(Book b);
	
	
	/**
	 * 删除书籍
	 * @param bid  书籍编号
	 * @return  影响行数
	 */
	public int deleteBook(int bid);
	
	/**
	 * 修改书籍
	 * @param bid  书籍编号
	 * @param b  要修改的对象
	 * @return  影响行数
	 */
	public int updateBook(int bid,Book b);
	
	
}


package com.zking.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zking.entity.Book;
import com.zking.util.DBHelper;

public class BookDao implements IBookDao{
	//三兄弟
	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	
	
	@Override
	public List getAllByPage(int pageIndex, int pageSize, String str,String col) {
		List  list = new ArrayList();
		int a =(pageIndex-1)*pageSize+1;
		int b = pageIndex*pageSize;
		try {
			//获得连接
			con=DBHelper.getCon();
			//定义SQL语句
			String sql = "select * from (select a.*,rownum as rid from tb_book a where "+col+" like '%"+str+"%') b  where b.rid between ? and ?";
			//获得执行对象
			ps=con.prepareStatement(sql);
			//给占位符赋值
			ps.setInt(1, a);
			ps.setInt(2, b);
			//获得结果集
			rs=ps.executeQuery();
			while(rs.next()) {
				Book  bb = new Book(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4));
				//加到集合里面去
				list.add(bb);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClo(con, ps, rs);
		}
		return list;
	}

	@Override
	public int getRows(String str) {
		int n = 0;
		try {
			con=DBHelper.getCon();
			//定义SQL语句
			String sql = "select count(*) from  "+str;
			//获得执行对象
			ps=con.prepareStatement(sql);
			//获得结果集
			rs=ps.executeQuery();
			if(rs.next()) {
				n=rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBHelper.myClo(con, ps, rs);
		}
		return n;
	}

	@Override
	public int addts(Book b) {
		int  n = 0;
		try {
			con = DBHelper.getCon();
			String sql = "insert into tb_book select nvl(max(bid),0)+1,?,?,? from tb_book";
			ps=con.prepareStatement(sql);
			ps.setString(1, b.getBname());
			ps.setDouble(2, b.getBprice());
			ps.setString(3, b.getBtype());
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClo(con, ps, rs);
		}
		return n;
	}

	@Override
	public int deleteBook(int bid) {
		int n = 0;
		try {
			//获得连接
			con=DBHelper.getCon();
			//定义SQL语句
			String  sql = "delete from tb_book  where bid="+bid;
			//获得执行对象
			ps=con.prepareStatement(sql);
			//开始执行
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBHelper.myClo(con, ps, rs);
		}
		return n;
	}

	@Override
	public int updateBook(int bid, Book b) {
		int n = 0;
		try {
			//获得连接
			con=DBHelper.getCon();
			//定义SQL语句
			String  sql = "update tb_book  set  bname=?,bprice=?,btype=?  where bid=?";
			//获得执行对象
			ps=con.prepareStatement(sql);
			//给占位符赋值
			ps.setString(1, b.getBname());
			ps.setDouble(2, b.getBprice());
			ps.setString(3, b.getBtype());
			ps.setInt(4, bid);
			//开始执行
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBHelper.myClo(con, ps, rs);
		}
		return n;
	}

}


com.zking.biz

package com.zking.biz;

import java.util.List;

import com.zking.entity.Book;

public interface IBookBiz {
	
	/**
	 * 带模糊查询的分页
	 * @param pageIndex  页数
	 * @param pageSize 每页几条数据
	 * @param str  名称的关键字
	 * @return  结果集合
	 */
	public List getAllByPage(int pageIndex,int pageSize,String str,String col);
	
	/**
	 * 获得总行数
	 * @param str  表名等
	 * @return 总行数
	 */
	public int getRows(String str);
	
	/**
	 * 增加
	 * @param b
	 * @return
	 */
	public int addts(Book b);
	
	
	/**
	 * 删除书籍
	 * @param bid  书籍编号
	 * @return  影响行数
	 */
	public int deleteBook(int bid);
	
	/**
	 * 修改书籍
	 * @param bid  书籍编号
	 * @param b  要修改的对象
	 * @return  影响行数
	 */
	public int updateBook(int bid,Book b);
	
}


package com.zking.biz;

import java.util.List;

import com.zking.dao.BookDao;
import com.zking.dao.IBookDao;
import com.zking.entity.Book;
/**
 * 业务逻辑层
 * @author zjjt
 *
 */
public class BookBiz implements IBookBiz{

	  IBookDao ibd = new BookDao();
	
	@Override
	public List getAllByPage(int pageIndex, int pageSize, String str,String col) {
		return ibd.getAllByPage(pageIndex, pageSize, str,col);
	}

	@Override
	public int getRows(String str) {
		return ibd.getRows(str);
	}

	@Override
	public int addts(Book b) {
		return ibd.addts(b);
	}

	@Override
	public int deleteBook(int bid) {
		return ibd.deleteBook(bid);
	}

	@Override
	public int updateBook(int bid, Book b) {
		return ibd.updateBook(bid, b);
	}
	
//	public static void main(String[] args) {
//		System.out.println(ibd.getAllByPage(1, 10, ""));
//	}

}

com.zking.servlet

package com.zking.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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.zking.biz.BookBiz;
import com.zking.biz.IBookBiz;

/**
 * Servlet implementation class DeleteBookServlet
 */
@WebServlet("/deleteBook.do")
public class DeleteBookServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		
		PrintWriter out = response.getWriter();
		
	
		//接受前台传过来的值
		String bid = request.getParameter("bid");//编号
		//调用biz层
		IBookBiz  ibb = new BookBiz();
		//调用删除的方法
		int n =  ibb.deleteBook(Integer.parseInt(bid));
		String str="error";
		if(n>0) {//说明删除成功
			str="success";
		}
		
		out.write(str);
		out.flush();
		out.close();
		
	}

}


package com.zking.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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.zking.biz.BookBiz;
import com.zking.biz.IBookBiz;
import com.zking.entity.Book;

/**
 * Servlet implementation class AddBookServlet
 */
@WebServlet("/updateBook.do")
public class UpdateBookServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		
		PrintWriter out = response.getWriter();
		
	
		//接受前台传过来的值
		String bid = request.getParameter("bid");//编号
		
		String bname = request.getParameter("bname");//名称
		
		String a = request.getParameter("bprice");//价格
		
		String btype = request.getParameter("btype");//类型
		double bprice =0;
		if(a!=null) {
			 bprice = Double.parseDouble(a);
		}
		IBookBiz ibb = new BookBiz();
		//实例化一个书籍对象
		Book b = new Book(bname, bprice, btype);
		//调用修改方法
		int n = ibb.updateBook(Integer.parseInt(bid), b);
		String str="error";
		if(n>0) {//说明修改成功
			str="success";
		}
		
		out.write(str);
		out.flush();
		out.close();
	}

}






bookList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  
  <%@ include file="../../common/head.jsp" %>




Insert title here



	
	
		书籍名称:
		
		 
		
		查询 
	
	
	
	

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/905947.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存