前言
一、历史时序数据字段介绍
二、插入历史时序数据
三、修改历史时序数据
四、查询历史时序数据
前文提到了用statement的方式,操作ProcessDB时序数据库,我们实际工作中可能更常使用的是 PreparedStatement方式,优点如下:1、可以防止SQL注入攻击。 2、进行动态查询。3、执行更快。4、简化拼接过程。所以,我在这里着重讲解下如何用PreparedStatement方式,操作ProcessDB实时/时序数据库的历史时序数据
CREATE TABLE HIS_TABLE (
ID_F INT;//数据点ID,
NAME_F STRING;//数据点名称,
VALUE_F STRING;//历史数据值,
UNIT_F STRING;//数据单位,
QUALITY_F STRING;//数据质量,
TIME_F STRING;//历史数据时间,
MESSAGE_F STRING;//历史数据值(特殊),
DATE_TYPE STRING;//数据类型,
INTERVAL_F INT;//历史数据时间间隔
)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws Exception {
try {
Class.forName("com.processdb.jdbc.JDBC");
Connection connection = null;
Statement statement = null;
try {
// 对数据库进行连接
connection = DriverManager.getConnection("jdbc:processdb://localhost:8301/instance01", "root", "root");
statement = connection.createStatement();
// 设置超时时间
statement.setQueryTimeout(30); // set timeout to 30 sec.
// 插入sql
/**插入历史数据表 HIS_TABLE
* 所需字段
* name_f = 'public.sys.PDB_OS_CPU_USAGE' 数据点的位置(必填)
* NAME_F 数据点的位置(必填)
* TIME_F 历史数据的时间(必填)
* DATE_TYPE 数据类型(必填)
* VALUE_F 插入数据的值(必填)
*/
String sql = "INSERT INTO HIS_TABLE (NAME_F,TIME_F,DATE_TYPE,VALUE_F) values (?,?,?,?)";
// 运行sql发起请求
String param1 = "D77.T77.P77";
String param2 = "1669702505000";
String param3 = "FLOAT32";
String param4 = "77";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, param1);
ps.setString(2, param2);
ps.setString(3, param3);
ps.setString(4, param4);
// 运行sql发起请求
ps.executeUpdate();
// 查询结果存储在ResultSet
System.out.println("Insert history data into table: history_table.");}
catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println(e);
}
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
return;
}
return;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
try {
Class.forName("com.processdb.jdbc.JDBC");
Connection connection = null;
Statement statement = null;
try {
// 对数据库进行连接
connection = DriverManager.getConnection("jdbc:processdb://localhost:8301/instance01", "root", "root");
statement = connection.createStatement();
// 设置超时时间
statement.setQueryTimeout(30); // set timeout to 30 sec.
// sql
/**修改历史数据 HIS_TABLE
* 所需字段
* name_f 数据库名称加数据表名称加数据点名称(例如:public.sys.PDB_OS_CPU_USAGE)
* TIME_F 历史数据时间
* 可修改字段
* value_f 历史数据数据值
*/
String sql = "Update HIS_TABLE set value_f = ? where name_f= ? and TIME_f= ? ";
// 运行sql发起请求
String param1 = "777";
String param2 = "D7.T7.P7";
String param3 = "2022-11-22 15:48:34";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, param1);
ps.setString(3, param2);
ps.setString(2, param3);
ps.executeUpdate();
System.out.println("Update historyData Success");
} catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println(e);
}
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
return;
}
return;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
try {
Class.forName("com.processdb.jdbc.JDBC");
Connection connection = null;
Statement statement = null;
try {
// 对数据库进行连接
connection = DriverManager.getConnection("jdbc:processdb://localhost:8301/instance01", "root", "root");
statement = connection.createStatement();
// 设置超时时间
statement.setQueryTimeout(30); // set timeout to 30 sec.
ResultSet rs = null;
// 查询sql
/**查询历史数据表 HIS_TABLE
* 所需字段
* name_f='public.sys.PDB_OS_CPU_USAGE' 数据点的位置(必填)
* time_f>'2022-10-26 00:00:00' and time_f<'2022-10-26 15:35:18'数据时间段
* 超限
* 可查询字段
* ID_F 数据id
* NAME_F 数据点的位置
* TIME_F 历史数据时间
* QUALITY_F 数据质量是否完好
* VALUE_F 历史数据值
*/
String sql = "select id_f,name_f,INTERVAL_F,time_f,QUALITY_F,value_f from his_table where name_f= ? and time_f> ? and time_f< ?";
// 填充sql所需要的参数
String param1 = "public.sys.PDB_OS_CPU_USAGE";
String param2 = "2022-10-26 00:00:00";
String param3 = "2022-10-26 14:33:50";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, param1);
ps.setString(2, param2);
ps.setString(3, param3);
// 运行sql发起请求
rs = ps.executeQuery();
// 查询结果存储在ResultSet(可以返回多条结果)
System.out.println("Query history data from table: his_table.");
while (rs.next()) {
// read the result set
System.out.println("id_f:" + rs.getString("id_f") + ",name_f:" + rs.getString("name_f")+ ",INTERVAL_F:" + rs.getString("INTERVAL_F")+ ",QUALITY_F" + rs.getString("QUALITY_F")+ ",TIME_F:" + rs.getString("time_f") + ",value_f:" + rs.getString("value_f")); }
System.out.println("Count: " + rs.getRow());
} catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println(e);
}
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
return;
}
return;
}
}
示例运行如下:
Query history data from table: his_table.
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:41.0,value_f:0.0
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:42.0,value_f:0.3071719
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:43.0,value_f:3.985534
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:44.0,value_f:1.1572248
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:45.0,value_f:1.2477847
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:46.0,value_f:4.986512
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:47.0,value_f:7.0197315
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:48.0,value_f:3.041393
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:49.0,value_f:7.414417
id_f:1001000001,name_f:PUBLIC.SYS.PDB_OS_CPU_USAGE,INTERVAL_F:0,QUALITY_FGOOD,TIME_F:2022-10-26 14:33:50.0,value_f:3.1563241
Count: 10