书城项目
登录
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");
}
标签:Java,String,req,改查,Sevelet,connection,books,new,Users
From: https://www.cnblogs.com/deyo/p/17253717.html