 <p>you can do on a other way: </p> <p><strong>Create a Select with this Concats</strong></p> <pre><code>select GROUP_CONCAT( CONCAT(" ('",field1,"','",field2,"')")) as vals from my_table; </code></pre> <p>The Result look like this:</p> <p><strong>Result</strong></p> <pre><code>mysql&gt; select GROUP_CONCAT( -&gt; CONCAT(" ('",field1,"','",field2,"')")) as vals -&gt; from my_table; +---------------------------------------------------------------------------------------------------------+ | vals | +---------------------------------------------------------------------------------------------------------+ | ('O1','AC'), ('O1','PT'), ('O2','PT'), ('O3','MI'), ('O3','PT'), ('O4','EG'), ('O4','PT'), ('O5','PT') | +---------------------------------------------------------------------------------------------------------+ 1 row in set (0,00 sec) mysql&gt; </code></pre> <p>and the you can direct concat this in the insert statement and you only read one row and only write and execute one statement.</p> <p><strong>your Code</strong></p> <pre><code> String selectStatement = "SELECT GROUP_CONCAT(CONCAT(" ('",field1,"','",field2,"')")) as vals FROM dbx.testx where time between ('2016-09-01 00:00:00') and ('2016-09-03 23:59:59');"; ResultSet resultSetForSelect = st.executeQuery(selectStatement); String insertStatement = "INSERT INTO testy(" + "field1," + "field2)" + + String from result + "ON DUPLICATE KEY UPDATE field1 = VALUES(field1);"; &lt;----field1 is a unique key but not primary # execute one time </code></pre>