Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Generate Prepared Statement Inside For Loop

I have a SQL Server table which number of row and the column name generated by using loop. Now for the data insert, I need to generate Prepared Statement inside for loop based on number of rows. My first approach by using Switch Case.

int numberOfRows;
PreparedStatement statement;
Object[] rowObjects;


 switch (numberOfRows) {
      case 1 -> {
           statement.setString(1, String.valueOf(rowObjects[0]));
      }
      case 2 -> {
           statement.setString(1, String.valueOf(rowObjects[0]));
           statement.setString(2, String.valueOf(rowObjects[1]));
      }
      case 3 -> {
           statement.setString(1, String.valueOf(rowObjects[0]));
           statement.setString(2, String.valueOf(rowObjects[1]));
           statement.setString(3, String.valueOf(rowObjects[2]));
      }
 }

statement.executeUpdate();
                                            

As you can see it is highly ineffective. Is there a better way than this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your switch/case statement could be written something like this.

for (int index = 0; index < numberOfRows; index++) {
    statement.setString(index + 1, String.valueOf(rowObjects[index]);
} 
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!