0% found this document useful (0 votes)
13 views11 pages

3 Day 3

The document discusses the SimpleJdbcTemplate class in the Spring JDBC module, which was introduced in Spring 2.5 to support JDK 1.5 features but was deprecated in later versions and removed in Spring 5.x. It outlines various methods provided by SimpleJdbcTemplate for executing SQL queries and highlights the use of ParameterizedRowMapper for data retrieval. Additionally, it includes code examples for setting up a MySQL database, creating an Employee class, and implementing DAO classes for database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

3 Day 3

The document discusses the SimpleJdbcTemplate class in the Spring JDBC module, which was introduced in Spring 2.5 to support JDK 1.5 features but was deprecated in later versions and removed in Spring 5.x. It outlines various methods provided by SimpleJdbcTemplate for executing SQL queries and highlights the use of ParameterizedRowMapper for data retrieval. Additionally, it includes code examples for setting up a MySQL database, creating an Employee class, and implementing DAO classes for database operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Frameworks Package

SimpleJdbcTemplate

In Spring JDBC module, the main intention of SimpleJdbcTemplate class is to provide


support for JDK5.0 version features like Auto Boxing, Auto Unboxing, Var-Arg methods,.....

SimpleJdbcTemplate class was provided in Spring 2.5 version only and it was
deprecated in the later versions Spring3.x and Spring4.x , in Spring5.x version
SimpleJdbcTemplate class was removed.

If we want to use SimpleJdbcTemplate class we have to use Spring 2.5 version jar files in
Spring applications.

To execute SQL queries, SimpleJdbcTemplate class has provided the following methods.

public Object execute(String sqlQuery)

Note:

To use this method we have to get JdbcOperations class by using getJdbcOperations()


method.

public int update(String query, Object ... params)


public Object queryForInt(String query, Object ... params)
public Object queryForLong(String query, Object ... params)
public Object query(String query, Object ... params)
public Object queryForObject(String query,Object ... params)
----
----

Note:

In case of SimpleJdbcTemplate class, to perform retrieval operations, we have to use


"ParameterizedRowMapper" in place of RowMapper interface.

Spring1.x, 2.4 versions are provided with JDk1.4 and below


versions of Java, so in Spring1.x version and Spring 2.4 versions
JDK1.5 version features are not supported, but, Spring2.5 version
is providing some environment to support for JDk1.5 version
features in spring applications.

In Spring Jdbc, to support for JDk1.5 version feature Spring has


provided “SimpleJdbcTemplate”.

1 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package

SimpleJdbcTemplate has the following methods to execute


applications.

public int update(String query, Object … values)


public List query(String query, Object … values)
public Object queryForInt(String query, Object … values);
—--
—--
SimpleJdbcTemplate was supported by Spring2.5 version only, it
was deprecated in Spring3.x versions, it was removed from
Spring4.x and Spring5.x versions.

Features in JDK1.5 version:

Auto Boxing and Auto Unboxing


Var-Arg method
Annotations
For-each loop
----
----

Project Name[App03]

MySQL:
Let's first create a table in the mysql database,

use College;

create table Employee

ENo int PRIMARY KEY NOT NULL,

EName varchar(40),

ESal int,

2 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
EAddr varchar(100)

);

To check the data from table

Select * from Employee;

Then Update the [pom.xml] file.


[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.ccteam</groupId>
<artifactId>App03</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-core</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>

3 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
</project>

Ex:
[Employee.java]
package com.ccteam.beans;

public class Employee


{
private int eno;
private String ename;
private float esal;
private String eaddr;

public int getEno()


{
return eno;
}

public void setEno(int eno) {


this.eno = eno;
}

public String getEname() {


return ename;
}

public void setEname(String ename) {


this.ename = ename;
}

public float getEsal() {


return esal;
}

public void setEsal(float esal) {


this.esal = esal;
4 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
}

public String getEaddr() {


return eaddr;
}

public void setEaddr(String eaddr) {


this.eaddr = eaddr;
}
}

[EmployeeMapper.java]
package com.ccteam.dao;

import com.ccteam.beans.Employee;
import
org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class EmployeeMapper implements
ParameterizedRowMapper<Employee>
{
@Override
public Employee mapRow(ResultSet rs, int row_No) throws
SQLException
{
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;
}
}

[EmployeeDao.java]

package com.ccteam.dao;
import com.ccteam.beans.Employee;
public interface EmployeeDao
{
public String add(Employee emp);
public Employee search(int eno);
public String update(Employee emp);
public String delete(int eno);
}
5 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package

[EmployeeDaoImpl.java]
package com.ccteam.dao;
import com.ccteam.beans.Employee;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
public class EmployeeDaoImpl implements EmployeeDao
{

private SimpleJdbcTemplate simpleJdbcTemplate;


String status = "";
public void setSimpleJdbcTemplate(SimpleJdbcTemplate
simpleJdbcTemplate)
{
this.simpleJdbcTemplate = simpleJdbcTemplate;
}

@Override
public String add(Employee emp)
{
String query = "insert into Employee
values("+emp.getEno()+",'"+emp.getEname()+"',"+emp.getEsal()
+",'"+emp.getEaddr()+"')";
simpleJdbcTemplate.getJdbcOperations().execute(query);
status = "SUCCESS";
return status;
}

@Override
public Employee search(int eno)
{
String query = "select * from employee where eno=?";
Employee emp = simpleJdbcTemplate.queryForObject(query,
new EmployeeMapper(), eno);
return emp;
}

@Override
public String update(Employee emp)
{
String query = "update employee set ename=?, esal =?,
eaddr=? where eno=?";
simpleJdbcTemplate.update(query, emp.getEname(),
emp.getEsal(), emp.getEaddr(), emp.getEno());
status = "SUCCESS";
return status;
}

6 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package

@Override
public String delete(int eno) {
String query = "delete from employee where eno = ?";
simpleJdbcTemplate.update(query, eno);
status = "SUCCESS";
return status;
}
}

[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.DriverManagerDataSourc
e">
<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="oracle"/>
</bean>
-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSourc
e" >
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/College"/>
<property name="username" value="root"/>
<property name="password" value="mysql"/>
</bean>
<bean id="simpleJdbcTemplate"

7 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>

<bean id="employeeDao"
class="com.ccteam.dao.EmployeeDaoImpl">
<property name="simpleJdbcTemplate"
ref="simpleJdbcTemplate"/>
</bean>
</beans>

[Main.java]

package com.ccteam;

import com.ccteam.beans.Employee;
import com.ccteam.dao.EmployeeDao;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContex
t;

public class Main


{

public static void main(String[] args)throws Exception


{
ApplicationContext context = new
ClassPathXmlApplicationContext("SpringConfig.xml");
EmployeeDao dao =
(EmployeeDao)context.getBean("employeeDao");
/*
Employee emp = new Employee();
emp.setEno(101);
emp.setEname("Gaurav");
emp.setEsal(5000);
emp.setEaddr("Agra");
String status1 = dao.add(emp);

System.out.println("Employee Insertion
Status :"+status1);
*/
System.out.println();
Employee emp1 = dao.search(101);

8 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
System.out.println("Employee Details");
System.out.println("--------------------");
System.out.println("Employee Number :"+emp1.getEno());
System.out.println("Employee Name :"+emp1.getEname());
System.out.println("Employee Salary :"+emp1.getEsal());
System.out.println("Employee Address :"+emp1.getEaddr());
System.out.println();
/*
Employee emp2 = new Employee();
emp2.setEno(101);
emp2.setEname("BBB");
emp2.setEsal(6000);
emp2.setEaddr("Sec");
String status2 = dao.update(emp2);
System.out.println("Employee Updation Status :"+status2);
System.out.println();
String status3 = dao.delete(101);
System.out.println("Employee Deletion Status :"+status3);
*/

DAO Support classes:

—-----------------------

public class StudentDaoImpl implements StudentDao


{
@Autowired
private JdbcTemplate jdbcTemplate;

9 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package

Without declaring xxxTemplate classes in Dao classes, if we want


to get XXXTemplate class we have to use “Dao Support classes”.
In Spring JDBC , we have to prepare DAO implementation classes with XXXTemplate
property and the corresponding setXXX() method inorder to inject XXXTemplate class.

In Spring JDBC applications, if we want to get XXXTemplate classes without declaring


Template properties and corresponding setXXX() methods we have to use DAO Support
classes provided Spring JDBC module.

There are three types of DAOSupport classes inorder to get Template object in DAO
classes.

There are three types of Dao classes.


1. JdbcDaoSupport
2. NamedParameterJdbcDaoSupport
3. SimpleJdbcDaoSupport

JdbcDaoSupport:
—------------------
To get JdbcTemplate through JdbcDaoSupport class we have to
use the following method from JdbcDaoSupport class .

public JdbcTemplate getJdbcTemplate()

NamedParameterJdbcDaoSupport:
—-------------------------------------
To get a NamedParameterJdbcTemplate object through
NamedParameterJdbcDaoSupport we have to use the following
method.

public NamedParameterJdbcTemplate
getNamedParameterJdbcTemplate()

SimpleJdbcDaoSupport:
Where SimpleJdbcDaoSupport class is able to provide SimpleJdbcTemplate reference in
Dao class by using the following method.

public SimpleJdbcTemplate getSimpleJdbcTemplate()

EX:
10 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Java Frameworks Package
public class EmployeeDaoImpl extends JdbcDaoSupport implements EmployeeDao
{

public String insert(int eno, String ename, float esal, String eaddr)
{
getJdbcTemplate().update("insert into emp1
values("+eno+",'"+ename+"',"+esal+",'"+eaddr+"')");
return "SUCCESS";
}
----
----
}

11 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)

You might also like