servlet获取数据:publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Connectionconn=null;response.setContentType("tex...
servlet获取数据:public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Connection conn = null;response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();try{// 注册 JDBC 驱动器Class.forName("com.mysql.jdbc.Driver");// 打开一个连接conn = DriverManager.getConnection(DB_URL,USER,PASS);// 执行 SQL 查String sql;sql = "SELECT title,subtitle,img_url,adult FROM travel_team LEFT JOIN travel_standard ON travel_standard.standard_id=travel_team.standard_id LEFT JOIN travel_price_type ON travel_price_type.team_id=travel_team.team_id LEFT JOIN travel_img ON travel_img.img_id=travel_standard.img_ids WHERE travel_team.shopowner=1;";PreparedStatement ps=conn.prepareStatement(sql);ResultSet rs = ps.executeQuery();//数据集合List> rows=new ArrayList>();//迭代结果集Map result=new HashMap();// 展开结果集数据库while(rs.next()){Map data=new HashMap();// 通过字段检索data.put("adult",rs.getFloat("adult"));data.put("img_url",rs.getString("img_url"));data.put("subtitle",rs.getString("subtitle"));data.put("title",rs.getString("title"));rows.add(data);}result.put("rows",rows);String json=JSON.toJSONString(result);//request.setAttribute("json", json);//response.sendRedirect("index.html");out.write(json);//out.print(json);//out.println("