Step 1: Add the Spring JDBC dependency in your pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Step 2: Configure your database in the application.properties
spring.datasource.url=jdbc:mysql://localhost/prodDB
spring.datasource.username=prodUser
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Step 3: Create JDBCTemplete @Bean
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
Step 3: Create Table in your Database & Insert few records
CREATE TABLE `employee` (
`employeeId` int(5) NOT NULL,
`employeeName` varchar(255) NOT NULL,
`employeeDateOfBirth` date NOT NULL,
`employeeDepartment` varchar(255) NOT NULL,
`employeeJoiningDate` date NOT NULL,
`employeeSalary` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
COMMIT;
insert into employee values(101,'Mike','2001-04-11','IT','2021-01-01',24000);
Step 4: Create @Repository Class with Update Query
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class AppRepository {
private final JdbcTemplate jdbcTemplate;
public MyRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
Step 5: Create Update Query with Parameters
private String empName="Jose";
private String empDept="Finance";
private int empId=101;
public void updateRecord(String name, int age, long id) {
String updateQuery = "UPDATE employee SET employeeName = ?, employeeDepartment = ? WHERE employeeId = ?";
jdbcTemplate.update(updateQuery, empName, empDept, empId);
}
You can also make use of the named parameters instead of the ? in the query.
Example:public void updateRecord(String name, int age, long id) {
String updateQuery = "UPDATE employee SET employeeName = :empName, employeeDepartment = :empDept WHERE employeeId = :empId";
Map<String, Object> params = new HashMap<>();
params.put("empName", empName);
params.put("employeeDepartment", empDept);
params.put("employeeId", empId);
jdbcTemplate.update(updateQuery, params);
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- jQuery: Check if an element exists or not - jQuery
- [Solution] Spring Tool Suite (STS) support for JSP (JAVA EE) - Eclipse
- Install Python on Alpine Linux - Docker - Docker
- YAML Parser using Java Jackson Library Example - Java
- How to Configure GitHub with Eclipse IDE in 2023 - Eclipse
- Java 8: Steam map with Code Examples - Java
- How to pretty print HTML using Java Code - Html
- [Fix] Microsoft Windows OneDrive 0x8007018b Error Code - Windows