Spring for JDBC packages to use a CallBack to parameterized model of an operation

2009-10-21  来源:本站原创  分类:Java  人气:291 

Spring for JDBC, the JDBC package to avoid a large number of low-level operations, simplifying the code, let's take a look at Spring addition to using the Template Method pattern to simplify the code we write what techniques also used. Spring use a technique called CallBackHandler interface, you can easily extend this interface to get the results you want to query to the following lines give an access to information in a CallBackHandler, and then realize StringHandler get queries to the String
Example:

interface RowCallBackHandler ... (
public void abstract processRow (ResultSet rs) throws SQLException;
)

class StringHandler implements RowCallBackHandler ... (
private List list;
public void processRow (ResultSet rs) throws SQLException; ... (
list.add (rs.getString (1));
)

public String [] getStrings () ... (
return (String []) list.toArray (new String [list.size ()]))
)
)

public class JdbcTemplate ... (
/ / ... Other code
public query (String sql, RowCallBackHandler callBackHandler) ... (
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try ... (
/ / Con = <code to get connetion>
ps = con.prepareStatement (sql);
rs.executeQuery ();
while (rs.next ()) ... (
callBackHandler.processRow (rs);
)
rs.close ();
ps.close ();
) Catch (SQLException e) ... (
e.printStackTrace ();
) Finally ... (
DataSourceUtils.closeConnectonIfNecessary (this. DataSource, con);
)
)
)

public class SimpleTest ... (
public static void main (String [] args) ... (
StringHandler sh = new StringHandler ();
JdbcTemplate jdbcTemplate = new JdbcTemplate ();
jdbcTemplate.query ( "Select firstName from user", sh);
String [] str = sh.getStrings ();
)
)

The key technique is the model parameters of an operation and take the initiative to call in JdbcTemplate method processRow (), the results focus on the information added to the List, this can be said that a kind of inversion of control thoughts. The query result is automatically injected into the query process to receive query results of our container, users do not need to care about how to obtain query results, when you need a result, only getStrings () to obtain.
Users only need to use the following code:
StringHandler sh = new StringHandler ();
JdbcTemplate jdbcTemplate = new JdbcTemplate ();
jdbcTemplate.query ( "Select firstName from user", sh);
String [] str = sh.getStrings ();
Can easily access query results.
However, this model can be said is more of a skill level of intuitive enough so that the code is not easy to understand and maintain.

相关文章
  • Spring for JDBC packages to use a CallBack to parameterized model of an operation 2009-10-21

    Spring for JDBC, the JDBC package to avoid a large number of low-level operations, simplifying the code, let's take a look at Spring addition to using the Template Method pattern to simplify the code we write what techniques also used. Spring use a t

  • Spring的Bean生命周期内的callback方法和Spring容器启动关闭时的callback方法 2014-11-05

    通过Spring管理的bean,在其生命周期内可以配置被Spring调用的callback方法一般有两种:通过实现Spring提供的特点接口和通过配置callback方法.(如果bean是prototype的话,由于其销毁不被Spring管理,其销毁方法不会被调用) 1,通过实现Spring提供的特点接口:通过实现InitializingBean接口,Spring容器会在初始化bean的时候调用该bean的afterPropertiesSet方法,通过实现DisposableBean接口,Spr

  • Spring Framework's design concept and design patterns (8) - Agent Model 2010-08-31

    Keywords: Spring, design pattern, factory pattern, proxy pattern the last chapter: Spring Framework's design concept and design patterns (7)-Spring AOP features and implementation in http://javapub.javaeye.com/blog/751642 Next Chapter: Spring Framewo

  • Using JDBC and Spring's JDBC connection based on the database 2010-03-29

    Morning, two pieces of code written, one is using the jdk in the JDBC connect to the database, the code is as follows: Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://l

  • Enabled services integration _AOP spring and jdbc proxy 2010-07-21

    No built-in transaction mechanism JdbcTemplate You can use Spring's AOP proxy mechanism to achieve Realization: Profile <!-- The data source object --> <bean> <property name="driverClassName" value="com.mysql.jdbc.Driver"

  • spring 3 jdbc常用小结 2013-04-06

    spring 3的jdbc跟spring mvc搭配起来挺好用的,本文试着小结其主要使用方法,并举出spring 3.3.2中的一些信变化进行解析 1) 在dao中注入jdbctemplate,然后直接execute sql一下,当然配置文件要配置,如 Java代码 @Autowired private JdbcTemplate jdbcTemplate; ........ String sql="......."; jdbcTemplate.execute(sql); 配置文件: &

  • [Relearn Spring] 5 Jdbc Support 2010-05-06

    1. JdbcTemplate is thread-safe. So a single instance of JdbcTemplate will be enough for the whole system, unless you have more than one data sources. 2. NamedParameterJdbcTemplate String sql = "select * from t where t.firstName = :firstName " Yo

  • Spring of database operations using JDBC Template 2009-07-18

    Spring provides a template class for three options: ■ JdbcTemplate-The most basic of Spring's JDBC templates, this class provides simple access to a database through JDBC and simple indexed-parameter queries. (in accordance with the index parameter)

  • Spring JDBC Practice - Yale CAS login module, a typical customer-oriented 2008-07-18

    Project using the Yale CAS + Spring Security to achieve single sign-on, as well as access to the Yale CAS authentication requires a login module what changes, if the user enter the account password fails more than three times the number of times, we

  • spring JDBC Description 2010-03-19

    A .Spring JDBC overview Spring Provides a powerful template class JdbcTemplate simplify JDBC Operation, DataSource, JdbcTemplate can Bean Defined in the XML configuration file ,JdbcTemplate You only need to create a DataSource, the implanted applicat

  • Spring JDBC package using the basic CRUD operations 2010-04-06

    Spring's JDBC to think of it bad, Take up and study a little, by the way has written a small example, hope that some use for beginners Major attention to the following points: 1. For specific De small projects, Hen Duo's are added or vary the workloa

  • Spring HibernateTemplate and Callback 2010-08-27

    Spring in the Callback Model and Template patterns shared everywhere. The following example commonly used HibernateTemplate briefly summarized. HibernateTemplate template in a core class method: doExecute, the core of the method using the template ap

  • Spring JDBC template (HSQL database) (a) 2011-01-09

    This section focuses on Spring's JDBC templates, JDBC template to use, combined with Java open-source HSQL database to do a small detail. JDBC is not strange, it is the Java language specification relational database operations, is all kinds of JDBC

  • CSDN see the article about the Spring JDBC transaction management article (full-content comparison) 2011-07-08

    JDBC transaction management Spring provides programmatic transaction management (Programmatic transaction manage-ment) and declarative transaction management (Declarative transaction management), to provide different services to achieve a consistent

  • 使用Spring Jdbc (1) 2014-05-04

    文件及其环境要求: <?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:aop="http://www.springframework.o

  • Spring | JdbcTemplate & Mysql 2010-07-28

    These days primarily studied JDBC, and Spring in JDBC abstraction framework provided by the application. Spring JDBC abstraction framework provided by the core, datasource, object, and support the composition of four different packages. org.springfra

  • Spring jar package Detailed 2009-02-28

    AspectJ directory are in the Spring framework to use AspectJ source code and test program files. AspectJ is the first java application framework provided by the AOP. dist directory is a Spring release package, regarding release package described belo

  • spring of the jar package Detailed 2009-03-02

    Excerpt from: http://blog.csdn.net/exceljava/archive/2007/10/05/1811925.aspx Download the spring package of documents and a variety of packages large projects are often at only some of which we must, if not sure what package when necessary, then take

  • Spring.Hibernate.Struts1 integrated approach 2009-05-25

    http://zxmzfbdc.javaeye.com/blog/258946 [Original] Spring , Hibernate, Struts1 integrated approach Complete source code as an example of the attachment upload, in order to reduce the size of the jar does not contain documents used, please download th

  • Detailed packet Spring Jar 2009-06-04

    Yes there is a complete spring.jar release contains a single jar package, spring.jar contains spring-mock.jar except in the content contained in all other contents of jar package, because only in the development environment will be used next spring-m