4 Day 4
4 Day 4
The basic difference b/w a thick client & thin client application is of business logic.
2) In thin client application business logic is implemented as part of the RDBMS package
Advantage of thin client model is performance better than thick and disadvantage is, it is
database dependent.
A procedure can be stored in the database, as a schema object, for repeated execution.
What is a Function:
• A function can be stored in the database as a schema object for repeated execution.
The Major difference b/w Procedure and Function is of mode of returning values.
The Function returns a value explicitly or directly where as Procedure only returns a
value indirectly.
Syntax: Procedure
create or replace procedure ProcName[(ParamList)]
AS
—---Global Variables—---
BEGIN
—--Database Logic—-----
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
End ProcName;
/ —-> To save procedure at Database
Syntax: Function
Create or replace Function functionName[(ParamValues)] as
ReturnType
—--Global Variables—--
BEGIN
—----Database Logic—----
Return value;
END functionName;
/→ To save Function.
If we want to access stored procedures and functions which are available at database from
Spring Jdbc application then we have to use "SimpleJdbcCall".
To use SimpleJdbcCall in Spring Jdbc applications we have to use the following steps.
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-
core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.23</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-
context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-
jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.23</version>
</dependency>
<!--
https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc
11 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>21.7.0.0</version>
</dependency>
</dependencies>
[Employee.java]
package com.ccteam.beans;
[EmployeeDao.java]
package com.ccteam.dao;
import com.ccteam.beans.Employee;
public interface EmployeeDao
{
public void create(Employee emp);
public Object getEmployeeSalary(int eno);
}
[EmployeeDaoImpl.java]
package com.ccteam.dao;
import com.ccteam.beans.Employee;
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;
import javax.sql.DataSource;
import java.util.Map;
[applicationContext.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">
</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;
}
}
jdbcCall = jdbcCall.withProcedureName("getAllEmployees");
jdbcCall = jdbcCall.returningResultSet("emps",BeanPropertyRowMapper.newInstance(Employee.class));
Example:
[COPY this Procedure in Database ]
[Employee.java]
package com.ccteam.beans;
return eno;
this.eno = eno;
return ename;
this.ename = ename;
return esal;
this.esal = esal;
return eaddr;
this.eaddr = eaddr;
[EmployeeDao.java]
package com.ccteam.dao;
import java.util.Map;
[EmployeeDaoImpl.java]
public class EmployeeDaoImpl implements EmployeeDao
this.dataSource = dataSource;
jdbcCall = jdbcCall.returningResultSet("emps",
BeanPropertyRowMapper.newInstance(Employee.class));
@Override
//System.out.println(map);
return map;
[applicationContext.xml]
</bean>
</bean>
[Main.java]
System.out.println(map);
System.out.println(list);
System.out.println("Employee Details");
System.out.println("ENO\tENAME\tESAL\tEADDR");
System.out.println("-----------------------------");
for(Employee e: list)
System.out.println(e.getEno()+"\t"+e.getEname()+"\t"+e.getEsal()+"\t"+e.getEaddr());
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");
Where,
in − SqlParameterSource object to pass a parameter to a stored function.
jdbcCall − SimpleJdbcCall object to represent a stored function.
DELIMITER //
BEGIN
SELECT SName
INTO out_name
RETURN out_name;
END
//
DELIMITER ;
[Student.java]
package com.ccteam.beans;
public class Student {
import com.ccteam.beans.Student;
import javax.sql.DataSource;
[StudentMapper.java]
package com.ccteam.dao;
import com.ccteam.beans.Student;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
[StudentDaoImpl.java]
import com.ccteam.beans.Student;
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;
import javax.sql.DataSource;
[Main.java]
package com.ccteam;
import com.ccteam.beans.Student;
import com.ccteam.dao.StudentDaoImpl;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContex
t;
StudentDaoImpl studentDaoImpl =
(StudentDaoImpl)context.getBean("student");