forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#14757 from parthiv39731/PR-6880
BAEL-6880, How to replace deprecated jdbcTemplate.queryForObject and …
- Loading branch information
Showing
10 changed files
with
465 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...rc/main/java/com/baeldung/spring/jdbc/replacedeprecated/ReplaceDeprecatedApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.baeldung.spring.jdbc.replacedeprecated; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.ComponentScan; | ||
|
||
@SpringBootApplication | ||
@ComponentScan(basePackages = "com.baeldung.spring.jdbc.replacedeprecated") | ||
public class ReplaceDeprecatedApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ReplaceDeprecatedApplication.class, args); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...m/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithDeprecatedJdbcTemplateMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.baeldung.spring.jdbc.replacedeprecated; | ||
|
||
import com.baeldung.spring.jdbc.replacedeprecated.model.Student; | ||
import com.baeldung.spring.jdbc.replacedeprecated.model.StudentResultExtractor; | ||
import com.baeldung.spring.jdbc.replacedeprecated.model.StudentRowMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowCountCallbackHandler; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class StudentDaoWithDeprecatedJdbcTemplateMethods { | ||
JdbcTemplate jdbcTemplate; | ||
|
||
@Autowired | ||
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public List<Student> getStudentsOfAgeAndGender(Integer age, String gender) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ?"; | ||
Object[] args = {age, gender}; | ||
return jdbcTemplate.query(sql, args, new StudentRowMapper()); | ||
} | ||
|
||
public List<Student> getStudentsOfAgeGenderAndGrade(Integer age, String gender, Integer grade) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ? and grade = ?"; | ||
Object[] args = {age, gender, grade}; | ||
return jdbcTemplate.query(sql, args, new StudentRowMapper()); | ||
} | ||
|
||
public List<Student> getStudentsOfGradeAndState(Integer grade, String state) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; | ||
Object[] args = {grade, state}; | ||
return jdbcTemplate.query(sql, args, new StudentResultExtractor()); | ||
} | ||
|
||
public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where student_id = ? and grade = ?"; | ||
Object[] args = {studentID, grade}; | ||
|
||
return jdbcTemplate.queryForObject(sql, args, new StudentRowMapper()); | ||
} | ||
|
||
public Integer getCountOfStudentsInAGradeFromAState(Integer grade, String state) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; | ||
Object[] args = {grade, state}; | ||
RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler(); | ||
jdbcTemplate.query(sql, args, countCallbackHandler); | ||
return countCallbackHandler.getRowCount(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...om/baeldung/spring/jdbc/replacedeprecated/StudentDaoWithPreferredJdbcTemplateMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.baeldung.spring.jdbc.replacedeprecated; | ||
|
||
import com.baeldung.spring.jdbc.replacedeprecated.model.Student; | ||
import com.baeldung.spring.jdbc.replacedeprecated.model.StudentResultExtractor; | ||
import com.baeldung.spring.jdbc.replacedeprecated.model.StudentRowMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowCountCallbackHandler; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class StudentDaoWithPreferredJdbcTemplateMethods { | ||
JdbcTemplate jdbcTemplate; | ||
|
||
@Autowired | ||
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public List<Student> getStudentsOfAgeAndGender(Integer age, String gender) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ?"; | ||
return jdbcTemplate.query(sql, new StudentRowMapper(), age, gender); | ||
} | ||
|
||
public List<Student> getStudentsOfAgeGenderAndGrade(Integer age, String gender, Integer grade) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where age= ? and gender = ? and grade = ?"; | ||
return jdbcTemplate.query(sql, new StudentRowMapper(), age, gender, grade); | ||
} | ||
|
||
public List<Student> getStudentsOfGradeAndState(Integer grade, String state) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; | ||
return jdbcTemplate.query(sql, new StudentResultExtractor(), grade, state); | ||
} | ||
|
||
public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where student_id = ? and grade = ?"; | ||
|
||
return jdbcTemplate.queryForObject(sql, new StudentRowMapper(), studentID, grade); | ||
} | ||
|
||
public Integer getCountOfGenderInAGrade(String gender, Integer grade) { | ||
String sql = "select count(1) as total from student where gender = ? and grade = ?"; | ||
|
||
return jdbcTemplate.queryForObject(sql, Integer.class, gender, grade); | ||
} | ||
|
||
public Integer getCountOfStudentsInAGradeFromAState(Integer grade, String state) { | ||
String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?"; | ||
|
||
RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler(); | ||
jdbcTemplate.query(sql, countCallbackHandler, grade, state); | ||
return countCallbackHandler.getRowCount(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...s/spring-jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.baeldung.spring.jdbc.replacedeprecated.model; | ||
|
||
public class Student { | ||
private Integer studentId; | ||
private String studentName; | ||
private String studentGender; | ||
private Integer age; | ||
private Integer grade; | ||
|
||
public Integer getStudentId() { | ||
return studentId; | ||
} | ||
|
||
public void setStudentId(Integer studentId) { | ||
this.studentId = studentId; | ||
} | ||
|
||
public String getStudentName() { | ||
return studentName; | ||
} | ||
|
||
public void setStudentName(String studentName) { | ||
this.studentName = studentName; | ||
} | ||
|
||
public String getStudentGender() { | ||
return studentGender; | ||
} | ||
|
||
public void setStudentGender(String studentGender) { | ||
this.studentGender = studentGender; | ||
} | ||
|
||
public Integer getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(Integer age) { | ||
this.age = age; | ||
} | ||
|
||
public Integer getGrade() { | ||
return grade; | ||
} | ||
|
||
public void setGrade(Integer grade) { | ||
this.grade = grade; | ||
} | ||
|
||
public String getState() { | ||
return state; | ||
} | ||
|
||
public void setState(String state) { | ||
this.state = state; | ||
} | ||
|
||
private String state; | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...rc/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentResultExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.baeldung.spring.jdbc.replacedeprecated.model; | ||
|
||
import org.springframework.jdbc.core.ResultSetExtractor; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class StudentResultExtractor implements ResultSetExtractor<List<Student>> { | ||
@Override | ||
public List<Student> extractData(ResultSet rs) throws SQLException { | ||
List<Student> students = new ArrayList<Student>(); | ||
while(rs.next()) { | ||
Student student = new Student(); | ||
student.setStudentId(rs.getInt("student_id")); | ||
student.setStudentName(rs.getString("student_name")); | ||
student.setAge(rs.getInt("age")); | ||
student.setStudentGender(rs.getString("gender")); | ||
student.setGrade(rs.getInt("grade")); | ||
student.setState(rs.getString("state")); | ||
students.add(student); | ||
} | ||
return students; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...jdbc/src/main/java/com/baeldung/spring/jdbc/replacedeprecated/model/StudentRowMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.baeldung.spring.jdbc.replacedeprecated.model; | ||
|
||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class StudentRowMapper implements RowMapper<Student> { | ||
@Override | ||
public Student mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
Student student = new Student(); | ||
student.setStudentId(rs.getInt("student_id")); | ||
student.setStudentName(rs.getString("student_name")); | ||
student.setAge(rs.getInt("age")); | ||
student.setStudentGender(rs.getString("gender")); | ||
student.setGrade(rs.getInt("grade")); | ||
student.setState(rs.getString("state")); | ||
return student; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# DataSource Configuration | ||
spring.datasource.url=jdbc:h2:mem:testdb | ||
spring.datasource.driverClassName=org.h2.Driver | ||
spring.datasource.username=user | ||
spring.datasource.password= # Leave this empty |
1 change: 1 addition & 0 deletions
1
...pring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/drop_student.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE student; |
98 changes: 98 additions & 0 deletions
98
...les/spring-jdbc/src/main/resources/com/baeldung/spring/jdbc/replacedeprecated/student.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
|
||
CREATE TABLE student ( | ||
student_id INT AUTO_INCREMENT PRIMARY KEY, | ||
student_name VARCHAR(255) NOT NULL, | ||
age INT, | ||
grade INT NOT NULL, | ||
gender VARCHAR(10) NOT NULL, | ||
state VARCHAR(100) NOT NULL | ||
); | ||
-- Student 1 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('John Smith', 18, 3, 'Male', 'California'); | ||
|
||
-- Student 2 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emily Johnson', 17, 2, 'Female', 'New York'); | ||
|
||
-- Student 3 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Michael Davis', 4, 1, 'Male', 'Texas'); | ||
|
||
-- Student 4 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Martinez', 2, 1, 'Female', 'Florida'); | ||
|
||
-- Student 5 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('William Brown', 5, 5, 'Male', 'California'); | ||
|
||
-- Student 6 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Garcia', 4, 2, 'Female', 'Texas'); | ||
|
||
-- Student 7 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ethan Rodriguez', 3, 1, 'Male', 'New York'); | ||
|
||
-- Student 8 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Hernandez', 2, 1, 'Female', 'Florida'); | ||
|
||
-- Student 9 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('James Wilson', 5, 4, 'Male', 'Texas'); | ||
|
||
-- Student 10 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emma Miller', 3, 1, 'Female', 'California'); | ||
|
||
-- Student 11 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Benjamin Brown', 4, 1, 'Male', 'New York'); | ||
|
||
-- Student 12 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Mia Smith', 2, 1, 'Female', 'Florida'); | ||
|
||
-- Student 13 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Daniel Johnson', 5, 4, 'Male', 'California'); | ||
|
||
-- Student 14 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Davis', 4, 2, 'Female', 'Texas'); | ||
|
||
-- Student 15 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Matthew Martinez', 3, 1, 'Male', 'New York'); | ||
|
||
-- Student 16 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Taylor', 2, 1, 'Female', 'Florida'); | ||
|
||
-- Student 17 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Alexander White', 5, 4, 'Male', 'California'); | ||
|
||
-- Student 18 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Johnson', 4, 2, 'Female', 'Texas'); | ||
|
||
-- Student 19 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Christopher Lee', 3, 1, 'Male', 'New York'); | ||
|
||
-- Student 20 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emma Wilson', 2, 1, 'Female', 'Florida'); | ||
|
||
-- Student 21 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Elijah Smith', 5, 3, 'Male', 'Texas'); | ||
|
||
-- Student 22 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Isabella Davis', 4, 2, 'Female', 'California'); | ||
|
||
-- Student 23 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Liam Johnson', 3, 1, 'Male', 'New York'); | ||
|
||
-- Student 24 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Garcia', 2, 1, 'Female', 'Florida'); | ||
|
||
-- Student 25 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Noah Rodriguez', 5, 3, 'Male', 'Texas'); | ||
|
||
-- Student 26 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Sophia Hernandez', 4, 2, 'Female', 'California'); | ||
|
||
-- Student 27 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Mason Smith', 3, 1, 'Male', 'New York'); | ||
|
||
-- Student 28 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Ava Taylor', 2, 1, 'Female', 'Florida'); | ||
|
||
-- Student 29 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('William Brown', 5, 5, 'Male', 'Texas'); | ||
|
||
-- Student 30 | ||
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Olivia Martinez', 4, 4, 'Female', 'California'); |
Oops, something went wrong.