Let's start the discussion of Spring JDBC support by looking at the lowest-level functionality. The
first thing you need to do before you can even think about running SQL queries is establish a connection
to the database.
Database Connections and DataSources
You can use Spring to manage the database connection for you by providing a bean that implements
javax.sql.DataSource. The difference between a DataSource and a Connection is that a DataSource
provides and manages Connections.
DriverManagerDataSource (under the package org.springframework.jdbc.datasource) is the simplest
implementation of a DataSource. By looking at the class name, you can guess that it simply calls the
DriverManager to obtain a connection. The fact that DriverManagerDataSource doesn't support database
connection pooling makes this class unsuitable for anything other than testing. The configuration of
DriverManagerDataSource is quite simple, as you can see in Listing 8-10; you just need to supply the
driver class name, a connection URL, a user name, and a password (datasource-drivermanager.xml).
Listing 8-10. Spring-Managed DriverManagerDataSource dataSource Bean
<?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
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<context:property-placeholder location="jdbc.properties"/>
</beans>
You most likely recognize the bold properties in the listing. They represent the values you normally
pass to JDBC to obtain a Connection interface. The database connection information typically is stored
in a properties file for easy maintenance and substitution in different deployment environments.
Listing 8-11 shows a sample jdbc.properties from which Spring's property placeholder will load the
connection information.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home