Spring jdbc prepared statement

PreparedStatementCallback interface:

The PreparedStatementCallback is a generic callback interface for code that operates on a PreparedStatement. Allows to execute any number of operations on a single PreparedStatement i.e. a single executeUpdate call or repeated executeUpdate calls with varying parameters.

We pass the PreparedStatementCallback’s instance in the execute method for executing parameterized queries.

Syntax:

public T execute(String sql,PreparedStatementCallback<T>);

Where T represents the return type.

Method of PreparedStatementCallback interface:

public T doInPreparedStatement(PreparedStatement ps)throws SQLException, DataAccessException

Example

package com.w3schools360.SpringDB;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringDbApplication {
	private static ApplicationContext applicationContext;
    public static void main( String[] args ) {
    	applicationContext = SpringApplication.run(SpringDbApplication.class, args);  
    	performDBOperation();
    }
    
    private static void performDBOperation(){
    	JDBCController jdbcController = (JDBCController) applicationContext.getBean("jDBCController");
    	Users user = new Users();
    	user.setSeq(15);
    	user.setUsername("Vishal");
    	user.setPassword("v123");
    	jdbcController.insertData(user);
    }
}
package com.w3schools360.SpringDB;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Repository;

@Repository("jDBCController")
public class JDBCController {
	@Autowired  
        JdbcTemplate jdbc;  
	
	public void insertData(Users user){  
		String query="insert into users values(?,?,?)";  
		jdbc.execute(query,new PreparedStatementCallback(){  
		    @Override  
		    public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException {  	              
		        ps.setInt(1,user.getSeq());  
		        ps.setString(2,user.getUsername());  
		        ps.setString(3,user.getPassword());  
		              
		        return ps.execute();  	              
		    }  
		});    
        System.out.println("Data inserted Successfully");  
    }
}
package com.w3schools360.SpringDB;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity  
@Table(name= "USERS") 
public class Users {
	private int seq;
	private String username;
	private String password;
	
	@Id
	@GeneratedValue
	@Column(name = "SEQ", unique = true, nullable = false)
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	
	@Column(name = "USER_NAME")
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(name = "PASSWORD")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
spring.datasource.url=jdbc:mysql://localhost:3306/test_schema?useSSL=false
spring.datasource.username=root
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Output

Data inserted Successfully

Related topics