PreparedStatementSetter

PreparedStatementSetter Interface ”; Previous Next The org.springframework.jdbc.core.PreparedStatementSetter interface acts as a general callback interface used by the JdbcTemplate class. This interface sets values on a PreparedStatement provided by the JdbcTemplate class, for each of a number of updates in a batch using the same SQL. Implementations are responsible for setting any necessary parameters. SQL with placeholders will already have been supplied. It”s easier to use this interface than PreparedStatementCreator. The JdbcTemplate will create the PreparedStatement, with the callback only being responsible for setting parameter values. Interface Declaration Following is the declaration for org.springframework.jdbc.core.PreparedStatementSetter interface − public interface PreparedStatementSetter Usage Step 1 − Create a JdbcTemplate object using a configured datasource. Step 2 − Use JdbcTemplate object methods to make database operations while passing PreparedStatementSetter object to replace place holders in query. Example Following example will demonstrate how to read a query using JdbcTemplate class and PreparedStatementSetter interface. We”ll read available record of a student in Student Table. Syntax final String SQL = “select * from Student where id = ? “; List <Student> students = jdbcTemplateObject.query( SQL, new PreparedStatementSetter() { public void setValues(PreparedStatement preparedStatement) throws SQLException { preparedStatement.setInt(1, id); } }, new StudentMapper()); Where, SQL − Select query to read students. jdbcTemplateObject − StudentJDBCTemplate object to read student object from database. PreparedStatementSetter − PreparedStatementSetter object to set parameters in query. StudentMapper − StudentMapper is a RowMapper object to map each fetched record to student object. To understand the above-mentioned concepts related to Spring JDBC, let us write an example which will select a query. To write our example, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring JDBC – First Application. 2 Update the bean configuration and run the application as explained below. Following is the content of the Data Access Object interface file StudentDAO.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDAO { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to list down * a record from the Student table corresponding * to a passed student id. */ public Student getStudent(Integer id); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public Student getStudent(final Integer id) { final String SQL = “select * from Student where id = ? “; List <Student> students = jdbcTemplateObject.query( SQL, new PreparedStatementSetter() { public void setValues(PreparedStatement preparedStatement) throws SQLException { preparedStatement.setInt(1, id); } }, new StudentMapper() ); return students.get(0); } } Following is the content of the MainApp.java file. package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean(“studentJDBCTemplate”); Student student = studentJDBCTemplate.getStudent(1); System.out.print(“ID : ” + student.getId() ); System.out.println(“, Age : ” + student.getAge()); } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.cj.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id = “studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. ID : 1, Age : 17 Print Page Previous Next Advertisements ”;

Spring JDBC – Delete Query

Spring JDBC – Delete Query ”; Previous Next The following example will demonstrate how to delete a query using Spring JDBC. We”ll delete one of the available records in Student Table. Syntax String deleteQuery = “delete from Student where id = ?”; jdbcTemplateObject.update(deleteQuery, id); Where, deleteQuery − Delete query to delete student with placeholders. jdbcTemplateObject − StudentJDBCTemplate object to delete student object in the database. To understand the above-mentioned concepts related to Spring JDBC, let us write an example which will delete a query. To write our example, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring JDBC – First Application. 2 Update the bean configuration and run the application as explained below. Following is the content of the Data Access Object interface file StudentDAO.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDAO { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to list down * all the records from the Student table. */ public List<Student> listStudents(); /** * This is the method to be used to delete * a record from the Student table corresponding * to a passed student id. */ public void delete(Integer id); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public List<Student> listStudents() { String SQL = “select * from Student”; List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } public void delete(Integer id){ String SQL = “delete from Student where id = ?”; jdbcTemplateObject.update(SQL, id); System.out.println(“Deleted Record with ID = ” + id ); return; } } Following is the content of the MainApp.java file. package com.tutorialspoint; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tutorialspoint.StudentJDBCTemplate; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean(“studentJDBCTemplate”); System.out.println(“—-Delete Record with ID = 2 —–” ); studentJDBCTemplate.delete(2); System.out.println(“——Listing Multiple Records——–” ); List<Student> students = studentJDBCTemplate.listStudents(); for (Student record : students) { System.out.print(“ID : ” + record.getId() ); System.out.print(“, Name : ” + record.getName() ); System.out.println(“, Age : ” + record.getAge()); } } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.cj.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id = “studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. —-Updating Record with ID = 2 —– Updated Record with ID = 2 —-Listing Record with ID = 2 —– ID : 2, Name : Nuha, Age : 20 Print Page Previous Next Advertisements ”;

Spring JDBC – Handling CLOB

Spring JDBC – Handling CLOB ”; Previous Next Following example will demonstrate how to update a CLOB using an Update Query with the help of Spring JDBC. We”ll update the available records in Student Table. Student Table CREATE TABLE Student( ID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, DESCRIPTION LONGTEXT, PRIMARY KEY (ID) ); Syntax MapSqlParameterSource in = new MapSqlParameterSource(); in.addValue(“id”, id); in.addValue(“description”, new SqlLobValue(description, new DefaultLobHandler()), Types.CLOB); String SQL = “update Student set description = :description where id = :id”; NamedParameterJdbcTemplate jdbcTemplateObject = new NamedParameterJdbcTemplate(dataSource); jdbcTemplateObject.update(SQL, in); Where, in − SqlParameterSource object to pass a parameter to update a query. SqlLobValue − Object to represent an SQL BLOB/CLOB value parameter. jdbcTemplateObject − NamedParameterJdbcTemplate object to update student object in the database. To understand the above-mentioned concepts related to Spring JDBC, let us write an example, which will update a query. To write our example, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring JDBC – First Application. 2 Update the bean configuration and run the application as explained below. Following is the content of the Data Access Object interface file StudentDAO.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDAO { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to update * a record into the Student table. */ public void updateDescription(Integer id, String description); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; private String description; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); student.setDescription(rs.getString(“description”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.simple.SimpleJdbcCall; import org.springframework.jdbc.core.support.SqlLobValue; import org.springframework.jdbc.support.lob.DefaultLobHandler; import java.io.ByteArrayInputStream; import java.sql.Types; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void updateDescription(Integer id, String description) { MapSqlParameterSource in = new MapSqlParameterSource(); in.addValue(“id”, id); in.addValue(“description”, new SqlLobValue(description, new DefaultLobHandler()), Types.CLOB); String SQL = “update Student set description = :description where id = :id”; NamedParameterJdbcTemplate jdbcTemplateObject = new NamedParameterJdbcTemplate(dataSource); jdbcTemplateObject.update(SQL, in); System.out.println(“Updated Record with ID = ” + id ); } } Following is the content of the MainApp.java file. package com.tutorialspoint; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tutorialspoint.StudentJDBCTemplate; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean(“studentJDBCTemplate”); studentJDBCTemplate.updateDescription(1, “This can be a very long text upto 4 GB of size.”); } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.cj.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id = “studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. Updated Record with ID = 1 You can check the description stored by querying the database. Print Page Previous Next Advertisements ”;

Multiple Batches Operation

Spring JDBC – Multiple Batches Operation ”; Previous Next Following example will demonstrate how to make multiple batch updates in a single call using Spring JDBC. We”ll update the available records in Student table in a multiple batch operation where batch size is 1. Syntax String SQL = “update Student set age = ? where id = ?”; int[][] updateCounts = jdbcTemplateObject.batchUpdate(SQL,students,1, new ParameterizedPreparedStatementSetter<Student>() { public void setValues(PreparedStatement ps, Student student) throws SQLException { ps.setInt(1, student.getAge()); ps.setInt(2, student.getId()); } }); Where, SQL − Update query to update student”s age. jdbcTemplateObject − StudentJDBCTemplate object to update student object in the database. ParameterizedPreparedStatementSetter − Batch executor, set values in PerparedStatement per item identified by the list of objects student. updateCounts − Int[][] array containing updated row count per update query per batch. To understand the above-mentioned concepts related to Spring JDBC, let us write an example which will update multiple batch operation. To write our example, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring JDBC – First Application. 2 Update the bean configuration and run the application as explained below. Following is the content of the Data Access Object interface file StudentDAO.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDAO { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to list down * all the records from the Student table. */ public List<Student> listStudents(); public void batchUpdate(final List<Student> students); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.sql.PreparedStatement; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter; import java.sql.SQLException; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public List<Student> listStudents() { String SQL = “select * from Student”; List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } public void batchUpdate(final List<Student> students){ String SQL = “update Student set age = ? where id = ?”; int[][] updateCounts = jdbcTemplateObject.batchUpdate( SQL,students,1,new ParameterizedPreparedStatementSetter<Student>() { public void setValues(PreparedStatement ps, Student student) throws SQLException { ps.setInt(1, student.getAge()); ps.setInt(2, student.getId()); } } ); System.out.println(“Records updated!”); } } Following is the content of the MainApp.java file. package com.tutorialspoint; import java.util.ArrayList; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean(“studentJDBCTemplate”); List<Student> initialStudents = studentJDBCTemplate.listStudents(); System.out.println(“Initial Students”); for(Student student2: initialStudents){ System.out.print(“ID : ” + student2.getId() ); System.out.println(“, Age : ” + student2.getAge()); } Student student = new Student(); student.setId(1); student.setAge(17); Student student1 = new Student(); student1.setId(3); student1.setAge(18); List<Student> students = new ArrayList<Student>(); students.add(student); students.add(student1); studentJDBCTemplate.batchUpdate(students); List<Student> updatedStudents = studentJDBCTemplate.listStudents(); System.out.println(“Updated Students”); for(Student student3: updatedStudents){ System.out.print(“ID : ” + student3.getId() ); System.out.println(“, Age : ” + student3.getAge()); } } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.cj.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id = “studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. Initial Students ID : 1, Age : 15 ID : 3, Age : 16 records updated! Updated Students ID : 1, Age : 17 ID : 3, Age : 18 Print Page Previous Next Advertisements ”;

Spring JDBC – JdbcTemplate

Spring JDBC – JdbcTemplate Class ”; Previous Next The org.springframework.jdbc.core.JdbcTemplate class is the central class in the JDBC core package. It simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC workflow, leaving the application code to provide SQL and extract results. This class executes SQL queries or updates, initiating iteration over ResultSets and catching JDBC exceptions and translating them to the generic, more informative exception hierarchy defined in the org.springframework.dao package. Class Declaration Following is the declaration for org.springframework.jdbc.core.JdbcTemplate class − public class JdbcTemplate extends JdbcAccessor implements JdbcOperations Usage Step 1 − Create a JdbcTemplate object using a configured datasource. Step 2 − Use JdbcTemplate object methods to make database operations. Example Following example will demonstrate how to read a query using JdbcTemplate class. We”ll read the available records in Student Table. Syntax String selectQuery = “select * from Student”; List <Student> students = jdbcTemplateObject.query(selectQuery, new StudentMapper()); Where, selectQuery − Select query to read students. jdbcTemplateObject − StudentJDBCTemplate object to read student object from the database. StudentMapper − StudentMapper is a RowMapper object to map each fetched record to the student object. To understand the above-mentioned concepts related to Spring JDBC, let us write an example which will select a query. To write our example, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring JDBC – First Application. 2 Update the bean configuration and run the application as explained below. Following is the content of the Data Access Object interface file StudentDAO.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDAO { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to list down * all the records from the Student table. */ public List<Student> listStudents(); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public List<Student> listStudents() { String SQL = “select * from Student”; List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } } Following is the content of the MainApp.java file. package com.tutorialspoint; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tutorialspoint.StudentJDBCTemplate; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean(“studentJDBCTemplate”); System.out.println(“——Listing Multiple Records——–” ); List<Student> students = studentJDBCTemplate.listStudents(); for (Student record : students) { System.out.print(“ID : ” + record.getId() ); System.out.print(“, Name : ” + record.getName() ); System.out.println(“, Age : ” + record.getAge()); } } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id=”dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.cj.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id=”studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. ——Listing Multiple Records——– ID : 1, Name : Zara, Age : 11 ID : 2, Name : Nuha, Age : 2 ID : 3, Name : Ayan, Age : 15 Print Page Previous Next Advertisements ”;

Spring JDBC – Create Query

Spring JDBC – Create Query ”; Previous Next The following example will demonstrate how to create a query using Insert query with the help of Spring JDBC. We”ll insert a few records in Student Table. Syntax String insertQuery = “insert into Student (name, age) values (?, ?)”; jdbcTemplateObject.update( insertQuery, name, age); Where, insertQuery − Insert query having placeholders. jdbcTemplateObject − StudentJDBCTemplate object to insert student object in database. To understand the above-mentioned concepts related to Spring JDBC, let us write an example which will insert a query. To write our example, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring JDBC – First Application. 2 Update the bean configuration and run the application as explained below. Following is the content of the Data Access Object interface file StudentDAO.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDAO { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to create * a record in the Student table. */ public void create(String name, Integer age); /** * This is the method to be used to list down * all the records from the Student table. */ public List<Student> listStudents(); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public void create(String name, Integer age) { String insertQuery = “insert into Student (name, age) values (?, ?)”; jdbcTemplateObject.update( insertQuery, name, age); System.out.println(“Created Record Name = ” + name + ” Age = ” + age); return; } public List<Student> listStudents() { String SQL = “select * from Student”; List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } } Following is the content of the MainApp.java file. package com.tutorialspoint; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tutorialspoint.StudentJDBCTemplate; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean(“studentJDBCTemplate”); System.out.println(“——Records Creation——–” ); studentJDBCTemplate.create(“Zara”, 11); studentJDBCTemplate.create(“Nuha”, 2); studentJDBCTemplate.create(“Ayan”, 15); System.out.println(“——Listing Multiple Records——–” ); List<Student> students = studentJDBCTemplate.listStudents(); for (Student record : students) { System.out.print(“ID : ” + record.getId() ); System.out.print(“, Name : ” + record.getName() ); System.out.println(“, Age : ” + record.getAge()); } } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.cj.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id = “studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. ——Records Creation——– Created Record Name = Zara Age = 11 Created Record Name = Nuha Age = 2 Created Record Name = Ayan Age = 15 ——Listing Multiple Records——– ID : 1, Name : Zara, Age : 11 ID : 2, Name : Nuha, Age : 2 ID : 3, Name : Ayan, Age : 15 Print Page Previous Next Advertisements ”;

Spring JDBC – First Application

Spring JDBC – First Application ”; Previous Next To understand the concepts related to Spring JDBC framework with JDBC Template class, let us write a simple example which will implement Insert and Read operations on the following Student table. CREATE TABLE Student( ID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, PRIMARY KEY (ID) ); Let us proceed to write a simple console based Spring JDBC Application, which will demonstrate JDBC concepts. Create Project Let”s open the command console, go the C:MVN directory and execute the following mvn command. C:MVN>mvn archetype:generate -DgroupId=com.tutorialspoint -DartifactId=Student -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false Maven will start processing and will create the complete Java application project structure. [INFO] Scanning for projects… [INFO] [INFO] ——————< org.apache.maven:standalone-pom >——————- [INFO] Building Maven Stub Project (No POM) 1 [INFO] ——————————–[ pom ]——————————— [INFO] [INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] — maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom — [INFO] Generating project in Batch mode [INFO] —————————————————————————- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0 [INFO] —————————————————————————- [INFO] Parameter: basedir, Value: C:MVN [INFO] Parameter: package, Value: com.tutorialspoint [INFO] Parameter: groupId, Value: com.tutorialspoint [INFO] Parameter: artifactId, Value: Student [INFO] Parameter: packageName, Value: com.tutorialspoint [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: C:MVNStudent [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] ———————————————————————— [INFO] Total time: 6.842 s [INFO] Finished at: 2022-01-01T13:49:20+05:30 [INFO] ———————————————————————— Now go to C:/MVN directory. You”ll see a Java application project created named student (as specified in artifactId). Update the POM.xml to include Spring JDBC dependencies. Add Student.java, StudentMapper.java, MainApp.java, StudentDAO.java and StudentJDBCTemplate.java files. 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 http://maven.apache.org/maven-v4_0_0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.tutorialspoint</groupId> <artifactId>Student</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Student</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.14</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> </dependencies> </project> Following is the content of the Data Access Object interface file StudentDAO.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDAO { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to create * a record in the Student table. */ public void create(String name, Integer age); /** * This is the method to be used to list down * all the records from the Student table. */ public List<Student> listStudents(); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public void create(String name, Integer age) { String SQL = “insert into Student (name, age) values (?, ?)”; jdbcTemplateObject.update( SQL, name, age); System.out.println(“Created Record Name = ” + name + ” Age = ” + age); return; } public List<Student> listStudents() { String SQL = “select * from Student”; List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } } Following is the content of the MainApp.java file. package com.tutorialspoint; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tutorialspoint.StudentJDBCTemplate; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate) context.getBean(“studentJDBCTemplate”); System.out.println(“——Records Creation——–” ); studentJDBCTemplate.create(“Zara”, 11); studentJDBCTemplate.create(“Nuha”, 2); studentJDBCTemplate.create(“Ayan”, 15); System.out.println(“——Listing Multiple Records——–” ); List<Student> students = studentJDBCTemplate.listStudents(); for (Student record : students) { System.out.print(“ID : ” + record.getId() ); System.out.print(“, Name : ” + record.getName() ); System.out.println(“, Age : ” + record.getAge()); } } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.cj.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id = “studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. ——Records Creation——– Created Record Name = Zara Age = 11 Created Record Name = Nuha Age = 2 Created Record Name = Ayan Age = 15 ——Listing Multiple Records——– ID : 1, Name : Zara, Age : 11 ID : 2, Name : Nuha, Age : 2 ID : 3, Name : Ayan, Age : 15 Print Page Previous Next Advertisements ”;

Spring JDBC – Useful Resources

Spring JDBC – Useful Resources ”; Previous Next The following resources contain additional information on Spring JDBC. Please use them to get more in-depth knowledge on this topic. Useful Video Courses Java Servlet Online Training 20 Lectures 5 hours Tutorialspoint More Detail Java Made Easy for Beginners, Testers, Selenium and Appium 249 Lectures 62 hours Arun Motoori More Detail Java Database Connectivity (JDBC) | Database Design In Java Most Popular 19 Lectures 3.5 hours Emenwa Global, Ejike IfeanyiChukwu More Detail Complete Java Programming Training Best Seller 57 Lectures 22.5 hours Uplatz More Detail An introduction to JDBC, JNDI and Spring JDBC with Tomcat 27 Lectures 3.5 hours Nicolaas C Kock More Detail Apache Druid : Complete Guide 21 Lectures 2 hours Ganesh Dhareshwar More Detail Print Page Previous Next Advertisements ”;

Spring JDBC – SqlQuery

Spring JDBC – SqlQuery Class ”; Previous Next The org.springframework.jdbc.object.SqlQuery class provides a reusable operation object representing a SQL query. Class Declaration Following is the declaration for org.springframework.jdbc.object.SqlQuery class − public abstract class SqlQuery<T> extends SqlOperation Usage Step 1 − Create a JdbcTemplate object using a configured datasource. Step 2 − Create a StudentMapper object implementing RowMapper interface. Step 3 − Use JdbcTemplate object methods to make database operations while using SqlQuery object. Following example will demonstrate how to read a Query using SqlQuery Object. We”ll map read records from Student Table to Student object using StudentMapper object. Syntax String sql = “select * from Student”; SqlQuery<Student> sqlQuery = new SqlQuery<Student>() { @Override protected RowMapper<Student> newRowMapper(Object[] parameters, Map<?, ?> context) { return new StudentMapper(); } }; sqlQuery.setDataSource(dataSource); sqlQuery.setSql(sql); List <Student> students = sqlQuery.execute(); Where, SQL − Read query to read all student records. jdbcTemplateObject − StudentJDBCTemplate object to read student records from the database. StudentMapper − StudentMapper object to map the student records to student objects. SqlQuery − SqlQuery object to query student records and map them to student objects. To understand the above-mentioned concepts related to Spring JDBC, let us write an example which will read a query and map the result using StudentMapper object. To write our example, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring JDBC – First Application. 2 Update the bean configuration and run the application as explained below. Following is the content of the Data Access Object interface file StudentDao.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDao { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to list down * all the records from the Student table. */ public List<Student> listStudents(); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.util.List; import java.util.Map; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.object.SqlQuery; public class StudentJDBCTemplate implements StudentDao { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public List<Student> listStudents() { String sql = “select * from Student”; SqlQuery<Student> sqlQuery = new SqlQuery<Student>() { @Override protected RowMapper<Student> newRowMapper(Object[] parameters, Map<?, ?> context){ return new StudentMapper(); } }; sqlQuery.setDataSource(dataSource); sqlQuery.setSql(sql); List <Student> students = sqlQuery.execute(); return students; } } Following is the content of the MainApp.java file. package com.tutorialspoint; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tutorialspoint.StudentJDBCTemplate; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean(“studentJDBCTemplate”); System.out.println(“——Listing Multiple Records——–” ); List<Student> students = studentJDBCTemplate.listStudents(); for (Student record : students) { System.out.print(“ID : ” + record.getId() ); System.out.print(“, Name : ” + record.getName() ); System.out.println(“, Age : ” + record.getAge()); } } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id = “studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. ——Listing Multiple Records——– ID : 1, Name : Zara, Age : 17 ID : 3, Name : Ayan, Age : 18 ID : 4, Name : Nuha, Age : 12 Print Page Previous Next Advertisements ”;

Spring JDBC – Calling Stored Function

Spring JDBC – Calling Stored Function ”; Previous Next Following example will demonstrate how to call a stored function using Spring JDBC. We”ll read one of the available records in Student Table by calling a stored function. We”ll pass an id and receive a student name. Syntax SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withFunctionName(“get_student_name”); SqlParameterSource in = new MapSqlParameterSource().addValue(“in_id”, id); String name = jdbcCall.executeFunction(String.class, in); Student student = new Student(); student.setId(id); student.setName(name); Where, in − SqlParameterSource object to pass a parameter to a stored function. jdbcCall − SimpleJdbcCall object to represent a stored function. jdbcTemplateObject − StudentJDBCTemplate object to called stored function from database. student − Student object. The SimpleJdbcCall class can be used to call a stored function with IN parameter and a return value. You can use this approach while working with either of the RDBMS such as Apache Derby, DB2, MySQL, Microsoft SQL Server, Oracle, and Sybase. To understand the approach, consider the following MySQL stored procedure, which takes student Id and returns the corresponding student”s name. So let us create this stored function in your TEST database using MySQL command prompt − DELIMITER $$ DROP FUNCTION IF EXISTS `TEST`.`get_student_name` $$ CREATE FUNCTION `get_student_name` (in_id INTEGER) RETURNS varchar(200) BEGIN DECLARE out_name VARCHAR(200); SELECT name INTO out_name FROM Student where id = in_id; RETURN out_name; DELIMITER ; To understand the above-mentioned concepts related to Spring JDBC, let us write an example which will call a stored function. To write our example, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring JDBC – First Application. 2 Update the bean configuration and run the application as explained below. Following is the content of the Data Access Object interface file StudentDAO.java. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; public interface StudentDAO { /** * This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /** * This is the method to be used to list down * a record from the Student table corresponding * to a passed student id. */ public Student getStudent(Integer id); } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } } Following is the content of the StudentMapper.java file. package com.tutorialspoint; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setId(rs.getInt(“id”)); student.setName(rs.getString(“name”)); student.setAge(rs.getInt(“age”)); return student; } } Following is the implementation class file StudentJDBCTemplate.java for the defined DAO interface StudentDAO. package com.tutorialspoint; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.simple.SimpleJdbcCall; public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public Student getStudent(Integer id) { SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withFunctionName(“get_student_name”); SqlParameterSource in = new MapSqlParameterSource().addValue(“in_id”, id); String name = jdbcCall.executeFunction(String.class, in); Student student = new Student(); student.setId(id); student.setName(name); return student; } } The code you write for the execution of the call involves creating an SqlParameterSource containing the IN parameter. It”s important to match the name provided for the input value with that of the parameter name declared in the stored function. The executeFunction method takes the IN parameters and returns a String as specified in the stored function. Following is the content of the MainApp.java file package com.tutorialspoint; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tutorialspoint.StudentJDBCTemplate; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean(“studentJDBCTemplate”); Student student = studentJDBCTemplate.getStudent(1); System.out.print(“ID : ” + student.getId() ); System.out.print(“, Name : ” + student.getName() ); } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd “> <!– Initialization for data source –> <bean id = “dataSource” class = “org.springframework.jdbc.datasource.DriverManagerDataSource”> <property name = “driverClassName” value = “com.mysql.cj.jdbc.Driver”/> <property name = “url” value = “jdbc:mysql://localhost:3306/TEST”/> <property name = “username” value = “root”/> <property name = “password” value = “admin”/> </bean> <!– Definition for studentJDBCTemplate bean –> <bean id = “studentJDBCTemplate” class = “com.tutorialspoint.StudentJDBCTemplate”> <property name = “dataSource” ref = “dataSource” /> </bean> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message. ID : 1, Name : Zara Print Page Previous Next Advertisements ”;