·按照地市统计最受欢迎的Top10课程 (ip)
Dao层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package echart; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class barDAO { public ArrayList<barBean> select_all() { Connection conn = null ; Statement stmt = null ; ResultSet rst = null ; try { conn = DBUtil.getConnection(); stmt = conn.createStatement(); String sql = "select * from abc limit 10" ; rst = stmt.executeQuery(sql); ArrayList<barBean> array = new ArrayList<barBean>(); while (rst.next()) { barBean bar = new barBean(); bar.setName(rst.getString( "name" )); bar.setNum(rst.getInt( "num" )); array.add(bar); } stmt.close(); rst.close(); return array; } catch (SQLException e) { System.out.println( "Error occured at barDAO->select_all()" ); return new ArrayList<barBean>(); } } } |
servlet层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package echart; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; public class helloBar extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //创建了一个bardao的对象,barDAO主要是对数据库的连接和对数据库的操作 barDAO bardao= new barDAO(); //调用bardao的select_all()方法把从数据库中读取所有的数据返回的是一个ArrayList,ArrayList里面放的是一个barBean ArrayList<barBean> array = bardao.select_all(); //设置返回时的编码格式 response.setContentType( "text/html; charset=utf-8" ); //调用JSONArray.fromObject方法把array中的对象转化为JSON格式的数组 JSONArray json=JSONArray.fromObject(array); System.out.println(array.toString()); //返回给前段页面 PrintWriter out = response.getWriter(); out.println(json); out.flush(); out.close(); } } |
配置xml文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version= "1.0" encoding= "UTF-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id= "WebApp_ID" version= "4.0" > <display-name>echart</display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>helloBar</servlet-name> <servlet- class >echart.helloBar</servlet- class > </servlet> <servlet-mapping> <servlet-name>helloBar</servlet-name> <url-pattern>/bar. do </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
截图:
本文来自博客园,作者:哈利波特甜,转载请注明原文链接:https://www.cnblogs.com/zmh-980509/p/11874840.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步