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 params = new HashMap<>();
params.put("empName", empName);
params.put("employeeDepartment", empDept);
params.put("employeeId", empId);
jdbcTemplate.update(updateQuery, params);
}
Have Questions? Post them here!
- Error: Can not find the tag library descriptor for
- Create a Database Table using JDBC PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- Java Jackson with Maven Example
- [fix] Java JDBC ConnectException: Connection refused
- Spring Boot: Transactions Management with JDBCTemplate Example
- Java Get Current Date for a Specific Time Zone
- What are the 8 Primitive Data Types in Java
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Maven Eclipse (M2e) No archetypes currently available
- How to Sort a LinkedList in Java
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Split a String in Java with Examples
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Truncate table using Java JDBC Example
- Java: Generate random numbers within a range
- Parse XML file in Java using DOM Parser
- How to get Client IP address using Java Code Example
- JDBCTemplate Querying Examples with Spring Boot 3
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- String Boot + Redis - SET and GET String Commands Examples
- Setting up Spring Boot 3 + Maven + MySQL + JDBC Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- Java Split String by Spaces
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Okta Hacked - Source Code Stolen from GitHub Repo - News
- Trigger Notification Center Message using Mac Terminal Command - MacOS
- Convert Java Object to YAML using Jackson Library - Java
- Sort a List using Java 8 Stream Examples - Java
- How to take a screenshot on Microsoft Windows OS on PC or Laptop - Microsoft
- [Docker M1/M2 Mac] qemu-x86_64: Could not open /lib64/ld-linux-x86-64.so.2: No such file or directory AWS CLI - Docker
- Android : Duplicate registration for activity com.example.abc - Android
- Perform an Empty Commit in Git without anything in Staging Area - Git