标签:void art error catch xsd url null 创建 org
·按照地市统计最受欢迎的Top10课程 (ip)
Dao层
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层
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文件:
<?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>
截图:
标签:void art error catch xsd url null 创建 org
原文地址:https://www.cnblogs.com/zmh-980509/p/11874840.html