Skip to content

Instantly share code, notes, and snippets.

@dmagda
Created May 16, 2023 16:35
Show Gist options
  • Save dmagda/0f3e92b8e972ed75398124dd42deea21 to your computer and use it in GitHub Desktop.
Save dmagda/0f3e92b8e972ed75398124dd42deea21 to your computer and use it in GitHub Desktop.
Generating Query with StringBuilder
                StringBuilder queryString = "insert into test (id, f1) values ";
                
                // add placeholder for values
                for (int counter = 0; counter <= 100; counter += 1) 
                    queryString.append("(?, ?),");

                // replace the trailing ',' with a ';'
                queryString.replace(queryString.length() - 1, queryString.length(), ";");

                PreparedStatement prepared_statement = connection.prepareStatement(queryString.toString());
                
                // bind the values
                for (int counter = 0; counter <= 100; counter += 1) {
                    prepared_statement.setInt((counter * 2) + 1, counter);
                    prepared_statement.setString((counter * 2) + 2, "A");
                }
                
                prepared_statement.execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment