ECharts与Java的后端数据交互(一)

功能:

  • 1.展示mysql中的数据
  • 2.导出Echarts报表,生成.xls文件

最终运行图:
ECharts与Java的后端数据交互(一)_第1张图片
ECharts与Java的后端数据交互(一)_第2张图片

项目整体目录:
ECharts与Java的后端数据交互(一)_第3张图片
sqlyog内容:
ECharts与Java的后端数据交互(一)_第4张图片
实力类代码:

package com.zh.bean;
public class Cxx {  
	private String cxx;
	private int c1;
	private int c2;
	private int c3;
	private int c4;
	private int c5;
	private int c6;
	
	public String getCxx() {
		return cxx;
	}
	public void setCxx(String cxx) {
		this.cxx = cxx;
	}
	public int getC1() {
		return c1;
	}
	public void setC1(int c1) {
		this.c1 = c1;
	}
	public int getC2() {
		return c2;
	}
	public void setC2(int c2) {
		this.c2 = c2;
	}
	public int getC3() {
		return c3;
	}
	public void setC3(int c3) {
		this.c3 = c3;
	}
	public int getC4() {
		return c4;
	}
	public void setC4(int c4) {
		this.c4 = c4;
	}
	public int getC5() {
		return c5;
	}
	public void setC5(int c5) {
		this.c5 = c5;
	}
	public int getC6() {
		return c6;
	}
	public void setC6(int c6) {
		this.c6 = c6;
	}
	
	
	
}  

持久层:

package com.zh.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import com.zh.bean.Cxx;

public class CxxDao {
	public List query() {
		ArrayList barArr = new ArrayList();
		try {
			//JDBC方式连接MySQL数据库
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test?characterEncoding=utf8", "root", "yj123456");
			PreparedStatement stmt = conn.prepareStatement("SELECT * FROM c1");
			ResultSet rs = stmt.executeQuery();
			//将test表中的数据存储到线性表中
			while(rs.next()) {
				Cxx bar = new Cxx();
				bar.setCxx(rs.getString("cxx"));
				bar.setC1(rs.getInt("a1"));
				bar.setC2(rs.getInt("a2"));
				bar.setC3(rs.getInt("a3"));
				bar.setC4(rs.getInt("a4"));
				bar.setC5(rs.getInt("a5"));
				bar.setC6(rs.getInt("a6"));
			
				barArr.add(bar);
			}
			//关闭连接
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return barArr;
	}
}

控制层:

package com.zh.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
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;

import com.zh.bean.Cxx;
import com.zh.dao.CxxDao;

public class CxxService extends HttpServlet {
	 
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//创建DAO
		CxxDao cxxDao = new CxxDao();
		//从数据库里取数据
		ArrayList cxxArr = (ArrayList) cxxDao.query();
		//设置服务器响应时向JSP表示层传输数据的编码格式
		resp.setContentType("text/html; charset=utf-8");
		//ArrayList对象转化为JSON对象
		JSONArray json = JSONArray.fromObject(cxxArr);
		//控制台显示JSON
		System.out.println(json.toString());
		//返回到JSP
		PrintWriter writer = resp.getWriter();
		writer.println(json);
		writer.flush();
		//关闭输出流
		writer.close();
	}
}

视图层:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName()
			+ ":" + request.getServerPort()	+ path + "/";
%>


	
	Hello ECharts
	
	
	
	
	


	
	

web.xml路径控制:



  echarts_demo
 
		test.jsp
	
 
	
	
		cxxService
		com.zh.service.CxxService
	
	
		cxxService
		/cxx.do
	
	
	 
  

文章已发表源码:
https://download.csdn.net/download/qq_43251032/11151158
ECharts3官网实例:
https://echarts.baidu.com/
ECharts3.0与java的后端数据交互(二):
https://blog.csdn.net/qq_43251032/article/details/89924977

你可能感兴趣的:(Java案列,java,echarts,mysql)