Skip to content

Commit 5518bd4

Browse files
authored
[spring-jdbc-col-count] resolving runtime error when using queryForList() (#18596)
1 parent 67bcb70 commit 5518bd4

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.spring.jdbc.queryforlist;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
@SpringBootApplication
6+
public class QueryForListDemoApplication {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE STUDENT_TBL
2+
(
3+
ID int NOT NULL PRIMARY KEY,
4+
NAME varchar(255),
5+
MAJOR varchar(255)
6+
);
7+
8+
INSERT INTO STUDENT_TBL VALUES (1, 'Kai', 'Computer Science');
9+
INSERT INTO STUDENT_TBL VALUES (2, 'Eric', 'Computer Science');
10+
INSERT INTO STUDENT_TBL VALUES (3, 'Kevin', 'Banking');
11+
INSERT INTO STUDENT_TBL VALUES (4, 'Liam', 'Law');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.spring.jdbc.queryforlist;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
13+
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
14+
import org.springframework.jdbc.core.BeanPropertyRowMapper;
15+
import org.springframework.jdbc.core.JdbcTemplate;
16+
import org.springframework.test.context.jdbc.Sql;
17+
18+
@JdbcTest
19+
@Sql(value = { "init-student-data.sql" }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
20+
class JdbcTemplateIntegrationTest {
21+
22+
@Autowired
23+
private JdbcTemplate jdbcTemplate;
24+
25+
@Test
26+
void whenUsingQueryForListToGetStudentList_thenGotException() {
27+
assertThrows(IncorrectResultSetColumnCountException.class, () -> jdbcTemplate.queryForList("SELECT * FROM STUDENT_TBL", Student.class));
28+
}
29+
30+
@Test
31+
void whenUsingQueryForListToSingleColumn_thenCorrect() {
32+
List<String> names = jdbcTemplate.queryForList("SELECT NAME FROM STUDENT_TBL", String.class);
33+
assertEquals(List.of("Kai", "Eric", "Kevin", "Liam"), names);
34+
35+
List<Integer> ids = jdbcTemplate.queryForList("SELECT ID FROM STUDENT_TBL", Integer.class);
36+
assertEquals(List.of(1, 2, 3, 4), ids);
37+
}
38+
39+
@Test
40+
void whenUsingQueryForListToGetMap_thenCorrect() {
41+
List<Map<String, Object>> nameMajorRowMaps = jdbcTemplate.queryForList("SELECT NAME, MAJOR FROM STUDENT_TBL");
42+
43+
// @formatter:off
44+
assertEquals(List.of(
45+
Map.of("NAME", "Kai", "MAJOR", "Computer Science"),
46+
Map.of("NAME", "Eric", "MAJOR", "Computer Science"),
47+
Map.of("NAME", "Kevin", "MAJOR", "Banking"),
48+
Map.of("NAME", "Liam", "MAJOR", "Law")
49+
), nameMajorRowMaps);
50+
// @formatter:on
51+
52+
List<Map<String, Object>> rowMaps = jdbcTemplate.queryForList("SELECT * FROM STUDENT_TBL");
53+
54+
// @formatter:off
55+
assertEquals(List.of(
56+
Map.of("ID", 1, "NAME", "Kai", "MAJOR", "Computer Science"),
57+
Map.of("ID", 2, "NAME", "Eric", "MAJOR", "Computer Science"),
58+
Map.of("ID", 3, "NAME", "Kevin", "MAJOR", "Banking"),
59+
Map.of("ID", 4, "NAME", "Liam", "MAJOR", "Law")
60+
), rowMaps);
61+
// @formatter:on
62+
}
63+
64+
@Test
65+
void whenUsingQueryWithRowMapperToGetStudentList_thenCorrect() {
66+
// @formatter:off
67+
List<Student> expected = List.of(
68+
new Student(1, "Kai", "Computer Science"),
69+
new Student(2, "Eric", "Computer Science"),
70+
new Student(3, "Kevin", "Banking"),
71+
new Student(4, "Liam", "Law")
72+
);
73+
// @formatter:on
74+
75+
List<Student> students = jdbcTemplate.query("SELECT * FROM STUDENT_TBL", new BeanPropertyRowMapper<>(Student.class));
76+
77+
assertEquals(expected, students);
78+
}
79+
80+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.spring.jdbc.queryforlist;
2+
3+
import java.util.Objects;
4+
5+
public class Student {
6+
7+
private Integer id;
8+
private String name;
9+
private String major;
10+
11+
public Student() {
12+
}
13+
14+
public Student(Integer id, String name, String major) {
15+
this.id = id;
16+
this.name = name;
17+
this.major = major;
18+
}
19+
20+
@Override
21+
public boolean equals(Object o) {
22+
if (!(o instanceof Student student)) {
23+
return false;
24+
}
25+
return Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(major, student.major);
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
return Objects.hash(id, name, major);
31+
}
32+
33+
public Integer getId() {
34+
return id;
35+
}
36+
37+
public void setId(Integer id) {
38+
this.id = id;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
public String getMajor() {
50+
return major;
51+
}
52+
53+
public void setMajor(String major) {
54+
this.major = major;
55+
}
56+
57+
58+
}

0 commit comments

Comments
 (0)