为什么控制台不输出内容哎

为什么控制台不输出内容哎

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();


}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学,你好!我使用你的代码是可以正常运行的,运行效果如下图所示。请查看一下数据库中是否有数据,然后查看一下控制台是否有报错提示,如果有的话请贴到问答区。祝学习愉快!

http://img1.sycdn.imooc.com//climg/5f7d61210963b12905050048.jpg

http://img1.sycdn.imooc.com//climg/5f7d663b09b5ca0a04850179.jpg

提问者 怀夢 2020-10-07 14:58:31

http://img1.sycdn.imooc.com//climg/5f7d67100972bc6406090181.jpg有这个报错

  • 同学,你好!请问你是启动Tomcat运行Servlet,还是使用的JUnit进行单元测试?看提示貌似要进行单元测试但又没有配置相关jar包。祝学习愉快!
    2020-10-07 16:26:49
  • 提问者 怀夢 回复 好帮手慕珊 #2
    我要进行单元测试,在控制台打印输出
    2020-10-07 19:23:57
  • 好帮手慕珊 回复 提问者 怀夢 #3
    同学,你好!如果是进行单元测试,请贴一下单元测试的代码看一下。对于现在这个问题,可以定义一个单元测试类,然后在该类中分别调用DAO和Service实现类的方法进行测试,或者直接执行Servlet也可以。祝学习愉快!
    2020-10-08 10:11:25
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师