课堂练习之疫情可视化
本次课堂练习主要是学习使用echart图标工具实现疫情sql文件的柱状图展示
1.jdbc连接
1 package com.yiqin.connect; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class BaseConnection { 10 public static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; 11 public static String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db_platform";//127.0.0.1:1433 12 public static String userName="sa"; 13 public static String userPwd="w669399221"; 14 15 public static Connection getConn () { 16 Connection conn = null ; 17 18 try { 19 Class.forName(driverName);//加载驱动 20 conn = DriverManager.getConnection(dbURL, userName, userPwd); 21 22 } catch (Exception e) { 23 System.out.println("数据库连接失败!!"); 24 e.printStackTrace(); 25 } 26 27 return conn; 28 } 29 30 /** 31 * 关闭连接 32 * @param state 33 * @param conn 34 */ 35 public static void close (Statement state, Connection conn) { 36 if (state != null) { 37 try { 38 state.close(); 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 44 if (conn != null) { 45 try { 46 conn.close(); 47 } catch (SQLException e) { 48 e.printStackTrace(); 49 } 50 } 51 } 52 53 public static void close (ResultSet rs, Statement state, Connection conn) { 54 if (rs != null) { 55 try { 56 rs.close(); 57 } catch (SQLException e) { 58 e.printStackTrace(); 59 } 60 } 61 62 if (state != null) { 63 try { 64 state.close(); 65 } catch (SQLException e) { 66 e.printStackTrace(); 67 } 68 } 69 70 if (conn != null) { 71 try { 72 conn.close(); 73 } catch (SQLException e) { 74 e.printStackTrace(); 75 } 76 } 77 } 78 79 }
2.yiqinservlet.java
1 package com.yiqin.servlet; 2 import java.sql.PreparedStatement; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 import java.util.ArrayList; 7 import java.io.IOException; 8 import java.sql.Connection; 9 import java.sql.DriverManager; 10 11 import javax.servlet.ServletException; 12 import javax.servlet.annotation.WebServlet; 13 import javax.servlet.http.HttpServlet; 14 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletResponse; 16 import javax.servlet.http.HttpSession; 17 import com.yiqin.user.*; 18 19 import com.yiqin.connect.*;; 20 /** 21 * Servlet implementation class YiQinServlet 22 */ 23 @WebServlet("/YiQinServlet") 24 public class YiQinServlet extends HttpServlet { 25 private static final long serialVersionUID = 1L; 26 27 /** 28 * @see HttpServlet#HttpServlet() 29 */ 30 public YiQinServlet() { 31 super(); 32 // TODO Auto-generated constructor stub 33 } 34 35 /** 36 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 37 */ 38 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 39 // TODO Auto-generated method stub 40 request.setCharacterEncoding("utf-8"); 41 response.setContentType("text/html;charset=UTF-8"); 42 ArrayList<User> list = new ArrayList(); 43 ArrayList<User> list2 = new ArrayList(); 44 //澹版槑缂撳啿鍖� 45 HttpSession session = request.getSession(); 46 Connection conn = BaseConnection.getConn(); 47 PreparedStatement ps = null; 48 49 50 String start=request.getParameter("start"); 51 String end=request.getParameter("end"); 52 53 int as=0; 54 String name[] =new String [330]; 55 String num[]=new String[330]; 56 for(int i=0;i<330;i++) 57 { 58 name[i]=""; 59 num[i]="0"; 60 } 61 //************************************* 62 //鍦ㄨ繖閲屽啓!!!! 63 64 /*try { 65 Statement stmt = conn.createStatement(); 66 //1.鏀箂ql璇彞 67 ResultSet rs = stmt.executeQuery("select * from info4"); 68 while (rs.next()) { 69 70 71 String date=new String(rs.getString("Date")); 72 if(date.compareTo(start)>=0&& date.compareTo(end)<=0) 73 { 74 String province=new String(rs.getString("Province")); 75 String city=new String(rs.getString("City")); 76 String quezhen=new String(rs.getString("Confirmed_num")); 77 String zhiyu=new String(rs.getString("Cured_num")); 78 String dead=new String(rs.getString("Dead_num")); 79 System.out.println(date+" "+province+" "+city+" "+quezhen); 80 User use=new User(province,date,,quezhen,dead,zhiyu); 81 list.add(use); 82 83 } 84 85 } 86 87 88 }catch (SQLException e) { 89 response.getWriter().print("鏌ユ壘澶辫触"); 90 } 91 */ 92 /*try { 93 Statement stmt = conn.createStatement(); 94 //1.鏀箂ql璇彞 95 ResultSet rs = stmt.executeQuery("select * from info3"); 96 while (rs.next()) { 97 String date=new String(rs.getString("Date")); 98 if(date.compareTo(start)>=0&& date.compareTo(end)<=0) 99 { 100 String province=new String(rs.getString("Province")); 101 String city=new String(rs.getString("City")); 102 String quezhen=new String(rs.getString("Confirmed_num")); 103 String zhiyu=new String(rs.getString("Cured_num")); 104 String dead=new String(rs.getString("Dead_num")); 105 System.out.println(date+" "+province+" "+city+" "+quezhen); 106 user use=new user(date,province,city,quezhen,zhiyu,dead); 107 list.add(use); 108 } 109 110 } 111 112 113 }catch (SQLException e) { 114 response.getWriter().print("鏌ユ壘澶辫触"); 115 }*/ 116 try { 117 Statement stmt = conn.createStatement(); 118 //1.鏀箂ql璇彞 119 ResultSet rs = stmt.executeQuery("select * from info4"); 120 while (rs.next()) { 121 String date=new String(rs.getString("Date")); 122 if(date.compareTo(start)>=0&& date.compareTo(end)<=0) 123 { 124 String continents=new String(rs.getString("Continents")); 125 String province=new String(rs.getString("Province")); 126 String quezhen=new String(rs.getString("Confirmed_num")); 127 String zhiyu=new String(rs.getString("Cured_num")); 128 String dead=new String(rs.getString("Dead_num")); 129 130 User use=new User(continents,province,date,quezhen,dead,zhiyu); 131 list2.add(use); 132 name[as]=province; 133 num[as]= quezhen; 134 as++; 135 } 136 137 } 138 139 140 }catch (SQLException e) { 141 response.getWriter().print("鏌ユ壘澶辫触"); 142 } 143 request.setAttribute("list",list); 144 request.setAttribute("list2",list2); 145 session.setAttribute("name",name); 146 session.setAttribute("num",num); 147 request.getRequestDispatcher( "echarts.jsp").forward(request,response); 148 //*************************************** 149 150 151 } 152 153 /** 154 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 155 */ 156 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 157 // TODO Auto-generated method stub 158 doGet(request, response); 159 } 160 161 }
3.find.html
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <!-- 9 <style> 10 11 *{margin:0;padding:0;} 12 body{}; 13 .content 14 { 15 16 17 left:0; 18 width:100%; 19 height:400px; 20 margin-top:0px; 21 22 23 } 24 .main 25 { 26 background-color:#ffff00; 27 text-align:center; 28 height:400px; 29 padding: 0 auto; 30 padding:100px 0px; 31 32 33 } 34 .main h1 35 { 36 margin:0px 150px; 37 font-family:"楷体"; 38 font-size:50px; 39 font-weight:2px; 40 color:#226DDD; 41 padding:0px 50px; 42 border-radius:30px; 43 background-color:#ffcccc; 44 45 } 46 47 48 form{ 49 padding:20px 0; 50 } 51 input{ 52 border:1px solid black; 53 display:block; 54 margin:0px auto 10px auto;/*上 右 下 左*/ 55 padding:10px; 56 width:220px; 57 border-radius:30px;/*H5设置圆角边框*/ 58 font-size:18px; 59 font-weight:300; 60 text-align:center; 61 } 62 input:hover{ 63 background-color:pink; 64 } 65 button{ 66 background-color:white; 67 border-radius:10px; 68 69 border:1px solid black; 70 71 height:30px; 72 width:50px; 73 padding:5px 10px; 74 font-family:"楷体"; 75 } 76 button:hover{ 77 background-color:red; 78 } 79 --> 80 </style> 81 </head> 82 <body> 83 <div class="content"> 84 <div class="main"> 85 <h1>疫情统计</h1> 86 <form action="YiQinServlet" method="post"> 87 <input type="datetime" id="start" name="start" placeholder="起始时间"> 88 <input type="datetime" id="end" name="end" placeholder="终止时间"> 89 <button type="submit" id="search" name="search">查找</button> 90 </form> 91 </div> 92 </div> 93 </body> 94 </html>
4.echarts.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <!-- <style> *{margin:0;padding:0;} body{}; .content { left:0; width:100%; height:400px; margin-top:0px; } .main { background-color:#ffff00; text-align:center; height:400px; padding: 0 auto; padding:100px 0px; } .main h1 { margin:0px 150px; font-family:"楷体"; font-size:50px; font-weight:2px; color:#226DDD; padding:0px 50px; border-radius:30px; background-color:#ffcccc; } form{ padding:20px 0; } input{ border:1px solid black; display:block; margin:0px auto 10px auto;/*上 右 下 左*/ padding:10px; width:220px; border-radius:30px;/*H5设置圆角边框*/ font-size:18px; font-weight:300; text-align:center; } input:hover{ background-color:pink; } button{ background-color:white; border-radius:10px; border:1px solid black; height:30px; width:50px; padding:5px 10px; font-family:"楷体"; } button:hover{ background-color:red; } --> </style> </head> <body> <div class="content"> <div class="main"> <h1>疫情统计</h1> <form action="YiQinServlet" method="post"> <input type="datetime" id="start" name="start" placeholder="起始时间"> <input type="datetime" id="end" name="end" placeholder="终止时间"> <button type="submit" id="search" name="search">查找</button> </form> </div> </div> </body> </html>