Spring jdbcTemplate, springjdbctemplate

Source: Internet
Author: User

Spring jdbcTemplate, springjdbctemplate

The spring framework provides a pure JDBC implementation method that can be used for development in programs requiring high performance and flexibility. The Spring JDBC framework consists of four parts, that is, core, datasource, object, and support.

Core Package: provides a JDBC template class. jdbcTemplate is the Core class of the core package.

Datasource package: provides tools to simplify the JDBC data source and some simple DataSource implementation classes, so that the links obtained by these DataSource can be automatically supported by Spring transaction management.

Object package: Provides relational data object representation, such as MappingSQLQuery, SQLUpdate, SQLCall, and SQLFunction.

Support Package: provides a conversion class and some tool classes for converting JDBC exceptions to DAO fee check exceptions.

The following describes the basic implementation steps of jdbcTemplate:

1. Import org. springframework. jdbc-*. jar ,:

<Bean id = "datasource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" $ {divername} "> </property> <! -- Driver configuration --> <property name = "url" value = "$ {utl}"> </property> <! -- Database connection url --> <property name = "username" value = "$ {username}"> </property> <! -- Database username --> <property name = "password" value = "$ {passworld}"> </property> <! -- Database Password --> </bean>

C3p0 data source Configuration:

The original data configuration of c3p0 requires the introduction of a jar package:

<Bean id = "datasource" class = "com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" driverClass "value =" $ {divername} "> </property> <! -- Driver configuration --> <property name = "jdbcUrl" value = "$ {url}"> </property> <! -- Database connection url --> <property name = "user" value = "$ {username}"> </property> <! -- Database username --> <property name = "password" value = ""> </property> <! -- Database Password --> </bean>

Original dbcp data Configuration:

To configure the dbcp data source, you also need to introduce the jar package:

<Bean id = "datasource" class = "org. apache. commons. dbcp. basicDataSource "> <property name =" driverClassName "value =" $ {divername} "> </property> <! -- Driver configuration --> <property name = "url" value = "$ {utl}"> </property> <! -- Database connection url --> <property name = "username" value = "$ {username}"> </property> <! -- Database username --> <property name = "password" value = "$ {passworld}"> </property> <! -- Database Password --> </bean>

3. Configure jdbcTemplate in the applicationContext. xml configuration file and inject dataSource to it.

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="datasource"/>    </bean>

4. Write the Dao interface to implement the class and use the jdbcTemplate API in its method to perform database operations.

Common Methods of JdbcTemplate API:

String export cute (new CallableStatementCreator () {@ Override public CallableStatement createCallableStatement (Connection con) throws SQLException {String storedProc = "{SQL statement for calling a stored procedure}"; CallableStatement cs = con. prepareCall (storedProc); cs. setString (1, "sdfd"); // input a String type parameter to the stored procedure (the first is the subscript of the parameter starting from 1 and the second is the parameter value) cs. registerOutParameter (2, Types. VARCHAR); // input the output parameter return cs to the stored procedure; }}, new CallableStatementCallback () {@ Override public Object doInCallableStatement (CallableStatement cs) throws SQLException, dataAccessException {cs.exe cute (); return cs. getString (2 );}});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.