2

I would like to execute this query :

    String query = "delete from table_a where id_table_a = ? and id_entity in ()";
    PreparedStatement prepare = db_connection.prepareStatement(query);
    prepare.setInt(1, var_a);

In the SQL query, I'm trying to put between the parentheses an attribute (defined as "id") of objects (defined as "Entity"). Objects "Entity" are contained in an ArrayList.

My goal is to put in a string all the "Entity.id" separated by commas. And then I will put this string between parentheses in my query.

I'd like to have advice cause I don't know if it's the better way to realize it...

0xh3xa
  • 4,801
  • 2
  • 14
  • 28

1 Answers1

1

Try this one:

List<String> idEntities = new ArrayList<>();

// Create an array of idEntities
Array arrIdEntities = db_connection.createArrayOf("string", idEntities.toArray());

// Added a placeholder '?' in the IN clause.
String query = "delete from table_a where id_table_a = ? and id_entity in (?)";
PreparedStatement prepare = db_connection.prepareStatement(query);
prepare.setInt(1, var_a);
prepare.setArray(2, arrIdEntities); // Assign the array of entities to the 2nd parameter.

return result;

Please see explanations in the comments.

Mhel
  • 50
  • 1
  • 7