首页 > 编程语言 >用java实现书城项目(简单增删改查2)

用java实现书城项目(简单增删改查2)

时间:2023-03-26 23:12:58浏览次数:45  
标签:java String req 改查 books sql new 书城 selectbooks

书城项目

登录

dao

接口:UserDao

Users login(String username,String password);

实现:UserDaoImpl

QueryRunner queryrunner = new QueryRunner();
Connection connection = createConnection();
Users query = null;
String sql = "select * from users where username=? and password=?";
query = queryrunner.query(connection,sql,new BeanHandler<>(Users.class),username,password);
return query;

services

接口:UserServices 同UserDao代码一致

实现:UserServicesImpl

UserDao userdao = new UserDaoImpl();
return userdao.login(username,password)

servlet

UserServlet:service

req.setCharacterEncoding("e-8");
String reqkey = req.getParameter("reqkey");
Method method= getClass().getDeclaredMethod(reqkey,HttpServletRequest.class,HttpServletResonse.class);
method.setAccessible(true);
method.invoke(this,req,resp);

UserServlet:tologin

//接收数据
String username = req.getParameter("username");
String password = req.getParameter("password");

//调取方法
UserServicesImpl userservices = new UserServicesImpl();
User users = userservices.login(username,password);

//根据结果跳转页面
if(users==null){
    req.getRequestDispatcher("/pages/user/login.html").forword(req,resp);
}else{  
    req.getRequestDispatcher("/pages/user/login_success.html").forword(req,resp);
}

注册

dao

接口:UserDao

boolean regist(Users users);

实现:UserDaoImpl

QueryRunner queryrunner = new QueryRunner();
Connection connection = createConnection();
int i = 0;
String sql = "insert into users values(null,?,?,?)";
i=queryRunner.update(connection,sql,user.getUsername(),user.getPassword(),user.getEmail);
return i;

services

接口:UserServices 同UserDao代码一致

实现:UserServicesImpl

return userdao.regist(Users);

servlet

UserServlet:toregist

//接收数据
Users users = new Users();
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = req.getWriter();
 Map<String, String[]> parameterMap = req.getParameterMap();
BeanUtils.populate(users,parameterMap);

//调取方法
boolean b = userservices.regist(Users);
    
//根据结果跳转页面
    if(b){
        writer.writer("<script type='text/javascript'>alert('注册成功');location.href='/pages/user/regist_success.html'</script>");
    }else{
        writer.writer("<script type='text/javascript'>alert('注册失败');location.href='/pages/user/regist.html'</script>");
    }   
    

查询图书

dao

接口:BookDao

List<Books> selectbooks();

实现:BookDaoImpl

QueryRunner queryrunner = new QueryRunner();
Connection connection = createConnection();
 List<Books> booksList = null;
String sql = "select * from books";
booksList = queryRunner.query(connection,sql,new BeanListHandle<>(Books.class));

services

接口:BookServices 同BookDao

实现:BookServicesImpl

BookDaoImpl bookDao = new BookDaoImpl();
return  bookDao.selectbooks();

servlet

BookServlet:selectbooks

List<Books> selectbooks = bookServices.selectbooks();

添加图书

dao

接口:BookDao

int insertbook(Books books);

实现:BookDaoImpl

Connection connection = createConnection();
int i = 0;
String sql = "insert into books values(null,?,?,?,?,?,?)";
i = queryRunner.update(connection,sql,books.getTitle(),books.getAuthor(), books.getPrice(), books.getSales(), books.getStock(), "/static/uploads/huozhe.jpg")
return i;

services

接口:BookServices 同BookDao

实现:BookServicesImpl

 return bookDao.insertbook(books);

servlet

BookServlet:addbooks

Books book = new Books();
Map<String, String[]> parameterMap = req.getParameterMap();
BeanUtils.populate(book,parameterMap);
int i = bookServices.insertbook(book);
if(i>0){
    resp.sendRedirect("/pages/manager/bookServlet?reqkey=selectbooks");
}else{
    resp.sendRedirect("/pages/manager/book_add.html");
}

图书列表展示

dao

接口:BookDao

List<Books> selectbooks();

实现:BookDaoImpl

public List<Books> selectbooks(){
    String sql = "select * from books";
    List<Books> booksList = queryRunner.query(connection,sql,new BeanListHandler<>(Books.class));
    return booksList;
}

services

接口:BookServices

实现:BookServicesImpl

servlet

BookServlet:selectbooks

List<Books> selectbooks = bookServices.selectbooks();
req.serAttribute("blist",selectbooks);
processTemplate("manager/book_manager",req,resp);

查询列表id

dao

接口:BookDao

Books findById(int bookId);

实现:BookDaoImpl

String sql = "select * from books where id=?";
Books books = queryRunner.query(connection,sql,new BeanHandler<>(Books.class),bookId);
return books;

services

servlet

BookServelet:findbybookid

String bookid = req.getParameter("bookid");
if(bookid = null){
    throw new RuntimeException("bookid is null");
}
Books id = bookServices.findById(Integer.parseInt(bookid));
req.setAttribute("mybook",id);
processTemplate("manager/book_edit",req,resp);

修改图书

dao

接口:BookDao

 int updatebook(Books books);

实现:BookDaoImpl

String sql = "update books set title=?,author=?,price=?,sales=?,stock=? where id=?";
int i = queryRunner.update(connection,sql,books.getTitle(),books.getAuthor(),books.getPrice,books.getSales(),books.getStock(),books.getId());
return i;

services

servlet

BookServelet:updatebook

Books books = new Books();
 Map<String, String[]> parameterMap = req.getParameterMap();
BeanUtils.populate(books,parameterMap);
int i = bookServices.updatebook(books);
if(i>0){
    resp.sendRedirect("/pages/manager/bookServlet?reqkey=selectbooks");
}else{
    resp.sendRedirect("/pages/manager/bookServlet?reqkey=findbybooks&bookid="+books.getId());
}

删除图书

dao

接口:BookDao

int deletebook(int bookId);

实现:BookDaoImpl

String sql = "delete from books where id =?";
int i = queryRunnering.update(connection,sql,bookId);
return i;

services

servlet

BookServlet:deletebyid

String id = req.getParameter("bookid");
if(id == null){
    throw new RuntimeException("bookid is null");
}
int i = bookServices.deletebook(Integer.parseInt(id));
resp.sendRedirect("/pages/manager/bookServlet?reqkey=selectbooks");

标签:java,String,req,改查,books,sql,new,书城,selectbooks
From: https://www.cnblogs.com/deyo/p/17259891.html

相关文章