diff --git a/cloud-modules/cloud-modules-template/src/main/java/com/template/service/impl/TemplateServiceImpl.java b/cloud-modules/cloud-modules-template/src/main/java/com/template/service/impl/TemplateServiceImpl.java index 0fe5e0d..0b21cc4 100644 --- a/cloud-modules/cloud-modules-template/src/main/java/com/template/service/impl/TemplateServiceImpl.java +++ b/cloud-modules/cloud-modules-template/src/main/java/com/template/service/impl/TemplateServiceImpl.java @@ -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; /** * @Author:liuxinyue @@ -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(); + } + } + + }