'insert several records simultaneously with java and mysql

Dear Community a pleasure to greet you, I am reading several HoldinRegister type registers for that I am using easymodbus in total will be 150 registers that must be read and saved each time a Coil type register changes from 0 to 1, the reading of these registers is done as follows:

public static void main(String[] args) throws Exception
{
    ModbusClient modbusClient = new ModbusClient();
    modbusClient.Connect("127.0.0.1", 502);
    int[] inputRegisters = modbusClient.ReadHoldingRegisters(0, 149);
    for (int i=0; i < inputRegisters.length; i++)
        System.out.println("Holding Register  #"+i+": "+inputRegisters[i]);
}

The values of the registers are updated only when the Coil changes its value, then save those registers in the database, I think this is better because if they would be updating all the time it would consume too many resources.

My question now is how can I do to save that amount of data at the same time, searching the net I found an example using FOR but I don't understand very well how to integrate it since I can't make it work.

String SQL = "INSERT INTO ingredients(date,regnumb,regval)"
    + "values (?,?,?)";

    mensaje="data inserted correctly";
    try{
        PreparedStatement sqls = (PreparedStatement) conn.prepareStatement(SQL);
       
        int rows =4;
     
             
        for(int i = 0; i
        sqls.setString(1, date);
        sqls.setString(2, regnumb);          
        sqls.setString(5, regval);
        sqls.addBatch();

} sqls.executeBatch();

thank you very much for your attention



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source