当前位置: 首页 > article >正文

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

把数据库的驱动放在bin目录下。

通过servlet 读取数据库的内容,生成session,然后跨页面传给展示页。

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,获取数据,二是显示数据。

<%@ 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> 


http://www.kler.cn/a/147759.html

相关文章:

  • EMS(energy managment system)从0到1
  • Verdi -- 打开Consol,创建和执行tcl命令举例
  • 活着就好20241225
  • 深入解读数据资产化实践指南(2024年)
  • 重温设计模式--单例模式
  • 【AI日记】24.12.24 kaggle 比赛 2-12
  • 【Unity入门】LayerMask小结
  • UniApp 组件 u-tabs 详细讲解
  • pandas教程:Introduction to scikit-learn scikit-learn简介
  • SparkSession介绍
  • Docker入门教程
  • java:/comp/env/jdbc/testDbJndi通过JAVA代码读取JNDI获取数据源
  • C++之哈希
  • 软件设计先进性之虚拟化技术的应用
  • 计算机网络(超详解!) 第一节计算机网络的性能指标
  • DELPHI开发APP回忆录二安卓与pc端路径的选择
  • C语言--根据成绩判断等级
  • String类讲解(1)
  • 一文例说嵌入式 C 程序的内聚和耦合
  • Git学习笔记01
  • 基于单片机体温心率脉搏检测仪系统设计
  • 利用ogr2ogr从PostGIS中导出/导入Tab/Dxf/Geojson等格式数据
  • 十八数藏的文化数字革新:传统之美的数字转变
  • C#简化工作之实现网页爬虫获取数据
  • 软件工程 课后题 acmakb 总结
  • pgsql常用命令总结