servlet一调用dao就报错?什么毛病?

java阿俊 2018-02-04 11:18:22
servlet一调用dao就报错?什么毛病?
jsp正常调用


public class test {
//测试类,测试正常
public static void main(String []args) {
CostDao dao=new CostDao();
List<Cost> list=dao.findAll();
System.out.println(list);
}
}


,

public class CostDao implements Serializable {

public List<Cost> findAll() {
try {
Connection conn =
DBUtil.getConnection();
String sql =
"select * from cost "
+ "order by cost_id";
Statement smt =
conn.createStatement();
ResultSet rs = smt.executeQuery(sql);
List<Cost> list = new ArrayList<Cost>();
while(rs.next()) {
Cost c = new Cost();
c.setCostId(rs.getInt("cost_id"));
c.setName(rs.getString("name"));
c.setBaseDuration(rs.getInt("base_duration"));
c.setBaseCost(rs.getDouble("base_cost"));
c.setUnitCost(rs.getDouble("unit_cost"));
c.setStatus(rs.getString("status"));
c.setDescr(rs.getString("descr"));
c.setCreatime(rs.getTimestamp("creatime"));
c.setStartime(rs.getTimestamp("startime"));
c.setCostType(rs.getString("cost_type"));
list.add(c);
}
return list;
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(
"找不到驱动类",e);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(
"查询资费失败",e);
} finally {
DBUtil.closeConnection();
}
}


}


,

public class MainServlet extends HttpServlet {

@Override
protected void service(
HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
//获取并判断访问路径
String path = req.getServletPath();
if("/findCost.do".equals(path)) {
findCost(req,res);
} else {
throw new RuntimeException("查无此页");
}
}

//查询资费
protected void findCost(
HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
//查询所有资费
CostDao dao = new CostDao();

//就是在这出的错,把findAll一注释就不报错了,jsp正常
List<Cost> list = dao.findAll();
//转发至jsp
req.setAttribute("costs", list);
//当前:/netctoss/findCost.do
//目标:/netctoss/WEB-INF/cost/find.jsp
req.getRequestDispatcher
("WEB-INF/cost/find.jsp").forward(req, res);
}

}
...全文
181 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

684

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧