Spring JDBC
Spring JDBC
—-------------
In Enterprise Applications we have to prepare the DAO layer in order to
perform database operations, where to perform database operations in the DAO
layer we have to use Spring-JDBC.
In the case of the Spring-JDBC module, we are able to get all the results in
the form of Collections directly, where Collection objects are Serializable
bydefault, so we are able to transfer Collection objects in the network.
Spring-JDBC module is providing very good support for Transactions with the
help of Transactions module.
5. Plain JDBC does not have mapper interfaces to map the data from ResultSet
object to Collection object.
Spring-JDBC module has direct mapper interfaces to map the data from
ResultSet object to Collection object.
6. In Plain JDBC, we are able to get the Connection object either through
DriverManager or through Connection Pooling, where to implement Connection
Pooling we have to write java code explicitly.
JdbcTemplate
NamedParameterJdbcTemplate
SimpleJdbcTemplate
SimpleJdbcCall
JdbcInsert
—---
—---
6. Prepare Test class and access Dao methods in order to perform Database
operations.
JdbcTemplate:
—------------
JdbcTemplate is a class provided by Spring Framework, it has provided an
abstraction for the boilerplate code of the JDBC.
It has provided the following methods to write and execute sql queries.
import com.durgasoft.beans.Employee;
import java.util.List;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
@Override
public List<Employee> getAllEmployees() {
List<Employee> empList = jdbcTemplate.query("select
* from emp1", (rs, rowNum)->{
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList;
}
Employee.java
package com.durgasoft.beans;
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/contex
t"
xsi:schemaLocation="http://www.springframework.org/schema/b
eans
https://www.springframework.org/schema/beans/spring-beans.x
sd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-conte
xt.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDat
aSource">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDao"
class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springjdbcapp01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springjdbcapp01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEn
coding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
In the case of JdbcTemplate, we are able to provide place holders or
positional parameters inside the sql queries in PreparedStatement
style, but we must provide values to the positional parameters
explicitly by using anonymous Object[] .
EX:
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
@Override
public List<Employee> getAllEmployees() {
List<Employee> empList = jdbcTemplate.query("select
* from emp1 where ESAL < ?",new Object[]{10000} ,(rs,
rowNum)->{
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList;
}
NamedParameterJdbcTemplate:
—----------------------------
In Spring JDBC, we are able to define names in place of place holders
in order to improve readability.
EX:
String query = “insert into emp1 values(:eno,:ename,
:esal,:eaddr)”;
To provide values to the named parameters we have to use the following
approaches.
1. By Using Map
2. By Using SqlParameterSource
1. By Using Map:
—----------------
In this approach, create a Map object and provide key-value pairs,
where keys must be the parameter names and values must be the
respective values.
EX:
String query = “insert into emp1 values(:eno,:ename,
:esal,:eaddr)”;
Map<String, Object> map = new HashMap<>();
map.put(“eno”, 111);
map.put(“ename”, “Durga”);
map.put(“esal”, 50000.0f);
map.put(“eaddr”, “Hyd”);
namedParameterJdbcTemplate.update(query, map);
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import java.util.List;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.JdbcTemplate;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbc
Template;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public void
setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate
namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate =
namedParameterJdbcTemplate;
}
public NamedParameterJdbcTemplate
getNamedParameterJdbcTemplate() {
return namedParameterJdbcTemplate;
}
List<Employee> empList=
namedParameterJdbcTemplate.query("select * from emp1 where
ENO = :eno",Map.of("eno",eno),(rs, rowNum) -> {
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList.isEmpty()?null:empList.get(0);
}
@Override
public List<Employee> getAllEmployees() {
List<Employee> empList =
namedParameterJdbcTemplate.query("select * from emp1 where
ESAL < :esal",Map.of("esal", 10000) ,(rs, rowNum)->{
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList;
}
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/contex
t"
xsi:schemaLocation="http://www.springframework.org/schema/b
eans
https://www.springframework.org/schema/beans/spring-beans.x
sd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-conte
xt.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDat
aSource">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParame
terJdbcTemplate">
<constructor-arg name="dataSource"
ref="dataSource"/>
</bean>
<bean id="employeeDao"
class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="namedParameterJdbcTemplate"
ref="namedParameterJdbcTemplate"/>
</bean>
</beans>
App.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplication
Context;
import java.util.List;
*/
/*
Employee employee = employeeDao.search(222);
if(employee == null){
System.out.println("Employee Does not Exist");
}else{
System.out.println("Employee Details");
System.out.println("-----------------------");
System.out.println("Employee Number :
"+employee.getEno());
System.out.println("Employee Name :
"+employee.getEname());
System.out.println("Employee Salary :
"+employee.getEsal());
System.out.println("Employee Address :
"+employee.getEaddr());
}
*/
/*
List<Employee> empList =
employeeDao.getAllEmployees();
if(empList == null){
System.out.println("No Employee is Existed");
}else{
for (Employee emp: empList ) {
System.out.print(emp.getEno()+"\t"+emp.getEname()+"\t"+emp.
getEsal()+"\t"+emp.getEaddr()+"\n");
}
}
*/
/*
Employee employee = new Employee();
employee.setEno(111);
employee.setEname("XXX");
employee.setEsal(9000);
employee.setEaddr("Chennai");
String status = employeeDao.update(employee);
System.out.println(status);
*/
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springjdbcapp01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springjdbcapp01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEn
coding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
By Using SqlparameterSource:
—---------------------------
Where SqlparameterSource is an interface, its implementation
classes are provided by the Spring Framework in the form of the
following classes.
1. MapSqlParameterSource
2. BeanPropertySqlparameterSource
MapSqlParameterSource:
—----------------------
If we want to provide values to the named parameters in sql
queries by using MapSqlparaeterSource then we have to use the
following method.
Ex:
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import java.util.List;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
@Override
public List<Employee> getAllEmployees() {
MapSqlParameterSource paramValues = new MapSqlParameterSource("esal",
10000);
List<Employee> empList = namedParameterJdbcTemplate.query("select * from
emp1 where ESAL < :esal",paramValues,(rs, rowNum)->{
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList;
}
public String update(Employee employee) {
String status = "";
String query = "update emp1 set ENAME = :ename, ESAL = :esal, EADDR =
:eaddr where ENO = :eno";
Employee.java
package com.durgasoft.beans;
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<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:3300/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDao" class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="namedParameterJdbcTemplate"
ref="namedParameterJdbcTemplate"/>
</bean>
</beans>
App.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
*/
/*
Employee employee = employeeDao.search(222);
if(employee == null){
System.out.println("Employee Does not Exist");
}else{
System.out.println("Employee Details");
System.out.println("-----------------------");
System.out.println("Employee Number : "+employee.getEno());
System.out.println("Employee Name : "+employee.getEname());
System.out.println("Employee Salary : "+employee.getEsal());
System.out.println("Employee Address : "+employee.getEaddr());
}
*/
/*
List<Employee> empList = employeeDao.getAllEmployees();
if(empList == null){
System.out.println("No Employee is Existed");
}else{
for (Employee emp: empList ) {
System.out.print(emp.getEno()+"\t"+emp.getEname()+"\t"+emp.getEsal()+"\t"+emp.
getEaddr()+"\n");
}
}
*/
/*
*/
}
}
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springjdbcapp01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springjdbcapp01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
By using BeanPropertySqlParameterSource:
—----------------------------------------
In the case of NamedParameterJdbcTemplate , to provide values to the named
parameters if we use BeanPropertySqlParameterSource then we have to use the
following steps.
EX:
String query = “insert into emp1 values(:eno, :ename, :esal,
:eaddr)”;
Employee emp = new Employee();
emp.setEno(111);
emp.setEname(“Durga”);
emp.setEsal(5000);
emp.setEaddr(“Hyd”);
BeanPropertySqlParameterSource paramValues = new
BeanPropertySqlParameterSource(emp);
namedParameterJdbcTemplate.update(query, paramValues);
EX:
Employee.java
package com.durgasoft.beans;
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import java.util.List;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Override
public List<Employee> getAllEmployees() {
Employee emp1 = new Employee();
emp1.setEsal(10000);
BeanPropertySqlParameterSource paramValues = new
BeanPropertySqlParameterSource(emp1);
List<Employee> empList = namedParameterJdbcTemplate.query("select * from
emp1 where ESAL < :esal",paramValues,(rs, rowNum)->{
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList;
}
App.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
*/
/*
System.out.print(emp.getEno()+"\t"+emp.getEname()+"\t"+emp.getEsal()+"\t"+emp.
getEaddr()+"\n");
}
}
*/
}
}
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<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:3300/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDao" class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="namedParameterJdbcTemplate"
ref="namedParameterJdbcTemplate"/>
</bean>
</beans>
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springjdbcapp01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springjdbcapp01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
SimpleJdbcTemplate:
—--------------------
Initially Spring 2.4 version was provided on the basis of JDk1.4 version,
When JDK1.5 version was introduced in Java a number of enhancements were
provided in JDK 1.5 version like Var-Arg method, Annotations,....
In the above context, DAO Support classes are able to supply XXXTemplate
objects directly in DAO classes without declaring XXXTemplate property and
its setter method in the DAO classes.
Spring Framework has provided the following three types of DAO Support
classes.
1. JdbcDaoSupport
2. NamedParameterJdbcDaoSupport
3. SimpleJdbcDaoSupport
To get XXXTemplate classes, all the DAO Support classes are having the
following methods.
EX:
EX:
Employee.java
package com.durgasoft.beans;
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import java.util.List;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.JdbcTemplate;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTempl
ate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Override
public List<Employee> getAllEmployees() {
List<Employee> empList = getJdbcTemplate().query("select
* from emp1 where ESAL < ?",new Object[]{10000} ,(rs, rowNum)->{
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList;
}
App.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationConte
xt;
import java.util.List;
*/
/*
Employee employee = employeeDao.search(111);
if(employee == null){
System.out.println("Employee Does not Exist");
}else{
System.out.println("Employee Details");
System.out.println("-----------------------");
System.out.println("Employee Number :
"+employee.getEno());
System.out.println("Employee Name :
"+employee.getEname());
System.out.println("Employee Salary :
"+employee.getEsal());
System.out.println("Employee Address :
"+employee.getEaddr());
}
*/
/*
List<Employee> empList = employeeDao.getAllEmployees();
if(empList == null){
System.out.println("No Employee is Existed");
}else{
for (Employee emp: empList ) {
System.out.print(emp.getEno()+"\t"+emp.getEname()+"\t"+emp.getEs
al()+"\t"+emp.getEaddr()+"\n");
}
}*/
/*
}
}
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xs
d">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSour
ce">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name=”dataSource” ref=”dataSource”/>
</bean>
<bean id="employeeDao"
class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springjdbcapp01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springjdbcapp01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncodin
g>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
NamedParameterJdbcDaoSupport:
—------------------------------
Employee.java
package com.durgasoft.beans;
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import java.util.List;
}
EmployeeDaoImpl1.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSu
pport;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTempl
ate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
List<Employee> empList=
getNamedParameterJdbcTemplate().query("select * from emp1 where
ENO = :eno",Map.of("eno",eno),(rs, rowNum) -> {
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList.isEmpty()?null:empList.get(0);
}
@Override
public List<Employee> getAllEmployees() {
List<Employee> empList =
getNamedParameterJdbcTemplate().query("select * from emp1 where
ESAL < :esal",Map.of("esal", 10000) ,(rs, rowNum)->{
Employee emp = new Employee();
emp.setEno(rs.getInt("ENO"));
emp.setEname(rs.getString("ENAME"));
emp.setEsal(rs.getFloat("ESAL"));
emp.setEaddr(rs.getString("EADDR"));
return emp;
});
return empList;
}
App.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationConte
xt;
import java.util.List;
*/
/*
Employee employee = employeeDao.search(111);
if(employee == null){
System.out.println("Employee Does not Exist");
}else{
System.out.println("Employee Details");
System.out.println("-----------------------");
System.out.println("Employee Number :
"+employee.getEno());
System.out.println("Employee Name :
"+employee.getEname());
System.out.println("Employee Salary :
"+employee.getEsal());
System.out.println("Employee Address :
"+employee.getEaddr());
}
*/
/*
List<Employee> empList = employeeDao.getAllEmployees();
if(empList == null){
System.out.println("No Employee is Existed");
}else{
for (Employee emp: empList ) {
System.out.print(emp.getEno()+"\t"+emp.getEname()+"\t"+emp.getEs
al()+"\t"+emp.getEaddr()+"\n");
}
}
*/
/*
*/
}
}
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xs
d">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSour
ce">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJd
bcTemplate">
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDao1"
class="com.durgasoft.dao.EmployeeDaoImpl1">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springjdbcapp01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springjdbcapp01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncodin
g>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
SimpleDaoSupport class:
The main purpose of SImpleDaoSupport class is to supply
SimpleJdbcTemplate object to the DAO class without declaring
SimpleJdbcTemplate property in Dao implementation class.
This DaoSupport class did not exist in the Spring 5.0 version,
because SimpleJdbcTemplate was removed from the Spring 5.x
version.
Batch Updations:
—-----------------
IN general, in database applications,we will execute all the sql
queries by sending query by query from java application to the
database, where at database all the queries are executed one after
another and database Engine will send the results back to the java
application one after another.
In the case of Batch updates, we will gather all the sql queries as a
single batch , we will send batch of sql queries to the database at a
time , we will make the database Engine to execute batch of sql
queries and we will make the database engine to send all the results
at a time to the java applications.
EX:
Employee.java
package com.durgasoft.beans;
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.w3c.dom.stylesheets.LinkStyle;
import java.util.List;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
@Override
public int[] insert(List<Employee> empList) {
int[] rowCounts = jdbcTemplate.batchUpdate("insert into emp1
values(?,?,?,?)", new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws
SQLException {
ps.setInt(1, empList.get(i).getEno());
ps.setString(2, empList.get(i).getEname());
ps.setFloat(3, empList.get(i).getEsal());
ps.setString(4, empList.get(i).getEaddr());
}
@Override
public int getBatchSize() {
return empList.size();
}
});
return rowCounts;
}
}
Main.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
}
}
SpringConfig.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.xsd">
<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3300/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean name="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean name="employeeDao" class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp02</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
EX:
EmployeeDao.java
package com.durgasoft.dao;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import javax.sql.DataSource;
import java.util.Map;
@Override
public double getEmployeeSalary(int eno) {
SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(dataSource);
simpleJdbcCall.withProcedureName("getSal");
SqlParameterSource paramValue = new MapSqlParameterSource("no", eno);
Map<String, Object> result = simpleJdbcCall.execute(paramValue);
return (double)result.get("SAL");
}
}
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="system"/>
<property name="password" value="durga"/>
</bean>
<bean id="employeeDao" class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
Main.java
/*
create or replace procedure getSal(no IN int, sal OUT float)
AS
BEGIN
select ESAL into sal from emp1 where ENO = no;
END getSal;
/
*/
package com.durgasoft;
import com.durgasoft.dao.EmployeeDao;
import com.durgasoft.dao.EmployeeDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp04</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.8</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.0-XE</version>
</dependency>
</dependencies>
</project>
To access Stored procedures and functions which are using CURSOR then
we have to use the following steps from Spring JDBC application.
import java.util.Map;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import
org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import javax.sql.DataSource;
import java.util.Map;
@Override
public double getEmployeeSalary(int eno) {
SimpleJdbcCall simpleJdbcCall = new
SimpleJdbcCall(dataSource);
simpleJdbcCall.withProcedureName("getSal");
SqlParameterSource paramValue = new
MapSqlParameterSource("no", eno);
Map<String, Object> result =
simpleJdbcCall.execute(paramValue);
return (double)result.get("SAL");
}
@Override
public Map<String, Object> getAllEmployees() {
SimpleJdbcCall simpleJdbcCall = new
SimpleJdbcCall(dataSource);
simpleJdbcCall =
simpleJdbcCall.withProcedureName("getAllEmployees");
simpleJdbcCall = simpleJdbcCall.returningResultSet("emps",
BeanPropertyRowMapper.newInstance(Employee.class));
Map<String, Object> results = simpleJdbcCall.execute();
return results;
}
}
Main.java
/*
create or replace procedure getSal(no IN int, sal OUT float)
AS
BEGIN
select ESAL into sal from emp1 where ENO = no;
END getSal;
/
*/
/*
create or replace procedure getAllEmployees(emps OUT SYS_REFCURSOR)
AS
BEGIN
open emps for select * from emp1;
END getAllEmployees;
/
*/
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import com.durgasoft.dao.EmployeeDaoImpl;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.Map;
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="oracle.jdbc.OracleDriver"/>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="system"/>
<property name="password" value="durga"/>
</bean>
<bean id="employeeDao" class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp04</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.8</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-conte
xt -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.0-XE</version>
</dependency>
</dependencies>
</project>
In Spring JDBC, to process BLOB and CLOB data Spring JDBC has provided the
predefined library in the form of the following interfaces.
1. AbstractLobCreatingPreparedStatementCallback:
It can be used to Store BLOB data or CLOB data in the databases by
using the following method.
2. AbstractLobStreamingResultSetExtractor:
It can be used to retrieve BLOB and CLOB data from the databases.
protected abstract void streamData(ResultSet rs) throws
SQLException, IOException, DataAccessException;
3. LobCreator:
It has provided some methods to send data BLOB and CLOB to the database.
EX:
Employee.java
package com.durgasoft.beans;
import java.io.File;
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import
org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStat
ementCallback;
import
org.springframework.jdbc.core.support.AbstractLobStreamingResultSetEx
tractor;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.util.FileCopyUtils;
import java.io.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@Override
public void insertEmployee(Employee employee) {
String query = "insert into emp10 values(?,?,?,?)";
jdbcTemplate.execute(query, new
AbstractLobCreatingPreparedStatementCallback(lobHandler) {
@Override
protected void setValues(PreparedStatement ps, LobCreator
lobCreator) throws SQLException, DataAccessException {
try {
ps.setInt(1, employee.getEno());
ps.setString(2, employee.getEname());
FileInputStream fileInputStream = new
FileInputStream(employee.getEmp_Image());
FileReader fileReader = new
FileReader(employee.getEmp_Resume());
lobCreator.setBlobAsBinaryStream(ps, 3,
fileInputStream, (int)employee.getEmp_Image().length());
lobCreator.setClobAsCharacterStream(ps, 4,
fileReader, (int)employee.getEmp_Resume().length());
}catch(IOException e){
e.printStackTrace();
}
}
});
}
@Override
public Employee readEmployee(int eno) {
Employee employee = new Employee();
String query = "select * from emp10 where ENO = "+eno;
jdbcTemplate.query(query, new
AbstractLobStreamingResultSetExtractor<Object>() {
@Override
protected void streamData(ResultSet rs) throws
SQLException, IOException, DataAccessException {
employee.setEno(rs.getInt("ENO"));
employee.setEname(rs.getString("ENAME"));
FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs,3),fileOutputS
tream);
employee.setEmp_Image(image_File);
FileCopyUtils.copy(lobHandler.getClobAsCharacterStream(rs, 4),
fileWriter);
employee.setEmp_Resume(resume_File);
}
});
return employee;
}
}
Main.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.File;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("SpringConfig.xml");
EmployeeDao employeeDao = (EmployeeDao)
applicationContext.getBean("employeeDao");
/*
Employee employee = new Employee();
employee.setEno(111);
employee.setEname("Durga");
employee.setEmp_Image(new File("E:\\images_new\\nag.jpg"));
employee.setEmp_Resume(new
File("E:\\documents\\nagoor.docx"));
employeeDao.insertEmployee(employee);
System.out.println("Employee Inserted Successfully");
*/
Employee employee = employeeDao.readEmployee(111);
System.out.println("Employee Details");
System.out.println("------------------------");
System.out.println("Employee Number :
"+employee.getEno());
System.out.println("Employee Name :
"+employee.getEname());
System.out.println("Employee Image :
"+employee.getEmp_Image().getAbsolutePath());
System.out.println("Employee Resume :
"+employee.getEmp_Resume().getAbsolutePath());
}
}
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="oracle.jdbc.OracleDriver"/>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="system"/>
<property name="password" value="durga"/>
</bean>
<bean name="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean name="lobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
<bean id="employeeDao" class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
<property name="lobHandler" ref="lobHandler"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp05</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.8</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-conte
xt -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.0-XE</version>
</dependency>
</dependencies>
</project>
In Enterprise applications, we may use more modules and more users wants
to access database, for every user and for every module creating a
separate connection object and destroying that connection objects will
consume more system memory and more execution time, it will affect the
enterprise application performance.
driverClassName
url
username
password
EX:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="system"/>
<property name="password" value="durga"/>
</bean>
EX:
Employee.java
package com.durgasoft.beans;
EmployeeDao.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
@Override
public Employee search(int eno) {
List<Employee> empList = jdbcTemplate.query("select * from
emp1 where ENO = "+eno,(rs, rowNum)->{
Employee employee = new Employee();
employee.setEno(rs.getInt("ENO"));
employee.setEname(rs.getString("ENAME"));
employee.setEsal(rs.getFloat("ESAL"));
employee.setEaddr(rs.getString("EADDR"));
return employee;
});
return empList.isEmpty()?null: empList.get(0);
}
}
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<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:3300/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean name="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
Main.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp06</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.8</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-conte
xt -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
</project>
Third party Provided Connection pooling mechanisms:
—---------------------------------------------------
Third party provided connection pooling mechanisms are more powerful
than Default Connection pooling mechanisms.
Apache DBCP:
—------------
To use Apache DBCP Connection pooling mechanism we have to use the
following properties and Dependencies.
DataSource : BasicDataSource
driverClassName :
url
username:
password:
initialSize: initial number of connections in a pool
maxTotal: Allows max number of connections in a pool
Dependencies:
commons-dbcp2-version.jar
commons-pool2-version.jar
EX:
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3300/durgadb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="initialSize" value="5"/>
<property name="maxTotal" value="10"/>
</bean>
<bean name="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp06</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.8</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-conte
xt -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>
C3P0:
To use the C3P0 Connection pooling mechanism we have to use the
following properties and Dependencies.
DataSource : ComboPooledDataSource
driverClass :
jdbcUrl
user:
password:
minpoolsize: initial number of connections in a pool
maxpoolsize: Allows max number of connections in a pool
Dependencies:
c3p0-version.jar
machangecommons-java-version.jar
EX:
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"
value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3300/durgadb"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="10"/>
</bean>
<bean name="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp06</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.8</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-conte
xt -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.20</version>
</dependency>
</dependencies>
</project>
Proxool:
To use the Proxool Connection pooling mechanism we have to use the
following properties and Dependencies.
DataSource : ProxoolDataSource
driver:
driverUrl:
user:
password:
minimumconnectioncount: initial number of connections in a pool
maximumconnectioncount: Allows max number of connections in a pool
Dependencies:
proxool-version.jar
proxool-cglib.jar
EX:
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="driverUrl"
value="jdbc:mysql://localhost:3300/durgadb"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="minimumConnectionCount" value="5"/>
<property name="maximumConnectionCount" value="10"/>
</bean>
<bean name="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp06</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-conte
xt -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>com.cloudhopper.proxool</groupId>
<artifactId>proxool</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
</project>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="durga_jndi"/>
<property name="jndiEnvironment">
<props>
<prop
key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory
</prop>
<prop
key="java.naming.provider.url">t3://localhost:7001</prop>
</props>
</property>
</bean>
EX:
Employee.java
package com.durgasoft.beans;
import com.durgasoft.beans.Employee;
EmployeeDaoImpl.java
package com.durgasoft.dao;
import com.durgasoft.beans.Employee;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
@Override
public Employee getEmployee(int eno) {
List<Employee> empList = jdbcTemplate.query("select *
from emp1 where ENO = "+eno, (rs, rowNum)->{
Employee employee = new Employee();
employee.setEno(rs.getInt("ENO"));
employee.setEname(rs.getString("ENAME"));
employee.setEsal(rs.getFloat("ESAL"));
employee.setEaddr(rs.getString("EADDR"));
return employee;
});
return empList.isEmpty()?null:empList.get(0);
}
}
Main.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicatio
nContext;
}
}
SpringConfig.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"
xmlns:context="http://www.springframework.org/schema/
context"
xsi:schemaLocation="http://www.springframework.org/sc
hema/beans
https://www.springframework.org/schema/beans/spring-b
eans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring
-context.xsd">
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean
">
<property name="jndiName" value="durga_jndi"/>
<property name="jndiEnvironment">
<props>
<prop
key="java.naming.factory.initial">weblogic.jndi.WLIni
tialContextFactory</prop>
<prop
key="java.naming.provider.url">t3://localhost:7001</p
rop>
</props>
</property>
</bean>
<bean name="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDao"
class="com.durgasoft.dao.EmployeeDaoImpl">
<property name="jdbcTemplate"
ref="jdbcTemplate"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>springjdbcapp08</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.so
urceEncoding>
</properties>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframewor
k/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframewor
k/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
</dependencies>
</project>
==============================================================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
</beans>