Spring jdbcTemplate with MySQL
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sks</groupId>
<artifactId>autowiring-examples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
</project>
Book.java
package com.sks.db;
public class Book {
private int id;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Book(int id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
}
public Book() {
super();
// TODO Auto-generated constructor stub
}
}
Configuration class
package com.sks.db;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
@ComponentScan(basePackages = "com.sks.db")
public class AppConfig {
@Bean
public DataSource dataSource1() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/saras_db");
dataSource.setUsername("root");
dataSource.setPassword("<password>");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource1());
}
@Bean("bookDAO")
public BookDAO bookDAO() {
BookDAO bookDAO = new BookDAO();
return bookDAO;
}
}
DAO class
package com.sks.db;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
public class BookDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
public boolean saveBookRecord(Book book) {
boolean result = false;
String query = "insert into Book values(" + book.getId() + ", '"
+ book.getName() + "', " + book.getPrice() + ")";
int rows = jdbcTemplate.update(query);
if(rows > 0) {
System.out.println("The book data has been saved successfully.");
result = true;
}
else {
System.out.println("The insertion is not successful");
}
return result;
}
}
Main class
package com.sks.db;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Testing {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
BookDAO dao = (BookDAO)context.getBean("bookDAO");
dao.saveBookRecord(new Book(101, "Learn Java in 21 days", 400));
dao.saveBookRecord(new Book(102, "The White Tiger", 300));
dao.saveBookRecord(new Book(103, "Over my dead body", 500));
dao.saveBookRecord(new Book(104, "Lovely Revolution", 350));
System.out.println("All the transaction completed successfully.");
((AnnotationConfigApplicationContext)context).close();
}
}
Output :

Database output :
