Friday, May 21, 2010

Using Oracle Procedure in Spring

Hi,
I am posting a simple guide of using(calling) oracle procedures in spring. And I'm just a beginner in using spring and
procedures, so any clarifications pls let me know.

Create a class which extends Spring's StoredProcedure and in the constructor of the class, specify to add
DataSource. You can inject this datasource at runtime using spring beans.
You can specify a dataSource in spring beans xml, as

<bean id="dataSource" destroy-method="close" class="org.springframework.jdbc.datasource.DriverManagerDataSource>
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@<ip-address>:1521:INET10G" />
<property name="username" value="<username>" />
<property name="password" value="<password>" />
</bean>

Here you are just specifying Spring's DriverManagerDataSource class with relevant properties and use this bean id for injecting datasource to any class which needs it.

Now, for stored procedure create a class like

public ModelStoredProc extends StoredProcedure{


private static final String PROC_NAME="PR_INSERT_MODEL";

public ModelStoredProc(DataSource dataSource){
super(dataSource,PROC_NAME);

declareParameter(new SqlParameter(COLUMN_NAME1,OracleTypes.VARCHAR));
declareParameter(new SqlParameter(COLUMN_NAME2,OracleTypes.VARCHAR));
declareParameter(new SqlParameter(COLUMN_NAME3,OracleTypes.VARCHAR));
declareParameter(new SqlParameter(COLUMN_NAME4,OracleTypes.VARCHAR));

compile();
}

public Map executeInsert(Model model){

Map in=new HashMap();

in.put(COLUMN_NAME1, model.getColumnName1());
in.put(COLUMN_NAME2, model.getColumnName2());
in.put(COLUMN_NAME3, model.getColumnName3());
in.put(COLUMN_NAME4, model.getColumnName4());

return execute(in);
}

}
contd..

First Blog

Hello blog world, this is my first blog!

Thank you.