Home > Software design >  How do I use JDBC Template to query a "Select where first letter starts with" Spring boot
How do I use JDBC Template to query a "Select where first letter starts with" Spring boot

Time:02-16

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;
}

CodePudding user response:

You can use the LIKE operator.

public List<String> getAllUserNames() {
    String firstLetter = "j";
    List<String> usernameList = new ArrayList<>();
    usernameList.addAll(jdbcTemplate.queryForList(String.format("SELECT FirstName from patients where first_name LIKE %s%", firstLetter), String.class));
    return usernameList;
}

CodePudding user response:

You can use the LIKE keyword for this.

Select name from paitents where name LIKE 'a%';

https://www.mysqltutorial.org/mysql-like/

  • Related