Servlet: public class StaticsServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PreparedStatement pstmt = null; Connection conn = DB.getConnection(); //注意这里要获取到数据库的连接操作 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); ResultSet rs = null; String sql = "select count(*) FROM recard ";//执行SQL进行统计 try { pstmt = conn.prepareStatement(sql); } catch (SQLException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } try { rs = pstmt.executeQuery(); } catch (SQLException e1){ // TODO Auto-generated catch block e1.printStackTrace(); } int num = 0; try { while (rs.next()) { //防止在结果集之前运行出现异常 num = rs.getInt(1); //取出结果集的第一列的值情况 } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } request.setAttribute("num", num); request.getRequestDispatcher("/Statics.jsp").forward(request, response); System.out.println("++++++++++++" + num); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }
Statics.jsp: <body> <% Integer num=(Integer)request.getAttribute("num");%> <table> <th>已借数量</th> </table> <td><%=num %></td>
主要是连接数据库之后 对数据库进行统计操作
得到的数据会存放在结果集中 然后把结果集里面的数据拿出来
传给jsp页面
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦