为什么控制台不输出内容哎
package com.imooc.web.action;
import java.io.IOException;
import java.util.List;
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.imooc.domain.Category;
import com.imooc.service.CategoryService;
import com.imooc.service.impl.CategoryServiceImpl;
/**
* Servlet implementation class CategoryServlet
*/
@WebServlet("/CategoryServlet")
public class CategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//请求路径:localhost:8080/shop/CategoryServlet?method=findAll
String methodName=request.getParameter("method");
if("findAll".equals(methodName)) {
//查询所有分类
findALL(request,response);
}
}
//后台分类管理查询所有分类的方法
private void findALL(HttpServletRequest request, HttpServletResponse response) {
//接受参数
//封装数据
//调用业务成处理数据
System.out.println("CategoryServlet的findALL方法执行了");
CategoryService categoryService=new CategoryServiceImpl();
List<Category> list=categoryService.findAll();
for(Category category:list){
System.out.println(category);
}
//页面跳转
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.imooc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.imooc.dao.CategoryDao;
import com.imooc.domain.Category;
import com.imooc.utils.JDBCUtils;
public class CategoryDaoImpl implements CategoryDao {
@Override
public List<Category> findALL() {
System.out.println("CategoryDaoImp的findALL方法执行了");
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List<Category> list=null;
try {
//1.处理连接
con=JDBCUtils.getConnection();
//2.编写sql语句
String sql="select*from category";
//3.预编译sql
pstmt=con.prepareStatement(sql);
//4.设置参数
//5.执行sql
rs=pstmt.executeQuery();
//6.结果处理
list=new ArrayList<Category>() ;
while(rs.next()) {
Category category=new Category();
category.setCid(rs.getInt("cid"));
category.setCname(rs.getString("cname"));
category.setCdesc(rs.getString("cdesc"));
list.add(category);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
//7.释放资源
JDBCUtils.release(rs, pstmt, con);
}
return list;
}
}
public class Category {
private Integer cid;
private String cname;
private String cdesc;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCdesc() {
return cdesc;
}
public void setCdesc(String cdesc) {
this.cdesc = cdesc;
}
@Override
public String toString() {
return "Category [cid=" + cid + ", cname=" + cname + ", cdesc=" + cdesc + "]";
}
}
package com.imooc.service.impl;
import java.util.List;
import com.imooc.dao.CategoryDao;
import com.imooc.dao.impl.CategoryDaoImpl;
import com.imooc.domain.Category;
import com.imooc.service.CategoryService;
public class CategoryServiceImpl implements CategoryService {
@Override
public List<Category> findAll() {
//调用CategoryDao方法
System.out.println("CategoryServiceImp的findALL方法执行了");
CategoryDao categoryDao=new CategoryDaoImpl();
return categoryDao.findALL();
}
package com.imooc.service;
import java.util.List;
import com.imooc.domain.Category;
public interface CategoryService {
List<Category> findAll();
}
正在回答
同学,你好!我使用你的代码是可以正常运行的,运行效果如下图所示。请查看一下数据库中是否有数据,然后查看一下控制台是否有报错提示,如果有的话请贴到问答区。祝学习愉快!
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星