I have a table where I can select a single entry like this:
SELECT * FROM `table` WHERE `id` = 1;
I know that I could easily select entries by multiple ids like this:
SELECT * FROM `table` WHERE `id` IN (1,2,3);
But now I have implemented this in Java with MariaDB/J Connector, using java.sql
classes:
public ArrayDeque<DataType> getItems(long... ids) {
ArrayDeque<DataType> deque = new ArrayDeque<DataType>();
PreparedStatement st = null;
ResultSet resultSet = null;
try {
st = getConnection().prepareStatement("SELECT * FROM `table` WHERE `id` = ?;");
st.setLong(1, id);
resultSet = st.executeQuery();
while (resultSet.next()) {
deque.add(parseResultSetToDataType(resultSet));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(resultSet, st);
}
return deque;
}
Now, basically I need to put a ?
for every variable I want to insert. Since the amount of selected Ids can vary, I can't put a static IN (?,?,?)
there. The dirty solution that comes to my mind is generate (?,?,?)
based on the size of long... ids
-- but is there a better, 'cleaner' solution to this?