insert several records simultaneously with java and mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ame5577
    New Member
    • Apr 2022
    • 1

    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:

    Code:
    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.

    Code:
    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
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    I haven't worked with Modbus communication or its libraries. I can provide generic advice. Proceed if it sounds useful to you.


    The values of the registers are updated only when the Coil changes its value, then save those registers in the database
    How does the 'Coil' change its value? If there's an event listener provided in the library, the code to update the database can be put in the function.

    Code:
    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();
    I see syntax errors.

    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.
    What do you want to 'integrate' with what? What isn't working? What errors do you see? You may want to mention the specific database schema or columns and what updates are to be made and where.

    Comment

    Working...