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页面
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦