I have a H2 database with bootstrapped data, and I would like to output a specific row based off a user selection. Right now in attempting to navigate to the desired page in order to run the script and let the user select their choice, it leads to an HTML 500 error (see attached screenshot).
Navigating to Page Error
Within my application.properties file, I have the following in order to call the desired database:
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:348dae81-3b35-422e-8aa6-eb69a6163544
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Within my characterCreator.html file which has the Select element, I have the following to populate the choices:
<div style="margin:auto; text-align: center;">
<form action="/characterCreator" method="get">
<select style="align-self: center;" th:field="*{id}">
<option th:each="race : ${RACE}"
th:value="${race.ID}"
th:text="${race.RACE_NAME}">Select Race
</option>
</select>
<br><br>
<input type="submit" value="Select">
</form>
</div>
And finally, since I am using SpringBoot I have an outside controller named RaceController.java, which has the same @RequestMapping as I desire for the output to appear in the same file as characterCreator.html.
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/characterCreator")
public class RaceController {
private final RaceRepository raceRepository;
public RaceController(RaceRepository raceRepository) {
this.raceRepository = raceRepository;
}
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/{id}")
public String getRace(@PathVariable int id) {
String query = "SELECT * FROM RACE WHERE ID = ?";
List<Map<String, Object>> rows = jdbcTemplate.queryForList(query, id);
return rows.get(0).get("ID").toString();
}
}
For reference, here is what the table appears as in localhost:8080/h2-console. I have also ensured that the query is written correctly.
h2 database
h2 database ID query