连接IoTDB数据库

dev
Number7 2024-09-21 21:03:44 +08:00
parent 93cca647a4
commit 5da94ff43a
1 changed files with 42 additions and 3 deletions

View File

@ -7,6 +7,8 @@ import com.template.service.MessageTemplateTypeService;
import com.template.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.*;
import java.util.List;
/**
* @Authorliuxinyue
@ -75,11 +77,48 @@ public class TemplateServiceImpl implements TemplateService{
messageTemplate.setLatitude(result.substring(startIndex, endIndex-1));
}
}
System.out.println("解析后的报文是:"+messageTemplate);
}
}
//连接IoTDB数据库并执行插入操作
public void addIoTDBTemplate(MessageTemplate messageTemplate) throws ClassNotFoundException {
// IoTDB 数据库的连接信息
String url = "jdbc:iotdb://47.116.173.119:6667/";
String user = "root";
String password = "root";
// 注册 JDBC 驱动
Class.forName("org.apache.iotdb.jdbc.IoTDBDriver");
System.out.println("JDBC Driver Registered!");
try {
// 注册 JDBC 驱动
Class.forName("org.apache.iotdb.jdbc.IoTDBDriver");
System.out.println("JDBC Driver Registered!");
// 数据库连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Connected to IoTDB!");
// 插入数据的 SQL 语句
String insertSQL = "INSERT INTO root.four.message_template ( vin_code, time_stamp, long_itude, latitude, speed_vehicle, total_mileage, total_voltage, combined_current, insulation_resistance, " +
"gear_position, accelerator_pedal_travel_value, brake_pedal_travel_value, specific_fuel_consumption, motor_controller_temperature, motor_speed, motor_torque, motor_temperature, motor_voltage, motor_current, " +
"power_battery_remaining_soc, maximum_feedback_power, maximum_discharge_power, bms_self_check_counter, power_battery, total_voltage_load_side, maximum_voltage, minimum_voltage, maximum_temperature, minimum_temperature," +
" available_capacity, vehicle_status, charging_state, operational_status, soc, energy_storage_devices," +
" drive_motor_condition, whether_works, eas, ptc, eps, abs, mcu, heating_state, power_battery_status, state_battery_insulation, dcdc, chg, check_digit, cutoff_bit) VALUES ( ?, ?, ?, ?, ?, ?, ?, " +
"?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ??, ?, ?, ?, ?, ?, ?, ?, ?, " +
"?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
// 使用 PreparedStatement 插入数据
PreparedStatement preparedStatement = connection.prepareStatement(insertSQL);
// 执行插入
preparedStatement.execute();
System.out.println("Data inserted successfully!");
// 关闭连接
preparedStatement.close();
connection.close();
} catch (ClassNotFoundException e) {
System.err.println("JDBC Driver not found: " + e.getMessage());
} catch (SQLException e) {
e.printStackTrace();
}
}
}