Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda