684
社区成员
发帖
与我相关
我的任务
分享
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);
}
}