Such a task: I have an array of type String containing client codes that I get with the getCodeClient method.

Then in the oracle database I need to get a table with the parameters of these clients, the table is based on the codes obtained above.

I’m supposed to convert the array to this list {'code012', 'code876', 'code123', etc.}

How to transfer this list to jdbc to use later in the request?

  • And SELECT * FROM "TABLE_NAME" WHERE CODE="code012" did not try? - GardenMan
  • I need to immediately press the entire array of strings - Padawan
  • Then SELECT * FROM "TABLE_NAME" WHERE CODE="code012" or CODE="code876" ... Get all the strings from the database - GardenMan
  • It turns out you need to manually register CODE = "code012" or CODE = "code876" ... or ... or ... or? - Padawan
  • one
    Can be used by streaming or StringBuilder - GardenMan

2 answers 2

Forced to correct previous speakers. Everything is true except for one, the formation of almost identical requests without the use of PreparedStatement is anti-pattern.

I explain. PreparedStatement request to precompile a stable SQL expression, so the next time the DBMS is called, it will not recompile the SQL query, but will only change the call parameters by retrieving the query from its cache, which is much faster in time + bonus protection from SQL injection

Closer to the code:

 String selectSQL = "SELECT * FROM TABLE_NAME WHERE CODE = ?"; String whereClause="blah-blah"; PreparedStatement preparedStatement = dbConnection.prepareStatement(selectSQL); 

And now it is already possible to form the string whereClause method proposed by StringJoiner through StringJoiner or StringBuilder substituting it in PreparedStatement and calling the actual request itself (without changing the PreparedStatement object each time):

 preparedStatement.setString(1, whereClause); ResultSet rs = preparedStatement.executeQuery(selectSQL ); 
  • Agree on PreparedStatement . +1 - GardenMan
  • @Barmaley I understand the problem is that the author does not know how many codes will come into the method and he needs a dynamic query. If 1 code has come then search by 1, if 3 then search by 3. Can a PreparedStatement do this? - Victor

based on a sentence from a comment:

 public Client getClient(String... params){ StringBuilder query = new StringBuilder("SELECT * FROM \"TABLE_NAME\""); StringJoiner join = new StringJoiner(" or "); for(String param : params){ join.add(param); } String queryParams = join.toString(); if (!queryParam.isEmpty()){ query.append(" WHERE "); query.append(queryParams); } ...//дальше логика вашего запроса, а формирование запроса можно //вынести в отдельный приватный метод. }