Index.jsp
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>主界面</title>
</head>
<body>
<div align=”center”>
<div><a href=”Servlet?method=cha1″>查询</a> </div>
</div>
</body>
</html>
Cha.jsp
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>查询</title>
</head>
<body>
<form action=”Servlet?method=chaxun” method=”post”>
<div>
开始时间<select name=”timeone”>
<c:forEach items=”${holds}” var=”item” varStatus=”status”>
<option>${item.date}</option>
</c:forEach>
</select>
</div>
<div>
截至时间<select name=”timetwo”>
<c:forEach items=”${holds}” var=”item” varStatus=”status”>
<option>${item.date}</option>
</c:forEach>
</select>
</div>
<div>
<input type=”submit” value=”提交”>
</div>
</form>
</body>
</html>
Xianshi.jsp
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>显示</title>
</head>
<body>
<table >
<tr>
<td>时间</td>
<td>省份</td>
<td>城市</td>
<td>确诊人数</td>
<td>疑似人数</td>
<td>痊愈人数</td>
<td>死亡人数</td>
</tr>
<c:forEach items=”${holds}” var=”item” varStatus=”status”>
<tr>
<td>2020-3-13 13:23:07</td>
<td>${item.province}</td>
<td>${item.city}</td>
<td>${item.connum}</td>
<td>${item.yisinum}</td>
<td>${item.curenum}</td>
<td>${item.deadnum}</td>
</tr>
</c:forEach>
</table>
<div align=”center”>
<a href=”Servlet?method=xianshi”>用图表显示</a>
</div>
</body>
</html>
Xianshi2.jsp
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title>疫情分布图</title>
<!– 引入 echarts.js –>
<script src=”https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js”></script>
</head>
<body>
<% String timetwo=null;
timetwo=request.getParameter(timetwo);
%>
<!– 为ECharts准备一个具备大小(宽高)的Dom –>
<div id=”main” style=”width: 1500px;height:600px;”></div>
<script type=”text/javascript”>
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById(‘main’));
// 指定图表的配置项和数据
var option = {
title: {
text: ‘疫情分布图’
},
tooltip: {},
legend: {
data:[‘城市’]
},
xAxis: {
data: [“湖北省”,”广东省”,”浙江省”,”河南省”,”安徽省”,”江西省”,”江苏省”,”重庆市”,”山东省”,”四川省”,”北京市”,”黑龙江省”,”上海市”,”河北省”,”陕西省”,”广西壮族自治区”,”云南省”,”海南省”,”山西省”,”辽宁省”,”贵州省”,”天津市”,”甘肃省”,”吉林省”,”内蒙古自治区”,”宁夏回族自治区”,”新疆维吾尔自治区”,”香港”,”青海省”,”台湾”,”澳门”,”西藏自治区”]
},
yAxis: {},
series: [{
name: ‘人数’,
type: ‘bar’,
data:[“67790″,”1075″,”1048″,”981″,”733″,”698″,”439″,”426″,”407″,”363″,”315″,”295″,”286″,”195″,”195″,”183″,”138″,”124″,”104″,”99″,”89″,”88″,”71″,”69″,”52″,”45″,”42″,”26″,”18″,”17″,”10″,”1”]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
</body>
</html>
Dao.java:
package Dao;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import Entity.Date;
import DBUtil.DButil;
import Entity.qingkuang;
import Entity.City;
public class Dao {
String sql=”select * from info”;
Connection conn = DButil.getConn();
Statement state =null;
ResultSet rs = null;
List<Date> list = new ArrayList<>();
List<String> da=new ArrayList<>();
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
Date bean = null;
while (rs.next()) {
String date=rs.getString(“Date”);
String Date=date.substring(0, 19);
if(da.contains(Date)) {
}
else {
da.add(Date);
bean=new Date(Date);
list.add(bean);
}
}
}catch (Exception e) {
e.printStackTrace();
} finally {
DButil.close(rs,state, conn);
}
return list;
}
public List<qingkuang> chaxun(String timeone,String timetwo){
String sql=”select * from info where Date = ‘”+timeone+”‘”;
Connection conn = DButil.getConn();
Statement state =null;
ResultSet rs = null;
List<qingkuang> list=new ArrayList<>();
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
qingkuang bean = null;
while (rs.next()) {
String Timet=rs.getString(“Date”);
String timet=Timet.substring(0, 19);
if(timet.equals(timetwo)) {
break;
}
else {
String date=rs.getString(“Date”);
String Date=date.substring(0, 19);
String Province=rs.getString(“Province”);
String City=rs.getString(“City”);
String Connum=rs.getString(“Confirmed_num”);
String Yisinum=rs.getString(“Yisi_num”);
String Curenum=rs.getString(“Cured_num”);
String Deadnum=rs.getString(“Dead_num”);
bean=new qingkuang(Province,City,Connum,Yisinum,Curenum,Deadnum,Date);
list.add(bean);
}
}
}catch (Exception e) {
e.printStackTrace();
} finally {
DButil.close(rs,state, conn);
}
return list;
}
public List<City> xianshi(String timetwo){
String sql=”select * from info where City =””;
Connection conn = DButil.getConn();
Statement state =null;
ResultSet rs = null;
List<City> list=new ArrayList<>();
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
City bean = null;
while (rs.next()) {
String Timet=rs.getString(“Date”);
String timet=Timet.substring(0, 19);
if(timet.equals(timetwo)) {
String province=rs.getString(“Province”);
String num=rs.getString(“Confirmed_num”);
bean=new City(province,num);
list.add(bean);
}
}
}catch (Exception e) {
e.printStackTrace();
} finally {
DButil.close(rs,state, conn);
}
return list;
}
DBUtil.java:
package DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DButil {
public static String db_url = “jdbc:mysql://localhost:3306/yiqing”;
public static String db_user = “root”;
public static String db_pass = “wyp6666”;
public static Connection getConn () {
Connection conn = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}//end getConn
public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws SQLException {
Connection conn = getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql =”select * from info1 “;
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
System.out.println(“成攻”);
}else{
System.out.println(“失败”);
}
}
City.java:
package Entity;
public class City {
private String city;
private String num;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public City(String city,String num) {
super();
this.city=city;
this.num=num;
}
Date.java:
package Entity;
public class Date {
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Date(String date) {
super();
this.date=date;
}
Qingkuang.java:
package Entity;
public class qingkuang {
private String province;
private String city;
private String Connum;
private String Yisinum;
private String Deadnum;
private String Curenum;
private String Date;
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getConnum() {
return Connum;
}
public void setConnum(String connum) {
Connum = connum;
}
public String getYisinum() {
return Yisinum;
}
public void setYisinum(String yisinum) {
Yisinum = yisinum;
}
public String getDeadnum() {
return Deadnum;
}
public void setDeadnum(String deadnum) {
Deadnum = deadnum;
}
public String getCurenum() {
return Curenum;
}
public void setCurenum(String curenum) {
Curenum = curenum;
}
public qingkuang(String province,String city,String Connum,String Yisinum,String Deadnum,String Curenum,String Date) {
super();
this.city=city;
this.Connum=Connum;
this.Curenum=Curenum;
this.Deadnum=Deadnum;
this.province=province;
this.Yisinum=Yisinum;
this.Date=Date;
}
Servlet.java:
package Servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Dao.Dao;
import Entity.Date;
import Entity.qingkuang;
import Entity.City;
@WebServlet(“/Servlet”)
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet() {
super();
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(“utf-8”);
String method = req.getParameter(“method”);
if (“cha1”.equals(method)) {
cha1(req,resp);
}
if (“chaxun”.equals(method)) {
chaxun(req,resp);
}
if (“xianshi”.equals(method)) {
xianshi(req,resp);
}
}
private void cha1(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding(“utf-8”);
Dao dao=new Dao();
List<Date> holds=dao.cha1();
req.setAttribute(“holds”, holds);
req.getRequestDispatcher(“cha1.jsp”).forward(req,resp);
}
private void chaxun(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding(“utf-8”);
String timeone=req.getParameter(“timeone”);
String timetwo=req.getParameter(“timetwo”);
HttpSession session = req.getSession();
session.setAttribute(“timetwo”,timetwo);
Dao dao=new Dao();
List<qingkuang> holds=dao.chaxun(timeone, timetwo);
req.setAttribute(“holds”, holds);
req.getRequestDispatcher(“xianshi.jsp”).forward(req,resp);
}
private void xianshi(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding(“utf-8”);
HttpSession session = req.getSession();
String timetwo=null;
timetwo=(String)session.getAttribute(“timetwo”);
Dao dao=new Dao();
List<City> holds=dao.xianshi(timetwo);
req.setAttribute(“holds”, holds);
req.getRequestDispatcher(“xianshi2.jsp”).forward(req,resp);
}
原文链接:https://www.cnblogs.com/wyppaa/p/13093658.html
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/2198