user569125
user569125

Reputation: 1463

Stored Procedure Call with Spring Framework

Can anyone provide complete example of Stored Procedure call with Spring framework.

Thanks, Raj

Upvotes: 1

Views: 11237

Answers (3)

user1917435
user1917435

Reputation: 23

The above solution won't work, as you cannot call a super class constructor in a sub class method. It has to be called within the sub class constructor

Upvotes: 0

Hoof
Hoof

Reputation: 1758

Create a Controller with reference to which you inject your datasource (applicationContext.xml):

<bean id="storedProcedureDao" class="com..myapp.SpringStoredProcedureDao">
    <property name="dataSource">
        <ref bean="jtdsDataSource"/>
    </property>
</bean>

Data source:

<bean id="jtdsDataSource" class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
    <property name="serverName">
        <value>servername</value>
    </property>
    <property name="databaseName">
        <value>database</value>
    </property>
    <property name="user">
        <value>username</value>
    </property>
    <property name="password">
        <value>password</value>
    </property>
</bean>

In your Controller, put the following:

public class SpringStoredProcedureDao extends StoredProcedure {
    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


    public CallStoredProcedure(String procedureName){
        super(this.dataSource, procedureName);              
        compile();
    }
}

This should more or less be it :)

Upvotes: 0

cmutt78
cmutt78

Reputation: 859

Using the Spring stored procedure framework:

jdbc-config.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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/orcl/DB"/>
    </bean>

    <bean id="storedProc" class="com.DatabaseStoredProc">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="aStoredProc" />
        <property name="parameters">
            <list>
                <bean class="org.springframework.jdbc.core.SqlParameter">
                    <constructor-arg index="0" value="p_id1" />
                    <constructor-arg index="1">
                        <util:constant static-field="java.sql.Types.VARCHAR" />
                    </constructor-arg>
                </bean>
                <bean class="org.springframework.jdbc.core.SqlParameter">
                    <constructor-arg index="0" value="p_id2" />
                    <constructor-arg index="1">
                        <util:constant static-field="java.sql.Types.VARCHAR" />
                    </constructor-arg>
                </bean>
            </list>
        </property>
    </bean>


</beans>

DatabaseStoredProcedure class

import java.util.Map;
import org.springframework.jdbc.object.StoredProcedure;

public class DatabaseStoredProc extends StoredProcedure {

    public Map<String, Object> execute(Map inputs){
        Map out=super.execute(inputs);
        return null;
    }

    // Method to map data to inputs Map:

public boolean businessRules(Object obj, Map inputs){
    SomeObject otd = (SomeObject) obj;
    inputs.put("p_id1", otd.getId1());
    inputs.put("p_id2", otd.getId2() );

    return true;
}
}

Upvotes: 2

Related Questions