spring学习(九)-spring jdbcTemplate的使用简介

2017-02-17 16:10

转载自传智博客学习视频
1.使用spring的jdbcTemplate进行数据库的操作

   package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;


public class JdbcTemplateDemo1 {

    @Test
    public void add() {
        //使用spring的jdbcTemplate进行数据库的添加操作,不需要传统的jdbc方法要使用大量的冗余代码保证流程的正常进行
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///springlearn");
        dataSource.setUsername("root");
        dataSource.setPassword("vislecaina");

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        String sql = "insert into user values(?,?)";
        int rows = jdbcTemplate.update(sql, "lucy", "250");
        System.out.println(rows);
    }

    @Test
    public void testJDBC() {
        //使用传统的jdbc驱动进行数据库的查询操作,此种方法需要大量的冗余代码以保证数据库查询操作的正常进行。
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql:///springlearn",
                    "root", "vislecaina");
            String sql = "select * from user where username=?";
            psmt = conn.prepareStatement(sql);
            psmt.setString(1, "lucy");
            rs = psmt.executeQuery();
            while(rs.next()){
                String username = rs.getString("username");
                String password = rs.getString("password");
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                System.out.println("user:"+user);
            }

        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try{
                rs.close();
                psmt.close();
                conn.close();
            } catch(Exception e){
                e.printStackTrace();
            }
        }
    }

}
package jdbc;

public class User {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }
}

**

2.使用c3p0数据库连接池进行数据库的查询操作,并将c3p0连接池的相关配置工作放到配置文件中进行

**

测试类:

package c3p0;

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

public class TestService {


    @Test
    public void  testDemo(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }

}
package c3p0;

import org.springframework.jdbc.core.JdbcTemplate;

public class UserDao {

    private JdbcTemplate jdbcTemplate;

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

    public void add() {
        String sql = "insert into user values(?,?)";
        jdbcTemplate.update(sql, "lilei", "520");
    }
}
package c3p0;

public class UserService {

    private UserDao userDao;

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

    public void add() {
        userDao.add();
    }

}
<?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.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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

    <!-- 配置c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 注入属性值 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///springlearn"></property>
        <property name="user" value="root"></property>
        <property name="password" value="vislecaina"></property>
    </bean>

    <bean id="userService" class="c3p0.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="userDao" class="c3p0.UserDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>

    </bean>

    <!-- 创建jdbc模板对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>

    </bean>
</beans>