Configuration of c3p0 Connection Pool in Spring and the Use of JdbcTemplate Complete Add ing () Method to Database by Injecting Various Required Objects into XML Configuration File

Keywords: Java xml JDBC Swift MySQL

Configuration of XML methods through configuration files

You can use a very concise Service class

The UserService class code is as follows:

package com.swift;

public class UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public boolean add() {
        return userDao.add();
    }
}

UserService needs a userDao object to call the method add() to connect the database in UserDao. This object injects the object through the configuration file XML and needs setUserDao() to cooperate with it.

Similarly, the UserDao class is very concise

package com.swift;

import org.springframework.jdbc.core.JdbcTemplate;

public class UserDao {
    private JdbcTemplate jdbcTemplate;
    
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public boolean add() {
//        ComboPooledDataSource dataSource=new ComboPooledDataSource();
//        dataSource.setDriverClass("com.mysql.jdbc.Driver");
//        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sw_database");
//        dataSource.setUser("root");
//        dataSource.setPassword("root");
    
    String sql="insert into sw_user(username,password) values(?,?)";
    int count=jdbcTemplate.update(sql,"Bullet scars","Battle chivalry song"); 
    if(count==1) {
        return true;
    }
    return false;
    }
}

To use the JdbcTemplate object in UserDao, which is also injected through the configuration file XML, you need to set JdbcTemplate () to cooperate.

And jdbcTemplate objects need to inject dataSource objects into the configuration through XML

The dataSource object is obtained through the c3p0 connection pool class and needs to import the jar package support provided in the previous essay

XML configuration file code:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- c3p0 Connection pool dataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property>
    <property name="user" value="root"></property>
    <property name="password" value="root"></property>
    </bean>
    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <bean id="userDao" class="com.swift.UserDao">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    
    <bean id="userService" class="com.swift.UserService">
    <property name="userDao" ref="userDao"></property>
    </bean>
    
</beans>

All you need to do in the test class is get the XML configuration file object.

The code is as follows:

package com.swift;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebServlet("/test")
public class ServletTest extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public ServletTest() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().append("Served at: ").append(request.getContextPath());
        
        //Use JdbcTemplat Of queryForObject Method
        ApplicationContext context=new ClassPathXmlApplicationContext("c3p0.xml");
        UserService userService= (UserService) context.getBean("userService");
        if(userService.add()) {
            response.getWriter().append("Add success!");
        }else {
            response.getWriter().append("Failed to add!");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

Results in the browser:

Results in the database:

Posted by netman182 on Thu, 07 Feb 2019 14:51:16 -0800