动态网页从数据库取信息,然后展示。

把数据库的驱动放在bin目录下。
通过servlet 读取数据库的内容,生成session,然后跨页面传给展示页。

java 复制代码
package src;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class aaa
 */
@WebServlet("/aaa")
public class aaa extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public aaa() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		
		 //1.导入jar包
        //2.注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

        //3.获取连接
        Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:mysql://mysql.sqlpub.com:3306/huangjin","laocooon","fc12f7a5215e8e0a_");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

        //4.获取执行者对象
        Statement stat = null;
		try {
			stat = con.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        //5.执行sql语句,并且接收结果
        String sql = "SELECT * FROM MonthlySorted";
        ResultSet rs = null;
		try {
			rs = stat.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        //6.处理结果
        String i1="",i2="",i3="";

        try {
			while(rs.next()) {
			    i1+="'"+rs.getString(1)+"',";
			    i2+="'"+rs.getString(2)+"',";
			    i3+="'"+rs.getString(3)+"',";
			    
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        i1=i1.substring(0,i1.length()-1);//去掉最后一个逗号
        i2=i2.substring(0,i2.length()-1);//去掉最后一个逗号
        i3=i3.substring(0,i3.length()-1);//去掉最后一个逗号
        request.getSession().setAttribute("i1", i1);
        request.getSession().setAttribute("i2", i2);
        request.getSession().setAttribute("i3", i3);
        //7.释放资源
        try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			stat.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        //PrintWriter out = response.getWriter();		
	    //out.println(request.getSession().getAttribute("i1"));
	    //out.println(request.getSession().getAttribute("i2"));
	    //out.println(request.getSession().getAttribute("i3"));
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

此页面作了两件事, 一就是通过<iframe src="./aaa" class="hidden"></iframe> 执行 aaa,获取数据,二是显示数据。

java 复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>  
    <%
    String i1=(String)session.getAttribute("i1");
    String i2=(String)session.getAttribute("i2");
    String i3=(String)session.getAttribute("i3");
    %> 
   
<!DOCTYPE html>
<html>
<head>
  <title>气温图表</title>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  <style>
    #myChart{
        width:300px;
        height: 300px;
    }

  .hidden {
    display: none;
  }

  </style>
</head>
<body>
<iframe src="./aaa" class="hidden"></iframe>   
  <canvas id="myChart"></canvas>
  <script>
   
    var tempData = {
      labels: [<%=i1%>],
      datasets: [
        {
          label: "最高气温",
          backgroundColor: "rgba(255,0,0,0.5)",
          borderColor: "red",
          borderWidth: 1,
          data: [<%=i2%>]
        },
        {
          label: "最低气温",
          backgroundColor: "rgba(0,0,255,0.5)",
          borderColor: "blue",
          borderWidth: 1,
          data: [<%=i3%>]
        }
      ]
    };
 
    var tempOptions = {
      responsive: true,
      maintainAspectRatio: false,
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true
          }
        }]
      }
    };
 
 
    var ctx = document.getElementById("myChart").getContext("2d");
 
    var myChart = new Chart(ctx, {
      type: "bar",
      data: tempData,
      options: tempOptions
    });
  </script>
</body>
</html> 
相关推荐
你的微笑,乱了夏天24 分钟前
linux centos 7 安装 mongodb7
数据库·mongodb
工业甲酰苯胺35 分钟前
分布式系统架构:服务容错
数据库·架构
独行soc2 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍08-基于时间延迟的SQL注入(Time-Based SQL Injection)
数据库·sql·安全·渗透测试·漏洞挖掘
White_Mountain2 小时前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu
Code apprenticeship2 小时前
怎么利用Redis实现延时队列?
数据库·redis·缓存
百度智能云技术站2 小时前
广告投放系统成本降低 70%+,基于 Redis 容量型数据库 PegaDB 的方案设计和业务实践
数据库·redis·oracle
装不满的克莱因瓶2 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
梦想平凡4 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
TianyaOAO4 小时前
mysql的事务控制和数据库的备份和恢复
数据库·mysql
Ewen Seong4 小时前
mysql系列5—Innodb的缓存
数据库·mysql·缓存