Spring DBCP is configured in XML and Properties2 formats DataSource

Source: Internet
Author: User
Tags connection pooling

Spring provides database connection pooling: Dbcp configures DataSource and obtains connection completion database operations: Address of the Spring Help document: http://static.springsource.org/spring/docs/2.5.6/ reference/beans.html#  Beans-value-element the jar package that needs to be imported: Commons-dbcp.jarcommons-pool.jarmysql-connector-java-5.0-nightly-20071116-bin.jar 1. First configure the data source within the container [HTML] View plain copy
    1. <!--configuring data sources-
    2. <bean id= "myDataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
    3. <property name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/>
    4. <property name= "url" value= "Jdbc:mysql://localhost:3306/sms"/>
    5. <property name= "username" value= "root"/>
    6. <property name= "Password" value= "root"/>
    7. </bean>
2. Get the injected data source and write operations on the database [Java] View plain copy
  1. @Component ("UserService")
  2. public class Userserviceimpl implements userservice{
  3. Private Userdao Userdao;
  4. public void Setuserdao (Userdao Userdao) {
  5. This.userdao = Userdao;
  6. }
  7. @Resource//resource Injection
  8. Private DataSource myDataSource;
  9. Public DataSource Getmydatasource () {
  10. return mydatasource;
  11. }
  12. public void Setmydatasource (DataSource mydatasource) {
  13. This.mydatasource = myDataSource;
  14. }
  15. In the preceding method, add the logical
  16. public void Save () {
  17. try{
  18. Get the connection to perform the operation
  19. Connection conn = Mydatasource.getconnection ();
  20. Conn.createstatement (). Execute ("INSERT INTO dept values (' 6 ', ' Bumen2 ')");
  21. }catch (Exception e) {
  22. E.printstacktrace ();
  23. }
  24. }
  25. }
3. Test run [Java] View plain copy
    1. @Test
    2. public void test01 () {
    3. Beanfactory ApplicationContext = new Classpathxmlapplicationcontext (
    4. "Beans.xml");
    5. UserService user = (UserService) applicationcontext.getbean ("UserService");
    6. User.save ();
    7. }
--------------------------------------------------------------------------------------------------------------- -------------------Configure the Datasource:1 of XML by the properties, write the properties file: [HTML] View plain copy
    1. Jdbc.driverclassname=com.mysql.jdbc.driver
    2. Jdbc.url=jdbc:mysql://localhost:3306/sms
    3. Jdbc.username=root
    4. Jdbc.password=root
2. Writing XML container configuration: [HTML] View plain copy
  1. <!--placeholder Placeholder--
  2. <bean
  3. class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
  4. <property name= "Locations" >
  5. <value>classpath:jdbc.properties</value>
  6. </property>
  7. </bean>
  8. <bean id= "DataSource" destroy-method= "Close"
  9. class= "Org.apache.commons.dbcp.BasicDataSource" >
  10. <property name= "Driverclassname" value= "${jdbc.driverclassname}"/>
  11. <property name= "url" value= "${jdbc.url}"/>
  12. <property name= "username" value= "${jdbc.username}"/>
  13. <property name= "Password" value= "${jdbc.password}"/>
  14. </bean>

Spring DBCP is configured in XML and Properties2 formats DataSource

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.