import com.apress.prospring3.ch8.dao.ContactSfDao;
public class JdbcContactSfDaoSample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context-sf.xml");
ctx.refresh();
ContactSfDao contactSfDao = ctx.getBean("contactSfDao", ContactSfDao.class);
System.out.println(contactSfDao.getFirstNameById(1l));
}
}
In the program, we pass an ID of 1 into the stored function. This will return Clarence as the first
name if you ran the test-data.sql against the MySQL database. Running the program produces the
following output:
15:16:11,990 DEBUG g.springframework.jdbc.core.JdbcTemplate:
635 - Executing prepared SQL
query
15:16:11,991 DEBUG g.springframework.jdbc.core.JdbcTemplate:
570 - Executing prepared SQL
statement [select firstnamebyid(?)]
15:16:11,998 DEBUG ramework.jdbc.datasource.DataSourceUtils:
110 - Fetching JDBC Connection
from DataSource
15:16:12,289 DEBUG ramework.jdbc.datasource.DataSourceUtils:
332 - Returning JDBC Connection
to DataSource
Clarence
You can see that the first name was retrieved correctly.
What is presented here is just a simple sample to demonstrate Spring JDBC module's functions.
Spring also provides other classes (e.g., StoredProcedure) for you to invoke complex stored procedures
that return complex data types. We recommend you refer to Spring's reference manual in case you need
to access stored procedures using JDBC.
Using the Java Configuration
In case you prefer using the Java configuration class instead of the XML configuration, Listing 8-56
shows the Spring configuration class.
Listing 8-56. Using the Java Configuration
package com.apress.prospring3.ch8.javaconfig;
import javax.sql.DataSource;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.ComponentScan;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import
org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import
org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home