Merge remote-tracking branch 'upstream/master' into feature/BAEL-6695-BooleanValidation

This commit is contained in:
Niket Agrawal 2023-09-17 22:38:30 +05:30
commit 58a74a6c80
16 changed files with 471 additions and 0 deletions

View File

@ -12,4 +12,5 @@ This module contains articles about core Java input and output (IO)
- [Getting a Files Mime Type in Java](https://www.baeldung.com/java-file-mime-type)
- [How to Avoid the Java FileNotFoundException When Loading Resources](https://www.baeldung.com/java-classpath-resource-cannot-be-opened)
- [Java Rename or Move a File](https://www.baeldung.com/java-how-to-rename-or-move-a-file)
- [Closing Java IO Streams](https://www.baeldung.com/java-io-streams-closing)
- [[More -->]](/core-java-modules/core-java-io-2)

View File

@ -3,4 +3,5 @@
- [Convert Integer to Hexadecimal in Java](https://www.baeldung.com/java-convert-int-to-hex)
- [Integer.class vs Integer.TYPE vs int.class](https://www.baeldung.com/java-integer-class-vs-type-vs-int)
- [Does Java Read Integers in Little Endian or Big Endian?](https://www.baeldung.com/java-integers-little-big-endian)
- [How to Split an Integer Number Into Digits in Java](https://www.baeldung.com/java-integer-individual-digits)
- More articles: [[<-- prev]](../core-java-numbers-5)

View File

@ -5,3 +5,4 @@ This module contains articles about performance of Java applications
### Relevant Articles:
- [External Debugging With JMXTerm](https://www.baeldung.com/java-jmxterm-external-debugging)
- [Create and Detect Memory Leaks in Java](https://www.baeldung.com/java-create-detect-memory-leaks)
- [Differences Between Heap Dump, Thread Dump and Core Dump](https://www.baeldung.com/java-heap-thread-core-dumps)

View File

@ -9,3 +9,4 @@
- [Invoke a Static Method Using Java Reflection API](https://www.baeldung.com/java-invoke-static-method-reflection)
- [What Is the JDK com.sun.proxy.$Proxy Class?](https://www.baeldung.com/jdk-com-sun-proxy)
- [Unit Test Private Methods in Java](https://www.baeldung.com/java-unit-test-private-methods)
- [Constructing Java Objects From Only the Class Name](https://www.baeldung.com/java-objects-make-using-class-name)

View File

@ -4,4 +4,5 @@ This module contains articles about core Java Security
### Relevant Articles:
- [Check if Certificate Is Self-Signed or CA-Signed With Java](https://www.baeldung.com/java-check-certificate-sign)
- [Extract CN From X509 Certificate in Java](https://www.baeldung.com/java-extract-common-name-x509-certificate)
- More articles: [[<-- prev]](/core-java-modules/core-java-security-3)

View File

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

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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');

View File

@ -0,0 +1,131 @@
package com.baeldung.spring.jdbc.replacedeprecated;
import com.baeldung.spring.jdbc.replacedeprecated.model.Student;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.test.context.jdbc.Sql;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@JdbcTest
@Sql(value = "student.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(value = "drop_student.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public class StudentDaoWithJdbcTemplateMethodsUnitTest {
private static final Logger logger = LoggerFactory.getLogger(StudentDaoWithJdbcTemplateMethodsUnitTest.class);
@Autowired
StudentDaoWithDeprecatedJdbcTemplateMethods studentDaoWithDeprecatedJdbcTemplateMethods;
@Autowired
StudentDaoWithPreferredJdbcTemplateMethods studentDaoWithPreferredJdbcTemplateMethods;
@Test
public void givenPreferredMethodQuery_whenArgsAgeAndGender_thenReturnStudents() {
List<Student> students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfAgeAndGender(4, "Female");
for (Student student: students) {
logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender());
}
assertEquals(6, students.size());
}
@Test
public void givenPreferredMethodQuery_whenArgsAgeGenderAndGrade_thenReturnStudents() {
List<Student> students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2);
for (Student student: students) {
logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender()
+ " Student grade: " + student.getGrade());
}
assertEquals(5, students.size());
}
@Test
public void givenDeprecatedMethodQuery_whenArgsAgeGenderAndGrade_thenReturnStudents() {
List<Student> students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2);
for (Student student: students) {
logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender()
+ " Student grade: " + student.getGrade());
}
assertEquals(5, students.size());
}
@Test
public void givenDeprecatedMethodQuery_whenArgsAgeAndGender_thenReturnStudents() {
List<Student> students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfAgeAndGender(4, "Female");
for (Student student: students) {
logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender());
}
assertEquals(6, students.size());
}
@Test
public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnStudents() {
List<Student> students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York");
for (Student student: students) {
logger.info("Student Name: " + student.getStudentName()
+ " Student grade: " + student.getStudentGender()
+ " Student State: " + student.getState());
}
assertEquals(6, students.size());
}
@Test
public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnStudents() {
List<Student> students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York");
for (Student student: students) {
logger.info("Student Name: " + student.getStudentName()
+ " Student grade: " + student.getStudentGender()
+ " Student State: " + student.getState());
}
assertEquals(6, students.size());
}
@Test
public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnCount() {
Integer count = studentDaoWithDeprecatedJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York");
logger.info("Total students of grade 1 from New York: " + count);
assertEquals(6, count);
}
@Test
public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnCount() {
Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York");
logger.info("Total students of grade 1 from New York: " + count);
assertEquals(6, count);
}
@Test
public void givenDeprecatedMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() {
Student student = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1);
assertEquals(1, student.getGrade());
assertEquals(4, student.getStudentId());
logger.info("Student ID: " + student.getStudentId()
+ " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade());
}
@Test
public void givenPreferredMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() {
Student student = studentDaoWithPreferredJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1);
assertEquals(1, student.getGrade());
assertEquals(4, student.getStudentId());
logger.info("Student ID: " + student.getStudentId()
+ " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade());
}
@Test
public void givenPreferredMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() {
Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2);
assertEquals(6, count);
logger.info("Total number of Female Students: " + count);
}
@Test
public void givenDeprecatedMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() {
Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2);
assertEquals(6, count);
logger.info("Total number of Female Students: " + count);
}
}

View File

@ -6,3 +6,4 @@ This module contains articles about Mockito
- [Mocking a Singleton With Mockito](https://www.baeldung.com/java-mockito-singleton)
- [Resolving Mockito Exception: Wanted But Not Invoked](https://www.baeldung.com/mockito-exception-wanted-but-not-invoked)
- [Matching Null With Mockito](https://www.baeldung.com/mockito-match-null)
- [Mock Same Method with Different Parameters](https://www.baeldung.com/java-mock-same-method-other-parameters)