In my previous project, I will be required to call Stored Procedure in SQL Server with Spring & Hibernate. Using HibernateTemplate class extends from HibernateDaoSupport, you can call the stored procedure in SQL Server.
The Stored Procedure:
The Stored Procedure:
USE [sample]; GO SET ANSI_NULLS ON; GO SET QUOTED_IDENTIFIER ON; GO ALTER PROCEDURE [SP_Sample] (@Param1 nvarchar(50), @Param2 nvarchar(50)) AS declare @Concatenation nvarchar(150) set @Concatenation = @Param1 + ' and ' + @Param2 select @Param1,@Param2,@Concatenation GOSampleDao Source:
public class SampleDao extends HibernateDaoSupport { @SuppressWarnings("unchecked") public String callSP(String param1, String Param2) { // create session from active session Session ses = getSession(true); // create query for call the stored procedure SQLQuery q = ses.createSQLQuery("exec SP_Sample :param1, :param2"); q.setString("param1", param1); q.setString("param2", param2); //execute stored procedure and get list result List l = q.list(); StringBuilder result = new StringBuilder(); if (l.size()>0){ for(Iterator it=l.iterator();it.hasNext();){ Object[] row = (Object[]) it.next(); result.append("Param 1 : " + row[0] + ", Param 2 : " + row[1] + ", Concatenation : " + row[2]); } } return result.toString(); }