Spring JDBC Spring JDBC: For Live Spring & Hibernate Training, See THTT// LT
Spring JDBC Spring JDBC: For Live Spring & Hibernate Training, See THTT// LT
com
2008 coreservlets.com
For live Spring & Hibernate training, see courses at http://courses.coreservlets.com/. t htt // l t /
Taught by the experts that brought you this tutorial. Available at public venues, or customized versions venues can be held on-site at your organization.
C Courses d developed and t l d d taught b M t H ll ht by Marty Hall Courses developed and taught by EE Training: http://courses.coreservlets.com/ Customized Java coreservlets.com experts (edited by Marty)
Spring, Hibernate/JPA, EJB3, Ruby/Rails Java 5, Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF, Ajax, GWT, custom mix of topics
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Contact [email protected] for details Developed and taught by well-known author and developer. At public venues or onsite at your location.
2008 coreservlets.com
Introduction
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Motivation
public void save(Customer customer) { private boolean update(Customer customer, Connection conn) { Connection conn = null; PreparedStatement stmt = null; boolean autoCommit = false; try{ try{ stmt = conn.prepareStatement( conn prepareStatement( conn = this.dataSource.getConnection(); hi d i () "update customer set name = ? where id = ?"); autoCommit = conn.getAutoCommit(); stmt.setString(1, customer.getName()); conn.setAutoCommit(false); stmt.setString(2, customer.getId()); if(!update(customer, conn)){ return stmt.executeUpdate() > 0; insert(customer, conn); } } catch(SQLException e){ conn.commit(); throw new CustomerPersistenceException("Error: } SQL." catch(SQLException e){ + " Failed to update customer.",e); il d d ) if(conn != null){ } try{ finally{ conn.rollback(); if(stmt != null){ } try{ catch(SQLException suppressed){} stmt.close(); } stmt = null; throw new CustomerPersistenceException("Error: } SQL." catch(Exception suppressed){} + " Error saving customer: " + customer,e); } } } catch(RuntimeException e){ } if(conn != null){ try{ conn.rollback(); } catch(SQLException suppressed){} } throw new CustomerPersistenceException( Error: CustomerPersistenceException("Error: SQL." + " Error saving customer: " + customer,e); } finally{ if(conn != null){ try{ conn.setAutoCommit(autoCommit); conn.close(); conn = null; } catch(SQLException suppressed){} } } } private boolean insert(Customer customer, Connection conn) { PreparedStatement stmt = null; try{ stmt = conn.prepareStatement( conn prepareStatement( "insert into customer (id, name) values (?, ?)"); stmt.setString(1, customer.getId()); stmt.setString(2, customer.getName()); return stmt.executeUpdate() > 0; } catch(SQLException e){ throw new CustomerPersistenceException("Error: SQL." SQL " + " Failed to insert customer.",e); } finally{ if(stmt != null){ try{ stmt.close(); stmt = null; } catch(Exception suppressed){} } } }
Spring JDBC p g
Standalone JDBC software
N d No dependencies on a running Spring IoC container d i i S i I C t i
Templated software
Replaces tedious Java SQL APIs Mitigates JDBC resource mismanagement risks ii C i ik
SQL objects
SqlUpdate MappingSqlQuery
2008 coreservlets.com
Register beans
Register the spring-beans XML schema into the bean definitions file Assign bean identifiers using the id attribute Specify the bean creation method; e.g, the class attribute for direct constructor invocation
Initialize container
Select an ApplicationContext implementation
The integration method will depend on the target environment
Register Beans
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns http://www.springframework.org/schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> </beans>
Register Beans
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns http://www.springframework.org/schema/beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerQuery" class="coreservlets.mockup.CustomerQueryImpl" /> </beans>
Initialize Container
import org.springframework.context.support.*; public class Main { public static void main(String[]args) { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "/applicationContext.xml"); } }
2008 coreservlets.com
Runtime dependencies
spring-core.jar spring core jar spring-context.jar spring-beans.jar commons-logging.jar commons-logging jar
Standard output
2008 coreservlets.com
Specify the bean creation method; e.g, the class attribute for direct constructor invocation
Integrate configuration
Register the spring-context XML schema into the bean definitions file Declare a property-placeholder element with the configuration file path assigned to the l th i d t th location attribute ti tt ib t
Map DataSource configuration into DataSource
Register Beans
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns http://www.springframework.org/schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> </beans>
Register Beans
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns http://www.springframework.org/schema/beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerQuery" class="coreservlets.SpringJdbcCustomerQuery" /> <bean id="dataSource" class="coreservlets.util.EmbeddedDerbyDataSource" /> </beans>
Integrate Configuration g g
Create the properties file
dataSource.properties
Integrate Configuration g g
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns http://www.springframework.org/schema/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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="customerQuery" class="coreservlets.SpringJdbcCustomerQuery" /> <bean id="dataSource" class="coreservlets.util.EmbeddedDerbyDataSource" /> </beans> /b
Integrate Configuration g g
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns http://www.springframework.org/schema/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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:property-placeholder location="classpath:/dataSource.properties" /> <bean id="customerQuery" class="coreservlets.SpringJdbcCustomerQuery" /> <bean id="dataSource" b id "d t S " class="coreservlets.util.EmbeddedDerbyDataSource" /> </beans>
Java EE training: http://courses.coreservlets.com
Initialize Container
import org.springframework.context.support.*; public class Main { public static void main(String[]args) { BeanFactory beanFactory = new ClassPathXmlApplicationContext( "/applicationContext.xml"); } }
2008 coreservlets.com
Wrap-up
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
C Create applicationContext.xml
Save in an accessible location
Initialize container
Instantiate a BeanFactory
e g ClassPathXmlApplicationContext e.g.,
2008 coreservlets.com
Questions? Q ti ?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.