'How do I use JDBC Template to query a "Select where first letter starts with" Spring boot
I want to create a query Select name from paitents where name [starts with a] using JDBC template. I have managed to create a query that selects everything as you can see from below.
public List<String> getAllUserNames() {
List<String> usernameList = new ArrayList<>();
usernameList.addAll(jdbcTemplate.queryForList("SELECT FirstName from paitents", String.class));
return usernameList;
}
Solution 1:[1]
You can use the LIKE keyword for this.
Select name from paitents where name LIKE 'a%';
Solution 2:[2]
You can use the LIKE operator.
public List<String> getAllUserNames(String firstLetter) {
List<String> usernameList = new ArrayList<>();
usernameList.addAll(jdbcTemplate.queryForList(String.format("SELECT FirstName from patients where first_name LIKE %s%s", firstLetter, "%"), String.class));
return usernameList;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user1738539 |
| Solution 2 |
