The use of the 21spring_jdbctemplatem Template Tool class-configuration file (connection pool of three database connections)

Source: Internet
Author: User
Tags aop

The previous article mentions that Drivermanagerdatasource is just spring's built-in database connection pool, and our options include C3P0 database connection pool and DBCP database connection pool.

So this article is about the form of the configuration file for the above three database connection pools.

The first: Drivermanagerdatasource:spring the built-in database connection pool.

The first step: Write the configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/sp Ring-beans-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://w Ww.springframework.org/schema/aop/spring-aop-2.5.xsd Http://www.springframework.org/schema/conte                           XT Http://www.springframework.org/schema/context/spring-context-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx- 2.5.xsd "><!--Configuring the database connection pool that comes with spring itself is equivalent to//1. Create a database connection pool, using spring's built-in connection pool Drivermanagerdatasource datasource=new    Drivermanagerdatasource ();    Connect Database driver Datasource.setdriverclassname ("Com.mysql.jdbc.Driver");    Datasource.seturl ("Jdbc:mysql:///spring3_day2");    Datasource.setusername ("root");    Datasource.setpassword ("root");  -<BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource"><!--Initialize the parameters inside the datasource -  < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"/>  < Propertyname= "url"value= "Jdbc:mysql:///spring3_day2"></ Property> < Propertyname= "username"value= "root"></ Property>< Propertyname= "Password"value= "root"></ Property></Bean><!--constructs a JdbcTemplate template object, which has the DataSource variable in the template object, which gives him the initial value this step is equivalent to://2. Constructing a template object from a connection pool jdbctemplate JdbcTemplate =new JdbcTemplate (dataSource); -<BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate">< Propertyname= "DataSource"ref= "DataSource"/></Bean></Beans>

Step Two: Write JUnit test code:

@RunWith (Springjunit4classrunner.  Class) @ContextConfiguration (Locations= "Classpath:applicationContext.xml")public  class  jdbctest {    // for annotations (based on type annotations), without this annotation, how spring assigns JdbcTemplate to a value     @Autowired    private  jdbctemplate jdbctemplate;    @Test    publicvoid  jdbctemplatetest ()    {        Jdbctemplate.execute ( "CREATE table Person2 (ID int primary key,name varchar)");}    }

Results:

A Person2 table was created.

The second type:

The use of the 21spring_jdbctemplatem Template Tool class-configuration file (connection pool of three database connections)

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.