基于Eclipse和Mysql数据库编写环境监控界面,主要有温度、湿度、光照等信息显示区域,历史查询显示区域等。由于没有硬件,本界面中的所有信息通过输入的方式输入到界面中,并存储到数据库中;环境监控界面具有提交和查询功能,而且能够根据时间查询历史数据,并显示到界面上。显示具体代码
时间: 2024-05-01 10:23:46 浏览: 98
环境监控
抱歉,由于您没有提供具体的代码,我无法为您提供完整的答案。但是,我可以给您一些指导和建议来帮助您编写环境监控界面。
1. 设计数据库表结构
首先,您需要设计数据库表结构来存储环境监控数据。您可以创建一个名为“environment_data”的表,其中包含以下字段:
- id:自增长的主键
- temperature:温度值
- humidity:湿度值
- light:光照值
- timestamp:时间戳
2. 创建Eclipse项目
在Eclipse中创建Java项目,并添加MySQL JDBC驱动程序以连接到MySQL数据库。
3. 创建用户界面
使用Eclipse的窗体设计器创建用户界面。您可以创建以下界面元素:
- 文本框:用于输入温度、湿度和光照值。
- 按钮:用于提交数据和查询历史数据。
- 表格:用于显示历史数据。
4. 编写Java代码
使用Java编写代码来处理用户界面上的操作。您需要编写以下方法:
- connect():连接到MySQL数据库。
- insertData():将温度、湿度和光照值插入到“environment_data”表中。
- queryData():根据时间戳查询历史数据,并将结果显示在表格中。
以下是示例代码:
```java
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class EnvironmentMonitor {
private JFrame frame;
private JTextField txtTemperature;
private JTextField txtHumidity;
private JTextField txtLight;
private JButton btnSubmit;
private JTable table;
private JTextField txtStartTime;
private JTextField txtEndTime;
private JButton btnQuery;
private Connection conn;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EnvironmentMonitor window = new EnvironmentMonitor();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public EnvironmentMonitor() {
initialize();
connect();
}
/**
* Connect to MySQL database.
*/
private void connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/env_monitor";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Insert data into database.
*/
private void insertData() {
try {
String sql = "INSERT INTO environment_data (temperature, humidity, light, timestamp) VALUES (?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setDouble(1, Double.parseDouble(txtTemperature.getText()));
stmt.setDouble(2, Double.parseDouble(txtHumidity.getText()));
stmt.setDouble(3, Double.parseDouble(txtLight.getText()));
stmt.setLong(4, System.currentTimeMillis());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Query data from database.
*/
private void queryData() {
try {
String sql = "SELECT * FROM environment_data WHERE timestamp BETWEEN ? AND ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setLong(1, Long.parseLong(txtStartTime.getText()));
stmt.setLong(2, Long.parseLong(txtEndTime.getText()));
ResultSet rs = stmt.executeQuery();
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
while (rs.next()) {
Object[] row = new Object[4];
row[0] = rs.getDouble("temperature");
row[1] = rs.getDouble("humidity");
row[2] = rs.getDouble("light");
row[3] = rs.getLong("timestamp");
model.addRow(row);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblTemperature = new JLabel("Temperature:");
lblTemperature.setBounds(10, 11, 80, 14);
frame.getContentPane().add(lblTemperature);
txtTemperature = new JTextField();
txtTemperature.setBounds(100, 8, 86, 20);
frame.getContentPane().add(txtTemperature);
txtTemperature.setColumns(10);
JLabel lblHumidity = new JLabel("Humidity:");
lblHumidity.setBounds(10, 36, 80, 14);
frame.getContentPane().add(lblHumidity);
txtHumidity = new JTextField();
txtHumidity.setBounds(100, 33, 86, 20);
frame.getContentPane().add(txtHumidity);
txtHumidity.setColumns(10);
JLabel lblLight = new JLabel("Light:");
lblLight.setBounds(10, 61, 80, 14);
frame.getContentPane().add(lblLight);
txtLight = new JTextField();
txtLight.setBounds(100, 58, 86, 20);
frame.getContentPane().add(txtLight);
txtLight.setColumns(10);
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertData();
}
});
btnSubmit.setBounds(10, 86, 89, 23);
frame.getContentPane().add(btnSubmit);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 120, 414, 131);
frame.getContentPane().add(scrollPane);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"Temperature", "Humidity", "Light", "Timestamp"
}
));
scrollPane.setViewportView(table);
JLabel lblStartTime = new JLabel("Start time:");
lblStartTime.setBounds(200, 11, 80, 14);
frame.getContentPane().add(lblStartTime);
txtStartTime = new JTextField();
txtStartTime.setBounds(290, 8, 86, 20);
frame.getContentPane().add(txtStartTime);
txtStartTime.setColumns(10);
JLabel lblEndTime = new JLabel("End time:");
lblEndTime.setBounds(200, 36, 80, 14);
frame.getContentPane().add(lblEndTime);
txtEndTime = new JTextField();
txtEndTime.setBounds(290, 33, 86, 20);
frame.getContentPane().add(txtEndTime);
txtEndTime.setColumns(10);
btnQuery = new JButton("Query");
btnQuery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
queryData();
}
});
btnQuery.setBounds(200, 58, 89, 23);
frame.getContentPane().add(btnQuery);
}
}
```
请注意,此代码仅为示例。您需要根据自己的需求进行适当的修改和扩展。
阅读全文