0

HI everyone I was wondering if there is a specific way how you can execute multiple INSERT queries with minimum code so I don't repeat the INSERT procedure every time.

For example I'm doing a workout feature for a program and the way it works is a user can select 3 exercises for each muscle group and than I'm having a table in my database where I want to store what the user have selected.

I want to execute all of the inserts by clicking just one button.

Here you can see screenshot of my program for your illustration.

https://i.sstatic.net/GYBny.jpg

2
  • Use addBatch & executeBatch commands for executing multiple SQL statment. Good Example Commented Apr 2, 2014 at 17:12
  • Thanks, I will take a look at this as well and than decide whether I should use this or a loop like rrirower suggested. Commented Apr 2, 2014 at 17:14

2 Answers 2

1

If you are using JDBC, you should check the addBatch method. Here is a good tutorial for efficient inserts: http://viralpatel.net/blogs/batch-insert-in-java-jdbc/

I would recommend the SQL injection safe mechanism.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I think the addBatch method is just what I needed, thank you for the nice tutorial
0

Depending on how you structure your code, you should be able to construct a loop that collects the necessary data and dynamically creates the sql insert statement. In your example, the loop would execute three times, each time just changing the values of the columns to be inserted.

2 Comments

Can you just explain a little more what do you mean by "dynamically creates the sql insert statement"
You can use prepared statements to accomplish that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.