java 如何从数据库中查询并且处理数据,用折线图展示数据趋势变化?

需求如下

1、数据库中有一张表名为alarm_data的表,有time(时间),address(地址),shift(位移)三列

     这个表是用来存储当发生位移报警时,发生的时间、报警地址、报警位移的,不存储正常数据;

2、我需要根据地址、日期,来查询当天时间内位移变化趋势

3、要求折线图的形式展现

     折线图横坐标是时间0,1,2,3,4,5,6,7,8,9,10........23

    纵坐标是位移


程序步骤

1、根据时间和地址,按照时间升序,把查询的数据存储在集合里面

2、在存储数据时,要知道报警时间在0:00-1:00,1:00-2:00 ,2:00-3:00........哪个区间内,不然折线图的横坐标无法对应

3、假设查询地址0号,日期2019-01-26日的报警数据的时间区间是(3时7时,12时,21时);

      其他时间点就没有报警,位移就是正常的位移;

      假设正常位移是6.57mm,我们需要得到一个数组
 

时间01234567.........
位移6.576.576.57报警位移6.576.576.57报警位移..........

 

 

 5、折线图的横坐标对应的是时间,纵坐标是位移;只需要循环遍历一下数据就可以得到折线图


代码如下

1、查询数据库里面的数据的方法

package cn.com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import cn.com.pack.Alarm_Data;
import cn.com.pack.Six;
import cn.com.way.Blocks;
public class GetConnection {
	// 连接数据库,把数据存储到数据库中
	// 1.常量
	// 声明Connection对象
	static Connection con = null;
	// 驱动程序名
	static String driver = "com.mysql.jdbc.Driver";
	// URL指向要访问的数据库名mydata
	static String url = "jdbc:mysql://192.168.16.8:3306/lf";
	// MySQL配置时的用户名
	static String user = "root";
	// MySQL配置时的密码
	static String password = "root";
	//查询alarm_data表里的数据
	public static List<Six> selectinfo(String sql, String[] str) {
		ResultSet rs=null;
		PreparedStatement ps=null;
		List<Six> list=new ArrayList<Six>();
		Six six=null;
		// 加载驱动程序
		try {
			Class.forName(driver);
			// 1.getConnection()方法,连接MySQL数据库!!
			con = DriverManager.getConnection(url, user, password);
			ps = con.prepareStatement(sql);
			for (int i = 0; i < str.length; i++) {
				ps.setString(i+1, str[i]);
			}
			rs=ps.executeQuery();
			while(rs.next()){
				//由于报警的时间不是连续的,所以需要判断
				String time=rs.getString(1);
				//根据时间获取想要的int类型的值9:00-10:00之间的就返回9
				int t=Blocks.DayTime(time);
				String shift=rs.getString(3);
				//把t和shift放在一起
				six=new Six(shift, t);
				list.add(six);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		return list;

	}

}

   判断时间在哪个时间区间内

package cn.com.way;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Blocks {
	//给定日期和一个时间,就可以知道这个时间的区间范围
		public static int DayTime(String armtime) throws ParseException{
			//根据具体的时间来获取想要的日期
			String rq=Blocks.debu(armtime);
			int y=0;
			SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				long arm0=sim.parse(armtime).getTime();
				for(int i=0;i<24;i++){
					String begin=rq+" ";
					String end =rq+" ";
					String a=Integer.toString(i);
					String b=Integer.toString(i+1);
					if(a.length()==1){
						a="0"+a;
					}
					if(b.length()==1){
						b="0"+b;
					}
					begin=begin+a+":00:00";
					  end=end+b+":00:00";
					  //把开始区间时间和结束区间时间转换为long类型。然后比较
					  long arm1=sim.parse(begin).getTime();
					  long arm2=sim.parse(end).getTime();
					  if(arm0>arm1&&arm0<arm2){
						  y=i;
						 System.out.println(armtime+"在区间"+begin+"-"+end+"之间"); 
						 break;
					  } 
				}
				return y;     }
		//把字符串根据空格进行切割,来获取想要的日期
		public static String debu(String rq){			
					String [] str=rq.split(" ");
					return str[0];
			}
}

 


 

 2、查询数据,处理日期和时间

package cn.com.chart;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import cn.com.jdbc.GetConnection;
import cn.com.pack.Six;
import cn.com.way.InsertNum;
public class Displacement {
	/*
	 * author:命运的信徒 date:2019/1/24 arm:根据一天的24个点来查询位移变化量
	 */
	static int [] stri=new int[24];
	static double [] doub=new double[24];
	public static void selectPlace(String address, String rq) {
		// 1.根据地址、当天的日期、和标准量,来计算偏移量
		int add = Integer.parseInt(address);
		Date date = new Date();
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
		String dates = sim.format(date);
		// 2.查询数据库里当天的所有位移量
		String sql = "select * from alarm_data where address=? and time like ? order by time asc";
        rq=rq+" %";
		String [] str=new String[]{address,rq};
        List<Six> list=GetConnection.selectinfo(sql, str);
        //开始排序,填充了
        int m=0;
        for (Six six : list) {
        	//string类型变成double类型
        	double dou=Double.parseDouble(six.getOne());
        	m=InsertNum.fill(m+1,six.getTwo(),dou,stri,doub);
		}
        
	}

}

 //给出一组任意长度的数据(0<=数据的值<=23)9,11,12,15,21
//得到一个固定长度为23的一组数据,数据所对应的位置正是值+1;如9应该放在第10位,0应该放在第一位

package cn.com.way;
public class InsertNum {
//给出一组任意长度的数据(0<=数据的值<=23)9,11,12,15,21
//得到一个固定长度为23的一组数据,数据所对应的位置正是值+1;如9应该放在第10位,0应该放在第一位
public static int fill(int a,int b,double c,int [] stri,double [] doub){
	for (int i = a; i < 24; i++) {
		if(b==i){
			stri[i]=b;
			doub[i]=c-6.5746;
			System.out.println("-时间段-"+b+"-位移-"+c);
			break;
		}else{
			stri[i]=0;
			doub[i]=0;
		}
	}
	return b;
}


}

 

封装类

package cn.com.pack;

public class Six {
	private String one;
	private int two;
	private String three;
	private int four;
	private String five;
	private int six;
	public String getOne() {
		return one;
	}
	public void setOne(String one) {
		this.one = one;
	}
	public int getTwo() {
		return two;
	}
	public void setTwo(int two) {
		this.two = two;
	}
	public String getThree() {
		return three;
	}
	public void setThree(String three) {
		this.three = three;
	}
	public int getFour() {
		return four;
	}
	public void setFour(int four) {
		this.four = four;
	}
	public String getFive() {
		return five;
	}
	public void setFive(String five) {
		this.five = five;
	}
	public int getSix() {
		return six;
	}
	public void setSix(int six) {
		this.six = six;
	}
	public Six(String one, int two) {
		super();
		this.one = one;
		this.two = two;
	}

}

 


生成折线图代码

package cn.com.chart;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class TimeChart {
	public static void main(String[] args) {
		CategoryDataset mDataset = GetDataset();
		JFreeChart mChart = ChartFactory.createLineChart("折线图",// 图名字
				"时",// 横坐标
				"位移",// 纵坐标
				mDataset,// 数据集
				PlotOrientation.VERTICAL, true, // 显示图例
				true, // 采用标准生成器
				false);// 是否生成超链接

		CategoryPlot mPlot = (CategoryPlot) mChart.getPlot();
		mPlot.setBackgroundPaint(Color.LIGHT_GRAY);
		mPlot.setRangeGridlinePaint(Color.BLUE);// 背景底部横虚线
		mPlot.setOutlinePaint(Color.black);// 边界线

		ChartFrame mChartFrame = new ChartFrame("折线图", mChart);
		mChartFrame.pack();
		mChartFrame.setVisible(true);

	}

	public static CategoryDataset GetDataset() {
		DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
		//1.执行方法
		Displacement.selectPlace("0", "2019-01-25");
		//获取数组
		int [] str=Displacement.stri;
		double [] dou=Displacement.doub;
		for (int i = 0; i < dou.length; i++) {
			mDataset.addValue(dou[i],"地址0",i+"h");
		}
		
		return mDataset;
	}
}

 


 折线图如下

 

  • 7
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
要从数据库获取数据并形成折线图,可以按照以下步骤进行: 1. 连接数据库:使用 JDBC API 来连接数据库。通常需要提供数据库的 URL、用户名和密码来进行连接。 2. 查询数据:使用 SQL 语句查询需要的数据。可以使用 Statement 或 PreparedStatement 对象来执行 SQL 语句。 3. 处理结果集:将查询结果转换为 Java 对象,并存储在合适的数据结构,例如 ArrayList。 4. 使用图表库绘制折线图:选择一个适合的图表库,例如 JFreeChart,将数据传递给库的相应方法,生成折线图。 以下是一个简单的示例代码,假设数据库有一个名为 data 的表,其有两个字段 x 和 y,分别表示横轴和纵轴坐标: ```java import java.sql.*; import java.util.ArrayList; import org.jfree.chart.*; import org.jfree.data.xy.*; public class LineChart { public static void main(String[] args) throws SQLException { // 连接数据库 String url = "jdbc:mysql://localhost:3306/mydb"; String username = "root"; String password = "password"; Connection conn = DriverManager.getConnection(url, username, password); // 查询数据 String sql = "SELECT x, y FROM data"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); // 处理结果集 ArrayList<Double> xList = new ArrayList<>(); ArrayList<Double> yList = new ArrayList<>(); while (rs.next()) { double x = rs.getDouble("x"); double y = rs.getDouble("y"); xList.add(x); yList.add(y); } // 使用 JFreeChart 绘制折线图 XYSeries series = new XYSeries("data"); for (int i = 0; i < xList.size(); i++) { series.add(xList.get(i), yList.get(i)); } XYSeriesCollection dataset = new XYSeriesCollection(series); JFreeChart chart = ChartFactory.createXYLineChart("Line Chart", "X", "Y", dataset); ChartFrame frame = new ChartFrame("Line Chart", chart); frame.pack(); frame.setVisible(true); // 关闭连接和结果集 rs.close(); stmt.close(); conn.close(); } } ``` 在这个示例,我们使用了 JFreeChart 这个流行的图表库来绘制折线图。可以使用 Maven 或 Gradle 来导入 JFreeChart 的依赖。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37591637

请给我持续更新的动力~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值